Skip to content

Commit

Permalink
fix(initial-value-templates): comment out check for _type
Browse files Browse the repository at this point in the history
This check currently fails a valid case where you set an initial value for an "anonymous" object field and omit the `_type` in the initial value. E.g. the following valid example will fail unless this line is commented out:

```js
export const initialValuesTest = {
  name: 'initialValuesTest',
  type: 'document',
  fields: [
    {
      name: 'simpleObject',
      title: 'Simple object',
      type: 'object',
      fields: [{name: 'field1', type: 'string'}],
    },
  ],
initialValue: {
    title: 'foo',
    simpleObject: {
      field1: 'hello',
    },
  },
}
```
  • Loading branch information
bjoerge committed Apr 28, 2021
1 parent 28593a0 commit 96416de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/@sanity/initial-value-templates/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ function validateValue(value: any, path: (string | number)[] = [], parentIsArray
// In the case of references, we know what the type should be, so apply it
initial._type = 'reference'
} else {
throw new Error(`missing "_type" property at path "${pathToString(path)}"`)
// todo: consider if we need to re-instantiate this. It currently makes the valid case of having an initial object value for a field fail
// throw new Error(`missing "_type" property at path "${pathToString(path)}"`)
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/@sanity/initial-value-templates/test/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ describe('resolveInitialValue', () => {

test('throws on wrong value type resolved', () => {
expect(resolveInitialValue(schema, {...example, value: () => null})).rejects.toMatchObject({
message: 'Template "author" initial value: resolved to a non-object',
message:
'Template "author" has invalid "value" property - must be a plain object or a resolver function returning a plain object',
})
})

test('throws on values with sub-objects missing `_type`', () => {
// todo: we should validate based on schema type here and reenable this test
// Currently the initial value validator is not schema aware and fails if resolved initial value is missing _type
// but this doesn't account for fields of type object, which is a valid case for omitting _type.
xtest('throws on values with sub-objects missing `_type`', () => {
expect(
resolveInitialValue(schema, {
...example,
Expand Down

0 comments on commit 96416de

Please sign in to comment.