Skip to content

v2.2.0 Schema validations like in zod

Compare
Choose a tag to compare
@numfin numfin released this 29 Apr 20:32
· 43 commits to main since this release

Implemented and tested Schema module for validating external data

Example

const s = Schema.num();
const v: Result<number> = s.parse(...);

// Parse unknown value to union
const innerUnion = Schema.union({
  v3v1: Schema.arr(Schema.num()),
  v3v2: Schema.str()
});
const tUnion = Schema.union({
  v1: Schema.num(),
  v2: Schema.str(),
  v3: innerUnion
});
const parsedValue: UnionInstance<...> = tUnion.parse(...);
// Or create union instance yourself
const v1 = tUnion.v1(10);
const v2 = tUnion.v2("hi");
const v3v1 = tUnion.v3(innerUnion.v3v1([42]));
const v3v2 = tUnion.v3(innerUnion.v3v2("qwerty"));

const v = v1.match({
  v1: (v) => 1, // all returns must have same type
  v2: (v) => 2,
  v3: (v) => 3,
}); // 1
const v = v2.matchSome({
  v2: (v) => 2
}); // Some(2)

const isV3 = v3.is("v3") // true