Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/validation/number.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ValidationError, ValidationErrorPath } from '../errors'
import type { NonBooleanJsfSchema, SchemaValue } from '../types'
import { getSchemaType } from './schema'
import { schemaTypeIncludes } from './schema'

interface DecimalParts {
digits: bigint
Expand Down Expand Up @@ -81,13 +81,12 @@ export function validateNumber(
path: ValidationErrorPath = [],
): ValidationError[] {
const errors: ValidationError[] = []
const schemaType = getSchemaType(schema)

if (typeof value !== 'number') {
return []
}

if (schemaType !== undefined && !['number', 'integer'].includes(schemaType as string)) {
if (!schemaTypeIncludes(schema, 'number', 'integer')) {
return []
}

Expand Down
15 changes: 15 additions & 0 deletions src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ export function getSchemaType(schema: JsfSchema): JsfSchemaType | JsfSchemaType[
return undefined
}

/**
* Whether the schema's type (a single type or a list of types) includes any of the given types.
* A schema without a type places no restriction, so it matches every type.
*/
export function schemaTypeIncludes(schema: JsfSchema, ...types: JsfSchemaType[]): boolean {
const schemaType = getSchemaType(schema)

if (schemaType === undefined) {
return true
}

const schemaTypes = Array.isArray(schemaType) ? schemaType : [schemaType]
return types.some(type => schemaTypes.includes(type))
}

/**
* Validate the type of a value against a schema
* @param value - The value to validate
Expand Down
5 changes: 2 additions & 3 deletions src/validation/string.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ValidationError, ValidationErrorPath } from '../errors'
import type { NonBooleanJsfSchema, SchemaValue } from '../types'
import { validateFormat } from './format'
import { getSchemaType } from './schema'
import { schemaTypeIncludes } from './schema'

/**
* Validate a string against a schema
Expand All @@ -21,13 +21,12 @@ export function validateString(
path: ValidationErrorPath = [],
): ValidationError[] {
const errors: ValidationError[] = []
const schemaType = getSchemaType(schema)

if (typeof value !== 'string') {
return []
}

if (schemaType !== undefined && schemaType !== 'string') {
if (!schemaTypeIncludes(schema, 'string')) {
return []
}

Expand Down
38 changes: 38 additions & 0 deletions test/validation/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,44 @@ describe('number validation', () => {
])
})

it('validates numeric keywords when the type also allows null', () => {
const nullableNumber = { type: ['number', 'null'], minimum: 1, maximum: 39, multipleOf: 2 }

expect(validateSchema(null, nullableNumber)).toEqual([])
expect(validateSchema(20, nullableNumber)).toEqual([])
expect(validateSchema(40, nullableNumber)).toEqual([
errorLike({
path: [],
validation: 'maximum',
}),
])
expect(validateSchema(0, nullableNumber)).toEqual([
errorLike({
path: [],
validation: 'minimum',
}),
])
expect(validateSchema(21, nullableNumber)).toEqual([
errorLike({
path: [],
validation: 'multipleOf',
}),
])

expect(validateSchema(40, { type: ['integer', 'null'], maximum: 39 })).toEqual([
errorLike({
path: [],
validation: 'maximum',
}),
])
expect(validateSchema(40, { type: ['null', 'number'], exclusiveMaximum: 40 })).toEqual([
errorLike({
path: [],
validation: 'exclusiveMaximum',
}),
])
})

it('validates the number against the exclusiveMinimum and exclusiveMaximum properties', () => {
expect(validateSchema(11, { type: 'number', exclusiveMinimum: 10 })).toEqual([])
expect(validateSchema(10, { type: 'number', exclusiveMinimum: 10 })).toEqual([
Expand Down
24 changes: 24 additions & 0 deletions test/validation/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,28 @@ describe('string validation', () => {
// Error message now includes a random example, so we can't do an exact match
expect(response.formErrors?.name).toMatch(/^Must have a valid format. E.g./)
})

it('validates string keywords when the type also allows null', () => {
const { handleValidation } = createHeadlessForm({
type: 'object',
properties: {
short: { type: ['string', 'null'], maxLength: 3 },
long: { type: ['string', 'null'], minLength: 3 },
code: { type: ['null', 'string'], pattern: '^[a-z]+$' },
},
})

expect(handleValidation({ short: null, long: null, code: null })).not.toHaveProperty('formErrors')
expect(handleValidation({ short: 'abc', long: 'abc', code: 'abc' })).not.toHaveProperty(
'formErrors',
)

expect(handleValidation({ short: 'abcd', long: 'ab', code: '123' })).toMatchObject({
formErrors: {
short: 'Please insert up to 3 characters',
long: 'Please insert at least 3 characters',
code: expect.stringMatching(/^Must have a valid format. E.g./),
},
})
})
})
Loading