Reputation: 1739
I have started using typebox recently in my nodejs project.
Most of the formats and validations are not working and compiling properly Like Type.Strict() -> does not enforce any strict schema like zod
I tried to add formats for email check that also not recognised
Do we have to add ajv additionally for these formats but then ajv-formats library is depricated and https://www.npmjs.com/package/ajv-formats-draft2019 is being used now
I tried to go without ajv as per doc and added format but its not working.
This is as per doc:
You can pass Json Schema options on the last argument of any given type. Option hints specific to each type are provided for convenience.
// String must be an email
const T = Type.String({ // const T = {
format: 'email' // type: 'string',
}) // format: 'email'
// }
// Number must be a multiple of 2
const T = Type.Number({ // const T = {
multipleOf: 2 // type: 'number',
}) // multipleOf: 2
// }
// Array must have at least 5 integer values
const T = Type.Array(Type.Integer(), { // const T = {
minItems: 5 // type: 'array',
}) // minItems: 5,
// items: {
// type: 'integer'
// }
// }
Validation :
import { Type, Static } from '@sinclair/typebox';
import { TypeCompiler } from '@sinclair/typebox/compiler';
export const userBSchema = Type.Object({
id: Type.String(),
username: Type.String(),
age: Type.Number({
multipleOf: 2
}),
firstName: Type.String(),
isActive: Type.Boolean(),
email: Type.String({ format: 'email' }),
address: Type.Object({
street: Type.String(),
city: Type.String(),
state: Type.String(),
zipCode: Type.String()
})
});
export const createManyUserBRequestBodySchema = Type.Pick(
userBSchema,
['username', 'age', 'firstName', 'email', 'address'],
{
additionalProperties: false
}
);
export const createManyUserBRequestSchema = Type.Object({
body: Type.Array(createManyUserBRequestBodySchema)
});
export type CreateManyUserBRequestType = Static<typeof createManyUserBRequestSchema>;
Usage
async myFUnction(input) => {
try {
const C = TypeCompiler.Compile(createManyUserBRequestSchema);
const isValid = C.Check(input.request);
if (isValid) {
const output = await this.createManyUserUseCase.execute(input.request.body);
return {
message: 'Many User Api Processed Successfully',
result: output
};
}
const all = [...C.Errors(input.request)];
throw new AppError({
name: ApplicationErrorType.VALIDATION_ERROR,
message: JSON.stringify(
all.map((item) => {
return `error is ${item.message}`;
})
),
cause: all
});
} catch (err) {
return {
result: Result.fail<Error>(err)
};
}
}
And my input is
Input Json
[
{
"username": "alpha_user",
"age": 2,
"firstName": "ALpha",
"email": "[email protected]",
"address": {
"street": "S",
"city": "AMX",
"state": "UK",
"zipCode": "54367"
}
},
{
"username": "beta",
"age": 22,
"firstName": "beta",
"email": "[email protected]",
"address": {
"street": "kotavilla",
"city": "BETA",
"state": "UK",
"zipCode": "54367"
}
},
{
"username": "gamma",
"age": 24,
"firstName": "gamma",
"email": "[email protected]",
"address": {
"street": "kota2villa",
"city": "GAMMA",
"state": "UK",
"zipCode": "54367"
}
}
]
This is error:
"message": "[\"error is Unknown format 'email'\",\"error is Unknown format 'email'\",\"error is Unknown format 'email'\"]",
The email format is correct still not recognized
Upvotes: 0
Views: 91
Reputation: 8428
The format
keyword does not perform validation by default. It has always been descriptive/annotative.
I'm not sure about the system that you're using, though. There may be a configuration that enables format
validation.
Upvotes: 0