0.4.2 • Published 2 months ago
balid v0.4.2
Lightweight Schema Validator
Balid is a light weight schema validator.
:warning: Not Production ready
Todos
- Basic Types
- any
- boolean
- number
- string
- Advanced Types
- Objects
- Arrays
- Advanced Errors
- Error Messages
- Custom Error Messages
Utility Functions (e.g: max, min)
- min
- max
Github Actions
- publish version when
main
branch changes - automated tests
- publish version when
Installation
pnpm add balid
# or
npm install balid
# or
yarn install balid
Examples
Any
import { b } from "balid";
const schema = b.any();
schema.validate(200); // returns { valid: true }
schema.validate({ foo: "bar" }); // returns { valid: true }
Boolean:
import { b } from "balid";
const schema = b.boolean();
schema.validate(false); // returns { valid: true }
schema.validate("false"); // returns { valid: false }
String:
import { b } from "balid";
const schema = b.string();
schema.validate("Hello World"); // returns { valid: true }
const minMaxSchema = b.string({ min: 2, max: 10 });
schema.validate("a"); // returns { valid: false }
schema.validate("hello"); // returns { valid: true }
const matchSchema = b.string({ match: /hello/ });
matchSchema.validate("hello"); // returns { valid: true }
matchSchema.validate("world"); // returns { valid: false }
Number:
import { b } from "balid";
const schema = b.number();
schema.validate(123); // returns { valid: true }
schema.validate("123"); // returns { valid: false }
const minMaxSchema = b.number({ min: 10, max: 20 });
schema.validate(1); // returns { valid: false }
schema.validate(25); // returns { valid: false }
schema.validate(15); // returns { valid: true }
Object:
import { b } from "balid";
const schema = b.object({
name: b.string(),
});
schema.validate({ name: "John Doe" }); // returns { valid: true }
Array:
import { b } from "balid";
const schema = b.array(name: b.string());
schema.validate(["John Doe"]); // returns { valid: true }
And
import { b } from "balid";
const schema = b.and({/* OPTIONS */}, b.any(), b.boolean());
schema.validate(true); // returns { valid: true }
Or
import { b } from "balid";
const schema = b.or({/* OPTIONS */}, b.string(), b.boolean());
schema.validate("hello world"); // returns { valid: true }
schema.validate(true); // returns { valid: true }
Optional
import { b } from "balid";
const schema = b.optional(b.string());
schema.validate("hello world"); // returns { valid: true }
schema.validate(); // returns { valid: true }