Juniper Validator
Validate data against a Juniper schema, and narrow it to the schema's type.
ts
import { objectSchema, stringSchema } from 'juniper';
import { makeValidator } from 'juniper-validator';
const userSchema = objectSchema({
properties: {
name: stringSchema({ minLength: 1 }),
email: stringSchema({ format: 'email' }),
},
required: ['name'],
additionalProperties: false,
});
const validator = makeValidator(userSchema);
if (validator.is(input)) {
console.log(input);}Features
- Typed by the schema:
is()andassert()narrowunknownto the schema's TypeScript type, with no type to write by hand. - Standard Schema: Works with any library that accepts Standard Schema validators, such as form libraries and API frameworks. See Standard Schema.
- Strict Ajv underneath: Compiles with Ajv in strict mode, and validates standard formats like
email,uuidanddate-time. - Any JSON Schema: Takes a Juniper schema, or a rendered JSON Schema object.