2.0.9 • Published 1 year ago
@uinz/validate v2.0.9
TypeSafe Validate
install
yarn add @uinz/validate
example
import * as t from "@uinz/validate";
import { asserts } from "@uinz/validate"; // @see https://github.com/microsoft/TypeScript/issues/34596
const schema = t.object({
name: t.string(),
age: t.number(),
height: t.number().optional(),
});
declare let x: unknown;
// x is {type}
if (schema.validate(x)) {
console.log("my name is:", x.name.toUppercase());
// ^ x.name is string
console.log("I am", x.height?.toUppercase() ?? "x", "cm");
// ^ x.height is number | undefined
}
// asserts x is {type}
try {
asserts(schema, { age: 13 }, "user", true);
} catch (error) {
console.log(error.message); // TypeError: user.name not a string
}
API
name | desc |
---|---|
t.string | - |
t.string.optional | - |
t.number | - |
t.number.optional | - |
t.array(schema) | - |
t.array(schema).optional | - |
t.record(schema) | - |
t.record(schema).optional | - |
t.object(schema) | - |
t.object(schema).optional | - |
t.literal(literal) | - |
t.literal(literal).optional | - |
t.Function | - |
t.Function.optional | - |
t.Null | - |
t.Undefined | - |
t.or | - |
t.or.optional | - |
2.0.3
1 year ago
2.0.2
2 years ago
2.0.5
1 year ago
2.0.7
1 year ago
2.0.6
1 year ago
2.0.9
1 year ago
2.0.8
1 year ago
2.0.1
2 years ago
2.0.0
3 years ago
1.3.5
4 years ago
1.3.3
4 years ago
1.3.2
4 years ago
1.3.1
4 years ago
1.3.0
4 years ago
1.2.0
4 years ago
1.1.2
4 years ago
1.1.1
4 years ago
1.1.0
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago