我想要 ruby on rails 和 cakephp 样式验证。我知道这是我会反复使用的东西,所以我制作了这个快速的 npm 模块:https://npmjs.org/package/iz
它在语义上读起来很像茉莉花,可以在客户端或服务器端使用。这意味着它支持 commonjs 和 amd 以及通过 JSON 传入的验证规则。
它经过了很好的单元测试,没有生产依赖项,并且范围仅限于验证。我们现在似乎有一个小社区。欢迎提出想法、反馈和拉取请求。
当前库函数:
iz.alphaNumeric(*); // Is number or string(contains only numbers or strings)
iz.between(number, start, end); // Number is start or greater but less than or equal to end, all params numeric
iz.blank(*); // Empty string, undefined or null
iz.boolean(*); // true, false, 0, 1
iz.cc(*); // Luhn checksum approved value
iz.date(*); // Is a data obj or is a string that is easily converted to a date
iz.decimal(*); // Contains 1 decimal point and potentially can have a - at the beginning
iz.email(*); // Seems like a valid email address
iz.extension(ob1, ob2); // If obj2's methods are all found in obj1
iz.fileExtension(arr, value); // Checks if the extension of value is in arr. An obj can be provide, but must have indexOf defined.
iz.fileExtensionAudio(value); // Check against mp3, ogg, wav, aac
iz.fileExtensionImage(value); // Check against png, jpg, jpeg, gif, bmp, svg, gif
iz.inArray(arr, value); // If * is in the array
iz.int(*, bool (optional)); // Is an int. If the 2nd variable is true (false by default) a decimal is allowed
iz.ip(str); // str resembles an IPV4 or IPV6 address
iz.minLen(val, min); // val (str or arr) is greater than min
iz.maxLen(val, max); // val (str or arr) is shorter than max
iz.multiple(num, mult); // Number is multiple of another number
iz.number(*); // Is either an int or decimal
iz.ofType(obj, typeName); // If it is a named object, and the name matches the string
iz.phone(str, canHaveExtension?); // Is an american phone number. Any punctuations are allowed.
iz.postal(*); // Is a postal code or zip code
iz.ssn(*); // Is a social security number