多个 regex/pattern 验证 Joi Hapi

multiple regex/pattern validation Joi Hapi

大家好,我想用 joi/hapi 进行多重正则表达式验证,但我不知道该怎么做我已经多次尝试但没有人成功,我希望密码至少有 1 个大写字母, 1 个减号字母、1 个数字和 1 个特殊字符,这是我的尝试:

const passwordValidation = (data) => {
  const schema = Joi.object({
    password: Joi.string()
      .pattern(new RegExp('^[a-z]{1,}$'))
      .pattern(new RegExp('^[A- Z]{1,}$'))
      .pattern(new RegExp('^[0 - 9]{1,}'))
      .pattern(new RegExp('^[!@#$%&*]{1,}'))
      .min(8)
      .required()
  });
  return schema.validate(data);
};

const passwordValidation = (data) => {
  const schema = Joi.object({
    password: Joi.string()
      .regex('^[a-z]{1,}$')
      .regex('^[A- Z]{1,}$')
      .regex('^[0 - 9]{1,}')
      .regex('^[!@#$%&*]{1,}')
      .min(8)
      .required()
  });
  return schema.validate(data);
};

我该怎么做?

regex() 要求对象是 RegExp 对象。

并在正则表达式中修复

https://javascript.info/regexp-anchors

工作演示

https://stackblitz.com/edit/js-7c78r7?file=index.js