Skip to content

Commit

Permalink
[field] Iterate over object field values when validating
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent 4804d5a commit aea76ed
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/@sanity/field/src/validation/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SchemaType} from '../diff'
import {SchemaType, ObjectSchemaType} from '../diff'

export function getValueError(value: unknown, schemaType: SchemaType) {
const {jsonType} = schemaType
Expand All @@ -12,5 +12,17 @@ export function getValueError(value: unknown, schemaType: SchemaType) {
return {error: `Value is ${valueType}, expected ${jsonType}`, value}
}

if (isObjectType(schemaType) && isObjectValue(value)) {
return schemaType.fields.find(field => getValueError(value[field.name], field.type))
}

return undefined
}

function isObjectType(schemaType: SchemaType): schemaType is ObjectSchemaType {
return schemaType.jsonType === 'object'
}

function isObjectValue(value: unknown): value is Record<string, unknown> {
return value !== null && !Array.isArray(value) && typeof value === 'object'
}

0 comments on commit aea76ed

Please sign in to comment.