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

139
Visualizações
Ajv: how to make a property nullable conditionally depending on other property

I have an object that can be in one of 4 states:

  1. {"A": "something", "B": [{"C": "D"}]}
  2. {"A": "", "B": [{"C": "D"}]}
  3. {"A": "something", "B": null}
  4. {"A": "", "B": null}

In my Ajv schema validation I want to make the property "B" nullable: true only if the property "A" is not an empty string.

How can I achieve this?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

I hope the below helpers function helps you. You can make changes in assignment value as you need

function mappNullable(obj, nonNullable, nullableProperty) { 
   if(obj[nonNullable] != '') { 
      obj[nullableProperty] = {nullable: true};
   }
   return;
}
const t1 = {"A": "something", "B": [{"C": "D"}]};
const t2 = {"A": "", "B": [{"C": "D"}]};
const t3 = {"A": "something", "B": null};
const t4 = {"A": "", "B": null};
mappNullable(t1, "A", "B");
mappNullable(t2, "A", "B");
mappNullable(t3, "A", "B");
mappNullable(t4, "A", "B");
console.log(t1);  // {"A": "something", "B": {nullable: true}};
console.log(t2); // {"A": "", "B": [{"C": "D"}]};
console.log(t3); // {"A": "something", "B": {nullable: true}};
console.log(t4); //  {"A": "", "B": null};

about 4 years ago · Santiago Gelvez 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