在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:imbrn/v8n开源软件地址:https://github.com/imbrn/v8n开源编程语言:JavaScript 97.8%开源软件介绍:
The ultimate JavaScript validation library you've ever needed. Installation - Documentation - API Introducing v8n
Chainable APICreate validations very easily with its chainable API: v8n()
.string()
.minLength(5)
.first("H")
.last("o")
.test("Hello"); // true Incredibly fluentMix rules and modifiers together to create complex validations with great ease and fluency: v8n()
.array()
.every.number()
.not.some.negative()
.test([1, 2, -3]); // false - no negative please! So fluent that it looks like English: v8n()
.some.not.uppercase() // expects that some character is not uppercase
.test("Hello"); // true
v8n()
.not.some.uppercase() // expects that no character is uppercase
.test("Hello"); // false Notice how we made very different validation strategies just by changing the order of the modifiers. It's so intuitive that seems to be impossible, but this is v8n. CustomizableCreate your own custom validation rules in a very intuitive way: function foo() {
return value => value === "bar";
}
v8n.extend({ foo }); v8n will treat them like built-in ones: v8n()
.string()
.foo()
.test("bar"); // true ReusableExport validations just like you're used to do with your JavaScript modules: specialNumber.js import v8n from "v8n";
export default v8n()
.number()
.between(50, 100)
.not.even(); and use them anywhere you want: import specialNumber from "../specialNumber";
specialNumber.test(63); // true For any kind of dataUse v8n to validate your data regardless of its type. You can validate primitives, arrays, objects and whatever you want! You can also use them together! // numbers
v8n()
.number()
.between(5, 10)
.test(7); //true
// strings
v8n()
.string()
.minLength(3)
.test("foo"); // true
// arrays
v8n()
.array()
.every.even()
.test([2, 4, 6]); // true
// objects
const myData = { id: "fe03" };
v8n()
.schema({
id: v8n().string()
})
.test(myData); // true For any kind of validationDo simple validations with boolean based tests. Get more information about your validation process with exception based tests. And of course, perform asynchronous tests as well. All in one library. Boolean based validation:v8n()
.string()
.first("H")
.test("Hello"); // true Exception based validation:try {
v8n()
.string()
.first("b")
.check("foo");
} catch (ex) {
console.log(ex.rule.name); // first
} Getting all failures:const failed = v8n()
.string()
.minLength(3)
.testAll(10);
failed;
// [
// ValidationError { rule: { name: "string", ... } },
// ValidationError { rule: { name: "minLength", ... } }
// ] Async validation:If your validation strategy has some rule that performs time consuming validation, like a back-end check, you should use asynchronous validation: v8n()
.somAsyncRule()
.testAsync("foo") // returns a Promise
.then(result => {
/* valid! */
})
.catch(ex => {
/* invalid! */
}); ShareableShare your rules with the world, and use theirs as well. Create useful validation rules and share them with the open source community, and let people around the world validate without reinventing the wheel. Ready to use!There are a lot of built-in rules and modifiers for you to use already
implemented in Tiny!All these incredible features for just a few bytes: ArchitectureThe v8n core is composed of RulesRules are the heart of the v8n()
.string()
.minLength(3)
.test("Hello"); // true In this code snippet, we're using two rules (
ModifiersModifiers can be used to change rules meaning. For example, you can use the
v8n()
.not.equal(5)
.test(5); // false
Modifiers are very powerful. They work as decorators for rules. When used together, they allow you to build very complex validations. ContributeContributions of any kind are welcome! Read our CONTRIBUTING guide. License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论