Skip to content

Commit

Permalink
test(schema): test block members validation
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin committed Sep 6, 2023
1 parent 9c8bb83 commit 0753806
Showing 1 changed file with 118 additions and 1 deletion.
119 changes: 118 additions & 1 deletion packages/@sanity/schema/test/validation/validation.test.ts
@@ -1,7 +1,12 @@
import {flatten} from 'lodash'
import {traverseSchema} from '../../src/core/traverseSchema'
import {validateSchema} from '../../src/sanity/validateSchema'
import object from '../../src/sanity/validation/types/object'
import array from '../../src/sanity/validation/types/array'
import block from '../../src/sanity/validation/types/block'
import file from '../../src/sanity/validation/types/file'
import image from '../../src/sanity/validation/types/image'
import object from '../../src/sanity/validation/types/object'
import reference from '../../src/sanity/validation/types/reference'

describe('Validation test', () => {
test('assigns/populates `_problems` property', () => {
Expand Down Expand Up @@ -89,4 +94,116 @@ describe('Validation test', () => {
helpId: 'schema-standalone-block-type',
})
})

test('validate block members as object like', () => {
const schemaDef = [
{
title: 'Valid object',
name: 'validObject',
type: 'object',
fields: [
{
title: 'Blocks',
name: 'blocks',
type: 'array',
of: [{type: 'block', of: [{type: 'image', name: 'myImage'}]}],
},
],
},
{
title: 'Invalid object',
name: 'invalidObject',
type: 'object',
fields: [
{
title: 'Blocks',
name: 'blocks',
type: 'array',
of: [
{
type: 'block',
of: [
// Should produce error
{type: 'string', name: 'foo'},
// Should produce warning
{type: 'object', name: 'validObject', fields: [{type: 'string', name: 'foo'}]},
// Should be allowed
{type: 'image', name: 'image'},
// Should be allowed
{type: 'reference', name: 'reference', to: {type: 'author'}},
// Should produce warning
{type: 'object', name: 'reference', fields: [{type: 'string', name: 'foo'}]},
// Should produce warning
{type: 'object', name: 'image', fields: [{type: 'string', name: 'foo'}]},
// Should produce warning
{type: 'object', name: 'file', fields: [{type: 'string', name: 'foo'}]},
// Should produce warning
{type: 'object', name: 'span', fields: [{type: 'string', name: 'foo'}]},
// Should not be allowed
{type: 'span', name: 'something', fields: [{type: 'string', name: 'foo'}]},
// Should be allowed
{type: 'reference', name: 'reference', to: {type: 'author'}},
],
},
],
},
],
},
]
const coreTypes = [
{name: 'array', type: 'type'},
{name: 'object', type: 'type'},
{name: 'block', type: 'type'},
{name: 'reference', type: 'type'},
{name: 'image', type: 'type'},
{name: 'file', type: 'type'},
]

const visitors = {
array: {visit: array},
object: {visit: object},
block: {visit: block},
reference: {visit: reference},
image: {visit: image},
file: {visit: file},
}

const validation = traverseSchema(schemaDef, coreTypes, (typeDef, visitorContext) => {
const visitor = visitors[typeDef.type]
return visitor ? visitor.visit(typeDef, visitorContext) : typeDef
})

const validObjectResult = validation.get('validObject')
expect(validObjectResult._problems).toHaveLength(0)

const invalidObjectResult = validation.get('invalidObject')
const problems = flatten(
invalidObjectResult.fields[0].of[0].of.map((item) => item._problems),
).filter(Boolean)
expect(problems).toHaveLength(6)
expect(problems[0]).toMatchObject({
severity: 'error',
helpId: 'schema-array-of-type-builtin-type-conflict',
})
expect(problems[1]).toMatchObject({
severity: 'warning',
helpId: 'schema-array-of-type-global-type-conflict',
})
expect(problems[2]).toMatchObject({
severity: 'warning',
helpId: 'schema-array-of-type-global-type-conflict',
})
expect(problems[3]).toMatchObject({
severity: 'warning',
helpId: 'schema-array-of-type-global-type-conflict',
})
expect(problems[4]).toMatchObject({
severity: 'warning',
helpId: 'schema-array-of-type-global-type-conflict',
})
expect(problems[5]).toMatchObject({
severity: 'error',
helpId: 'schema-array-of-type-builtin-type-conflict',
})
})
})

0 comments on commit 0753806

Please sign in to comment.