primatejs / primate

Web framework focused on flexibility and developer freedom
https://primatejs.com
MIT License
211 stars 9 forks source link

create `@primate/schema` and deprecate `@primate/types` #144

Open terrablue opened 4 months ago

terrablue commented 4 months ago

General purpose validation module.

import schema from "@primate/schema";
import array from "@primate/schema/array";
import file from "@primate/schema/file";
import number from "@primate/schema/number";
import string from "@primate/schema/string";

// options:
// coerce: accept string values as valid by trying to coerce them first to target type
// strict: make sure all fields are present
const form = schema({
  id: number,
  title: string,
  tags: array.of(string).min(2),
  content: file,
}, { coerce: true, strict: false });

form.validate({ id: "1", title: "foobar" });
// -> success: returns validated fields, coerced to correct type if option
// `coerce` was set to true
// -> failure: throws, with validation errors
terrablue commented 3 months ago

renamed in a31b9a3, schema validation itself pending