Replies: 1 comment 2 replies
-
@Nour-DEV Hi, This is more an Ajv question than a TypeBox question. However to make this work, you need to configure the The following should resolve things import { Type } from '@sinclair/typebox'
import Ajv from 'ajv'
const userSchema = Type.Object(
{
id: Type.Number(),
name: Type.String({ minLength: 3, unique: {table: "users", field: "name"} }),
password: Type.Optional(Type.String({ minLength: 3 }))
},
{
$id: 'User',
$async: true,
additionalProperties: false
}
)
const ajv = new Ajv({
allErrors: true,
messages: false
})
ajv.addKeyword({
keyword: 'unique',
type: 'string',
async: true,
validate: async (schema: any, data: any) => {
logger.info(schema, data)
const exists = await knex.table(schema.table).where(schema.field).first()
return !!exists
}
})
// const validate = dataValidator.compile(userSchema) // incorrect
const validate = ajv.compile(userSchema) // correct
try {
const result = await validate({name: 'somestring' })
} catch(e) {
// error about unique name should be shown here
} Hope this helps |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am trying to use typebox with ajv to validate user name to users table. so I need to make async validation with ajv. as I build the schema rules with typebox . I could not find any information about how to handle this case.
here is example of how I prepare typebox and ajv to handle this case (excluding how to handle the question part)
when I try to change it like:
I see error
Error: strict mode: unknown keyword: "unique"
Beta Was this translation helpful? Give feedback.
All reactions