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

163
Visualizações
fastest-validator package: How to have multiple pattern and message

I use fastest-validator for validation. I want password field to contain at least one character and one number. I need a message for validating at least one character and a message for validating one number. here is my code:

const Validator = require("fastest-validator");

const v = new Validator();

const registerSchema = {
    email: { type: "email"},
    pass: { type: "string",pattern:'/.*[0-9].*/', min: 8, max: 20 },
};
//also I need a pattern to check at least one char is used and its message specific for it.

exports.validateRegister = v.compile(registerSchema);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It can be done with a custom validator, see Custom validation for built-in rules:

const Validator = require("fastest-validator");
const v = new Validator({
    useNewCustomCheckerFunction: true, // using new version
    messages: {
        // Register our new error message text
        atLeastOneLetter: "The pass value must contain at least one letter from a-z and A-Z ranges!",
        atLeastOneDigit: "The pass value must contain at least one digit from 0 to 9!"
    }
});

const schema = {
    email: {type:"email"},
    pass: {
        type: "string",
        custom: (v, errors) => {
            if (!/[0-9]/.test(v)) errors.push({ type: "atLeastOneDigit" });
            if (!/[a-zA-Z]/.test(v)) errors.push({ type: "atLeastOneLetter" });
            return v;
        },
        min:8,
        max:20,
        messages: {
            stringPattern: "pass value must contain a digit",
            stringMin: "Your pass value is too short",
            stringMax: "Your pass value is too large",
        }
    }
}
const check = v.compile(schema);

console.log( check( {email:"abc@def.com", pass: "JohnABCD"} ));
console.log( check( {email:"abc@def.com", pass: "123456789"} ));
console.log( check( {email:"abc@def.com", pass: "12A"} ));
console.log( check( {email:"abc@def.com", pass: "12A12A12A12A12A12A12A12A12A12A"} ));

Output of node fv.js (this is how I named the file with the above code):

# node fv.js
[ { message:
     'The pass value must contain at least one digit from 0 to 9!',
    field: 'pass',
    type: 'atLeastOneDigit' } ]
[ { message:
     'The pass value must contain at least one letter from a-z and A-Z ranges!',
    field: 'pass',
    type: 'atLeastOneLetter' } ]
[ { type: 'stringMin',
    message: 'Your pass value is too short',
    field: 'pass',
    expected: 8,
    actual: 3 } ]
[ { type: 'stringMax',
    message: 'Your pass value is too large',
    field: 'pass',
    expected: 20,
    actual: 30 } ]
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