Skip to content

Commit bb59dac

Browse files
lukadantoniocapelo
andauthored
feat(next): add allowForbiddenValues validation option (#194)
* add allowForbiddenValues validation option * fix description comment for the new option Co-authored-by: Capelo <[email protected]> --------- Co-authored-by: Capelo <[email protected]>
1 parent 6f2e7d7 commit bb59dac

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

next/src/validation/schema.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export interface ValidationOptions {
2121
* @default false
2222
*/
2323
treatNullAsUndefined?: boolean
24+
25+
/**
26+
* If true, providing a value for a schema that is `false` won't create an error
27+
* @default false
28+
*/
29+
allowForbiddenValues?: boolean
2430
}
2531

2632
/**
@@ -189,7 +195,12 @@ export function validateSchema(
189195

190196
// Handle boolean schemas
191197
if (typeof schema === 'boolean') {
192-
return schema ? [] : [{ path, validation: 'forbidden', schema, value }]
198+
// When the boolean schema is false, we will return an error, but only when forbidden values are not explicitly
199+
// allowed per the allowForbiddenValues option.
200+
if (!schema && !options.allowForbiddenValues) {
201+
return [{ path, validation: 'forbidden', schema, value }]
202+
}
203+
return []
193204
}
194205

195206
// Check if it is a file input (needed early for null check)

next/test/validation/boolean_schema.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ describe('boolean schema validation', () => {
1414
expect(validateSchema({ name: 'anything' }, schema)).toEqual([])
1515
expect(validateSchema({}, schema)).toEqual([])
1616
})
17+
18+
it('does not return an error if the value is false and allowForbiddenValues is true', () => {
19+
const schema = { type: 'object', properties: { name: false } }
20+
expect(validateSchema({ name: 'anything' }, schema, { allowForbiddenValues: true })).toEqual([])
21+
expect(validateSchema({}, schema, { allowForbiddenValues: true })).toEqual([])
22+
})
1723
})

0 commit comments

Comments
 (0)