{"id":25627607,"url":"https://github.com/jeppech/schema-ts","last_synced_at":"2025-04-14T14:09:10.485Z","repository":{"id":264704785,"uuid":"894170609","full_name":"jeppech/schema-ts","owner":"jeppech","description":"Validate some unknown data, with a few functions","archived":false,"fork":false,"pushed_at":"2025-02-12T12:52:40.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T12:56:55.514Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jeppech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-25T22:01:57.000Z","updated_at":"2025-02-12T12:52:44.000Z","dependencies_parsed_at":"2024-11-25T22:12:45.049Z","dependency_job_id":"88ef7466-472c-41d3-b741-6c78d3b9347b","html_url":"https://github.com/jeppech/schema-ts","commit_stats":null,"previous_names":["jeppech/validate-ts","jeppech/schema-ts"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeppech%2Fschema-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeppech%2Fschema-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeppech%2Fschema-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeppech%2Fschema-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeppech","download_url":"https://codeload.github.com/jeppech/schema-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240214592,"owners_count":19766264,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-22T17:53:32.968Z","updated_at":"2025-02-22T17:53:33.611Z","avatar_url":"https://github.com/jeppech.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Validate unknown data\n[![npm package version](https://img.shields.io/npm/v/@jeppech/schema-ts)](https://npmjs.com/package/@jeppech/schema-ts)\n\nSmall library for creating schemas, that can validate and assert unknown data.\n\n\u003e someone: Why not use valibot  \n\u003e me: We have valibot at home...  \n\u003e valibot at home:\n\n## Install\n```sh\npnpm add @jeppech/schema-ts\n```\n\n## Usage\n\n```ts\nimport * as v from '@jeppech/schema-ts'\n\nconst userdata = {\n  username: v.as(v.string()),\n  age: v.as(v.number()),\n  email: v.as(v.string(), v.email()),\n  created_at: v.as(v.timestamp()),\n  deleted: v.as(v.optional(v.timestamp())),\n  have_you_heard_about_our_extended_warranty: v.as(v.bool())\n}\n\ntype User = v.InferObject\u003ctypeof userdata\u003e\n/**\n * The `User` type will have the following shape, and\n * will following any changes made to the object above.\n * \n * type User = {\n *   username: string;\n *   age: number;\n *   email: string;\n *   created_at: Date;\n *   deleted: Option\u003cDate\u003e;\n *   have_you_heard_about_our_extended_warranty: boolean;\n * } \n */\n\nconst form = new FormData() // from a request, eg. `await req.formData()`\n\nconst result = v.parse_formdata(form, userdata)\n\nif (result.is_err()) {\n  // Contains a list of errors, WIP\n  console.log(result.unwrap_err())\n} else {\n  // Returns the `User` object\n  const user = result.unwrap()\n}\n```\n\n# Extending\nYou can add your own `Valuers` and `Validators`, they are just simple functions.\n\n## Valuer\nA `Valuer` is a function, that is passed as the first argument to the `v.as(...)` function.\nThe Valuers job, is to assert, that the input value is of the type that we want, and return that type.\nIf this assertion fails, it must throw a `ValidationError`.\n\nHere's an example of a Valuer, that requires the property to be either `admin`, `user` or `anonymous`\n```ts\nconst roles = ['admin', 'user', 'anonymous'] as const;\ntype UserRole = typeof roles[number]\n\nexport function role(err = 'expected a valid role') {\n  return (value: unknown, field: string) =\u003e {\n    if (typeof value === 'string') {\n      if (roles.includes(value as UserRole)) {\n        return value as UserRole;\n      }\n    }\n    throw new ValidationError(err, value, field);\n  };\n}\n\nconst user = {\n  name: v.as(v.string()),\n  role: v.as(v.role())\n}\n\ntype UserWithRole = v.InferObject\u003ctypeof user\u003e\n```\nSee [src/valuers.ts](src/valuers.ts) for more examples\n\n\n## Validator\nA `Validator` is a function, that is passed as any other argument, besides the first, to the `v.as(...)` function.\n\nA Validators job is, as the name implies, to validate the input data. If a validation succeeds it must return `void`/`undefined`. If it fails, it must return a ValidationError.\n\nAs a Validator comes __after__ a Valuer, we can expect an exact type as input data, for the function.\n\nHere's an example of a Validator, that requires a timestamp to be in the future\n```ts\nfunction in_the_future(err = 'expected a timestamp in the future') {\n  return (value: Date, field: string) =\u003e {\n    const now = new Date();\n    if (value \u003c now) {\n      return new ValidationError(err, value, field);\n    }\n  };\n}\n\nconst notification = {\n  message: v.as(v.string()),\n  fire_at: v.as(v.timestamp(), in_the_future())\n}\n\ntype NotifyInFuture = v.InferObject\u003ctypeof notification\u003e\n```\nSee [src/validators.ts](src/validators.ts) for more examples\n\n# Acknowledgement\n[Valibot](https://github.com/fabian-hiller/valibot)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeppech%2Fschema-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeppech%2Fschema-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeppech%2Fschema-ts/lists"}