Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda