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

166
Vistas
paquete de validación más rápida: cómo tener múltiples patrones y mensajes

Uso el validador más rápido para la validación. Quiero que el campo de contraseña contenga al menos un carácter y un número. Necesito un mensaje para validar al menos un carácter y un mensaje para validar un número. aquí está mi código:

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

0

Se puede hacer con un validador personalizado, consulte Validación personalizada para reglas integradas :

 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 az and AZ 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"} ));

Salida del node fv.js (así es como nombré el archivo con el código anterior):

 # 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 az and AZ 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 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