-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(validation): resolve array items without _type (#2715)
- Loading branch information
1 parent
7c40692
commit 4bd8082
Showing
5 changed files
with
206 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import typeString from './typeString' | ||
|
||
describe('typeString', () => { | ||
it('returns the a type string of built in types', () => { | ||
expect(typeString({})).toBe('Object') | ||
expect( | ||
typeString(function () { | ||
// intentionally blank | ||
}) | ||
).toBe('Function') | ||
expect(typeString(['hey'])).toBe('Array') | ||
expect(typeString('some string')).toBe('String') | ||
expect(typeString(false)).toBe('Boolean') | ||
expect(typeString(5)).toBe('Number') | ||
expect(typeString(new Date())).toBe('Date') | ||
}) | ||
|
||
it('returns a type string string using the constructor', () => { | ||
class ExampleClass {} | ||
expect(typeString(new ExampleClass())).toBe('ExampleClass') | ||
}) | ||
|
||
it('returns a type string for null or undefined', () => { | ||
expect(typeString(null)).toBe('null') | ||
expect(typeString(undefined)).toBe('undefined') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
packages/@sanity/validation/src/validateDocument.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/// <reference types="@sanity/types/parts" /> | ||
|
||
import {Rule, SanityDocument, Schema} from '@sanity/types' | ||
import createSchema from 'part:@sanity/base/schema-creator' | ||
import validateDocument, {resolveTypeForArrayItem} from './validateDocument' | ||
|
||
describe('validateDocument', () => { | ||
it('takes in a document + a compiled schema and returns a list of validation markers', async () => { | ||
const schema = createSchema({ | ||
types: [ | ||
{ | ||
name: 'simpleDoc', | ||
type: 'document', | ||
title: 'Simple Document', | ||
fields: [ | ||
{ | ||
name: 'title', | ||
type: 'string', | ||
validation: (rule: Rule) => rule.required(), | ||
}, | ||
], | ||
}, | ||
], | ||
}) | ||
|
||
const document: SanityDocument = { | ||
_id: 'testId', | ||
_createdAt: '2021-08-27T14:48:51.650Z', | ||
_rev: 'exampleRev', | ||
_type: 'simpleDoc', | ||
_updatedAt: '2021-08-27T14:48:51.650Z', | ||
title: null, | ||
} | ||
|
||
const result = await validateDocument(document, schema) | ||
expect(result).toMatchObject([ | ||
{ | ||
type: 'validation', | ||
level: 'error', | ||
item: { | ||
message: 'Expected type "String", got "null"', | ||
paths: [], | ||
}, | ||
path: ['title'], | ||
}, | ||
{ | ||
type: 'validation', | ||
level: 'error', | ||
item: { | ||
message: 'Required', | ||
paths: [], | ||
}, | ||
path: ['title'], | ||
}, | ||
]) | ||
}) | ||
|
||
it('should be able to resolve an array item type if there is just one type', async () => { | ||
const schema = createSchema({ | ||
types: [ | ||
{ | ||
name: 'testDoc', | ||
type: 'document', | ||
title: 'Test Document', | ||
fields: [ | ||
{ | ||
name: 'values', | ||
type: 'array', | ||
// note that there is only one type available | ||
of: [{type: 'arrayItem'}], | ||
validation: (rule: Rule) => rule.required(), | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'arrayItem', | ||
type: 'object', | ||
fields: [{name: 'title', type: 'string'}], | ||
}, | ||
], | ||
}) | ||
|
||
const document: SanityDocument = { | ||
_id: 'testId', | ||
_createdAt: '2021-08-27T14:48:51.650Z', | ||
_rev: 'exampleRev', | ||
_type: 'testDoc', | ||
_updatedAt: '2021-08-27T14:48:51.650Z', | ||
values: [ | ||
{ | ||
// note how this doesn't have a _type | ||
title: 5, | ||
_key: 'exampleKey', | ||
}, | ||
], | ||
} | ||
|
||
await expect(validateDocument(document, schema)).resolves.toEqual([ | ||
{ | ||
type: 'validation', | ||
level: 'error', | ||
item: { | ||
message: 'Expected type "String", got "Number"', | ||
paths: [], | ||
}, | ||
path: ['values', {_key: 'exampleKey'}, 'title'], | ||
}, | ||
]) | ||
}) | ||
}) | ||
|
||
describe('resolveTypeForArrayItem', () => { | ||
const schema: Schema = createSchema({ | ||
types: [ | ||
{ | ||
name: 'foo', | ||
type: 'object', | ||
fields: [{name: 'title', type: 'number'}], | ||
}, | ||
{ | ||
name: 'bar', | ||
type: 'object', | ||
fields: [{name: 'title', type: 'string'}], | ||
}, | ||
], | ||
}) | ||
|
||
const fooType = schema.get('foo') | ||
const barType = schema.get('bar') | ||
|
||
it('finds a matching schema type for an array item value given a list of candidate types', () => { | ||
const resolved = resolveTypeForArrayItem( | ||
{ | ||
_type: 'bar', | ||
_key: 'exampleKey', | ||
title: 5, | ||
}, | ||
[fooType, barType] | ||
) | ||
|
||
expect(resolved).toBe(barType) | ||
}) | ||
|
||
it('assumes the type if there is only one possible candidate', () => { | ||
const resolved = resolveTypeForArrayItem( | ||
{ | ||
// notice no _type | ||
_key: 'exampleKey', | ||
title: 5, | ||
}, | ||
[fooType] | ||
) | ||
|
||
expect(resolved).toBe(fooType) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4bd8082
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
test-studio – ./
test-studio-git-next.sanity.build
test-studio.sanity.build
4bd8082
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
perf-studio – ./
perf-studio.sanity.build
perf-studio-git-next.sanity.build