Skip to content

how to use ajv addKeyword with typebox ? #3321

Answered by daffl
noor-tg asked this question in Q&A
Discussion options

You must be logged in to vote

When using properties with that keyword in the query validator it needs to be added there as well.

const keywordUnique = {
  keyword: 'unique',
  type: 'string',
  async: true,
  validate: async (schema: any, data: any) => {
    try {
      let row = await app.get('dbclient').table(schema.table).where(schema.field, data).first()
      return row !== undefined
    } catch (e) {
      return true
    }
  },
  error: {
    message: 'out'
  }
} as const

export const dataValidator: Ajv = addFormats(new Ajv({}), formats)

export const queryValidator: Ajv = addFormats(
  new Ajv({
    coerceTypes: true
  }),
  formats
)

dataValidator.addKeyword(keywordUnique)
queryValidator.addKeyword(keywordU…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@noor-tg
Comment options

@daffl
Comment options

@noor-tg
Comment options

@noor-tg
Comment options

Answer selected by noor-tg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants