Validation
yamiteru opened this issue · comments
Yamiteru commented
- Type
type TInt = Type<"int", number>;
- Assert
const int: Assert<TInt> = (v) => {
assertType(v, "number");
assertInt(v);
};
- Composition
const person = object({
name: string(),
age: int(min(18), max(99)),
friends: nullable(array(person))
});
// error since number is not assignable to type person
person(1);
- Value
const $int = value(int);
const double = $int(0, (v) => v * 2);
const doubleUSD = $string("0 USD", (v, { previous }) => {
const _double = $(double);
if(v === previous.input) return undefined;
return `${_double} USD`;
});
- Validate
// valid = false, data = undefined, error = "value 12.5 is not int"
const { valid, data, error } = validate(int, 12.5);
- Composition
const age = int(
min(0),
max(99)
);
const zip = string(
length(5)
);
const email = string(
min(8),
regex(emailRegex)
);