Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
How to set up a custom AJV keyword?

I'm using ajv and want to add a custom validator inspecting a given object. This custom validator should either return true or fail with the correct error message. Based on

  • (deprecated syntax) AJV custom keyword validation
  • https://ajv.js.org/keywords.html#define-keyword-with-code-generation-function

I started with the following sample code

const Ajv = require("ajv");
const ajv = new Ajv({ allErrors: true });

ajv.addKeyword({
  keyword: "eachPropIsTrue",
  type: "object",
  schemaType: "boolean",
  code(context) {
    const { data } = context;
    const eachPropIsTrue = Object.values(data).every(value => !!value);

    context.fail(!eachPropIsTrue);
  },
})

const schema = {
  type: "object",
  eachPropIsTrue: true,
  properties: {
    a: { type: "boolean" }
  },
  required: [ "a" ],
  additionalProperties: false
};

const validate = ajv.compile(schema);
console.log(validate({ a: true })); // true
console.log(validate({ a: false })); // true but should be false!
console.log(validate({ })); // false

Unfortunately I'm facing two problems:

  • The second validation does not fail although I would expect it to fail
  • Where do I have to pass in information about the constructed error message?

When inspecting the data variable inside the code function I can see that it does not contain the current object. So what is the correct way to set up a custom keyword?

I'm looking for a sample function like

validate(theObject): boolean | string => {
  const anyPropIsFalse = Object.values(data).some(value => value === false);

  if(anyPropIsFalse) {
    return 'Any prop is false!';
  }

  return true;
};

or maybe a function returning void but using context.fail('Any prop is false!');


Sidenote:

My real world usecase scenario is an object with two properties where one is an object with an id and the other property is an array holding objects with ids. I have to make sure that all ids from both properties are completely unique.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This makes the example to work as intended.

ajv.addKeyword({
  keyword:    "eachPropIsTrue",
  type:       "object",
  schemaType: "boolean",
  compile:    () => data => Object.values(data).every(value => !!value)
});

Anyway I strongly suggest to better check ajv user-defined keywords documentation.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda