Skip to content

Commit

Permalink
make type validation checks easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
lukad committed Feb 4, 2025
1 parent d3316af commit c96207b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions next/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ function validateType(value: SchemaValue, schema: JsfSchema): ValidationError[]
const schemaType = getSchemaType(schema)
const valueType = value === undefined ? 'undefined' : typeof value

if (Array.isArray(schemaType) ? !schemaType.includes(valueType) : valueType !== schemaType) {
return [{ path: [], validation: 'type', message: `should be ${schemaType}` }]
const hasTypeMismatch = Array.isArray(schemaType)
? !schemaType.includes(valueType)
: valueType !== schemaType

if (hasTypeMismatch) {
return [{
path: [],
validation: 'type',
message: `should be ${Array.isArray(schemaType)
? schemaType.join(' | ')
: schemaType}`,
}]
}

return []
Expand Down

0 comments on commit c96207b

Please sign in to comment.