简体   繁体   English

特定值或特定值列表的 Joi 验证正则表达式

[英]Joi validation regex for a specific value or a list of specific values

I am converting CSV to JSON and I need to validate a cell value.我正在将 CSV 转换为 JSON 并且我需要验证单元格值。 Having a list of possible valid values ['abc', 'def', 'ref', 'rop'] .有一个可能的有效值列表['abc', 'def', 'ref', 'rop'] I want to write a regex assuming a valid cell would be either我想写一个正则表达式,假设一个有效的单元格是

  1. one value from the above list (eg validValue='abc' notValidValue='adr' ) or上述列表中的一个值(例如validValue='abc' notValidValue='adr' )或
  2. multiple values from the above list separated by \\\ (eg validValue='abc \\\ ref \\\ def' notValidValue='aef \\\ kop' )上面列表中的多个值由\\\分隔(例如validValue='abc \\\ ref \\\ def' notValidValue='aef \\\ kop'

I tried with Joi.string().regex(/(?:abc|def|ref|rop)(/s\\\\\\/s(?:abc|def|ref|rop))?/) but it throws eeror for first /s -> invalid character and also Argument of type RegExp is not assignable to parameter of type 'string | StringRegexOptions'....我试过Joi.string().regex(/(?:abc|def|ref|rop)(/s\\\\\\/s(?:abc|def|ref|rop))?/)但是它会为第一个/s -> 无效字符抛出错误,并且Argument of type RegExp is not assignable to parameter of type 'string | StringRegexOptions'.... Argument of type RegExp is not assignable to parameter of type 'string | StringRegexOptions'....

You have typo.你有错字。 The /s should be \s . /s应该是\s And the tail ?尾巴? will match the second part 0 or once.将匹配第二部分 0 或一次。 Change it to * can match as many time as it will.将其更改为*可以匹配任意多次。 or you want to specify the maximum {0,3} .或者您想指定最大值{0,3}

regex(/(?:abc|def|ref|rop)(\s\\\\\\\s(?:abc|def|ref|rop))*/)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM