-
Notifications
You must be signed in to change notification settings - Fork 394
/
createSchema.js
54 lines (47 loc) · 1.6 KB
/
createSchema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Schema from '@sanity/schema'
import legacyRichDate from 'part:@sanity/form-builder/input/legacy-date/schema?'
import validateSchema from '@sanity/schema/lib/sanity/validateSchema'
import groupProblems from '@sanity/schema/lib/sanity/groupProblems'
import {inferFromSchema as inferValidation} from '@sanity/validation'
import slug from './types/slug'
import geopoint from './types/geopoint'
import imageCrop from './types/imageCrop'
import imageHotspot from './types/imageHotspot'
import assetSourceData from './types/assetSourceData'
import imageAsset from './types/imageAsset'
import imagePalette from './types/imagePalette'
import imagePaletteSwatch from './types/imagePaletteSwatch'
import imageDimensions from './types/imageDimensions'
import imageMetadata from './types/imageMetadata'
import fileAsset from './types/fileAsset'
const isError = problem => problem.severity === 'error'
module.exports = schemaDef => {
const validated = validateSchema(schemaDef.types).getTypes()
const validation = groupProblems(validated)
const hasErrors = validation.some(group => group.problems.some(isError))
let types = []
if (!hasErrors) {
types = [
...schemaDef.types,
assetSourceData,
slug,
geopoint,
legacyRichDate,
imageAsset,
fileAsset,
imageCrop,
imageHotspot,
imageMetadata,
imageDimensions,
imagePalette,
imagePaletteSwatch
].filter(Boolean)
}
const compiled = Schema.compile({
name: schemaDef.name,
types
})
compiled._source = schemaDef
compiled._validation = validation
return inferValidation(compiled)
}