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
13 changes: 12 additions & 1 deletion next/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export interface ValidationOptions {
* @default false
*/
treatNullAsUndefined?: boolean

/**
* If true, providing a value for a schema that is `false` won't create an error
* @default false
*/
allowForbiddenValues?: boolean
}

/**
Expand Down Expand Up @@ -189,7 +195,12 @@ export function validateSchema(

// Handle boolean schemas
if (typeof schema === 'boolean') {
return schema ? [] : [{ path, validation: 'forbidden', schema, value }]
// When the boolean schema is false, we will return an error, but only when forbidden values are not explicitly
// allowed per the allowForbiddenValues option.
if (!schema && !options.allowForbiddenValues) {
return [{ path, validation: 'forbidden', schema, value }]
}
return []
}

// Check if it is a file input (needed early for null check)
Expand Down
6 changes: 6 additions & 0 deletions next/test/validation/boolean_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ describe('boolean schema validation', () => {
expect(validateSchema({ name: 'anything' }, schema)).toEqual([])
expect(validateSchema({}, schema)).toEqual([])
})

it('does not return an error if the value is false and allowForbiddenValues is true', () => {
const schema = { type: 'object', properties: { name: false } }
expect(validateSchema({ name: 'anything' }, schema, { allowForbiddenValues: true })).toEqual([])
expect(validateSchema({}, schema, { allowForbiddenValues: true })).toEqual([])
})
})