Adding a Custom Validation Predicate #1401
-
I have a schema, and for a particular field on the schema, I'd like to supplement schema validation with a custom function. I Googled this, and unfortunately, Google's AI fed me some BS that got my hopes up, but its answer demonstrates what I'm looking for: const MySchema = Type.Object({
customValidationField: Type.Unsafe<string>({
validate: (value: unknown) => {
// Implement your custom validation logic
return typeof value === 'string' && value.length > 5;
},
}),
}); This doesn't even allow the schema to compile/be checked (it throws a Is anything like this possible via Typebox? Of course, I could just add some supplemental validation outside of Typebox, but that leads to the unfortunate case where if some code bypasses my supplemental validation and checks against Typebox directly, the extra validation might be missed. I'd ideally like the predicate to be tied to the field so any time Typebox does validation, the predicate will run and be taken into consideration. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@carcigenicate Hello,
You shouldn't need a custom type for this, the following should work in TypeBox 0.34.x import { Type } from '@sinclair/typebox' // 0.34.x
const MySchema = Type.Object({
customValidationField: Type.String({ minLength: 6 }), // > 5
}); But if you are using TypeBox 1.0, you can also do this. import Type from 'typebox' // 1.0
const MySchema = Type.Object({
customValidationField: Type.Refine(Type.String(), value => value.length > 5),
}) But for simple constraints such as this, you should use Hope this helps |
Beta Was this translation helpful? Give feedback.
-
You can use either TypeBox or Ajv for custom validation, but I would recommend using Ajv as the mechanisms for custom validation aligns better to how TypeBox 1.0 will be handling things in future. Here is how you can express custom validation schematics with 0.34.33 (but assumes you have setup the Ajv import { Type, Static, TSchema } from '@sinclair/typebox'
// ---------------------------------------------------------------
// TChecked
//
// This type just models whatever keyword you're adding to Ajv
// via the ajv.addKeyword function.
//
// ---------------------------------------------------------------
export interface TChecked<Value = unknown> extends TSchema {
static: Value // required for inference in typebox 0.34.x
checked: true
validate: (value: unknown) => boolean
}
export function Checked<Value>(validate: (value: unknown) => boolean): TChecked<Value> {
// return whatever structure is required to match ajv.addKeyword(...)
return { checked: true, validate } as never
}
// Usage
type MySchema = Static<typeof MySchema>
const MySchema = Type.Object({
customValidationField: Checked<string>(value =>
typeof value === 'string' && value.length > 5
),
}); |
Beta Was this translation helpful? Give feedback.
You can use either TypeBox or Ajv for custom validation, but I would recommend using Ajv as the mechanisms for custom validation aligns better to how TypeBox 1.0 will be handling things in future. Here is how you can express custom validation schematics with 0.34.33 (but assumes you have setup the Ajv
addKeyword
correctly)TypeScript Link Here