0.1.0 • Published 6 months ago

@ikasoba000/kensho-ts v0.1.0

Weekly downloads
-
License
-
Repository
-
Last release
6 months ago

about

kensho-ts is a small validator for TypeScript compatible with JSONSchema.

example

import {
  $object,
  $opt,
  $regexp,
  $string,
  createValidator,
  Infer,
} from "https://esm.sh/gh/ikasoba/kensho-ts@0.1.0/mod.ts";

const UserSchema = $object({
  id: $string,
  name: $regexp(/^[a-zA-Z0-9_-]+$/),
  displayName: $opt($string),
});
// {
//   type: "object",
//   properties: {
//     id: { type: "string" },
//     name: { type: "string", pattern: "^[a-zA-Z0-9_-]+$" },
//     displayName: { type: "string", required: false }
//   }
// }

type User = Infer<typeof UserSchema>;
// {
//   id: string;
//   name: string;
//   displayName?: string | undefined;
// }

// type User = Infer<typeof UserSchema, /* strict flag */ true>;
// // {
// //   id: string;
// //   name: ParsedString<RegExp>;
// //   displayName?: string | undefined;
// // }

const validateUser = createValidator(UserSchema);

validateUser({
  id: "abcdef1234567890",
  name: "ikasoba",
} as User);
0.1.0

6 months ago