diff --git a/next/src/validation/schema.ts b/next/src/validation/schema.ts index dbb3e5fee..63d7ccb04 100644 --- a/next/src/validation/schema.ts +++ b/next/src/validation/schema.ts @@ -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 } /** @@ -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) diff --git a/next/test/validation/boolean_schema.test.ts b/next/test/validation/boolean_schema.test.ts index f56b62eb9..a4c4ffda9 100644 --- a/next/test/validation/boolean_schema.test.ts +++ b/next/test/validation/boolean_schema.test.ts @@ -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([]) + }) })