{"id":22731335,"url":"https://github.com/sno2/jason","last_synced_at":"2025-04-14T00:35:59.666Z","repository":{"id":46113118,"uuid":"425349819","full_name":"sno2/jason","owner":"sno2","description":"A composable validation library for TypeScript and JavaScript with amazing type support.","archived":false,"fork":false,"pushed_at":"2021-11-13T16:42:20.000Z","size":33,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T14:51:50.997Z","etag":null,"topics":["data-validation","deno","deno-module","node","typescript"],"latest_commit_sha":null,"homepage":"https://deno.land/x/jason","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sno2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-06T21:13:48.000Z","updated_at":"2024-09-27T17:16:28.000Z","dependencies_parsed_at":"2022-09-26T21:31:30.209Z","dependency_job_id":null,"html_url":"https://github.com/sno2/jason","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sno2%2Fjason","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sno2%2Fjason/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sno2%2Fjason/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sno2%2Fjason/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sno2","download_url":"https://codeload.github.com/sno2/jason/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248802696,"owners_count":21163903,"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":["data-validation","deno","deno-module","node","typescript"],"created_at":"2024-12-10T19:21:35.500Z","updated_at":"2025-04-14T00:35:59.624Z","avatar_url":"https://github.com/sno2.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jason\n\nA Deno module for building composable validators for your JavaScript data with\nfluid TypeScript support.\n\n## Usage\n\nTo use `jason`, simply import it from `deno.land`. We **strongly advise** to\nimport the module under the namespace of `jason` to avoid conflicts with some\nTypeScript types and our methods.\n\n```ts\nimport * as jason from \"https://deno.land/x/jason/mod.ts\";\n```\n\n## Examples\n\nFind all examples in the [`examples` directory](./examples).\n\n## Guides\n\n### User Object\n\nFirst, let's look at the format of the data we are going to be parsing:\n\n```jsonc\n{\n  \"id\": \"user-0128432\", // must begin with 'user-'\n  \"username\": \"theskyisblue\", // 4 - 16 characters\n  \"age\": 23 // must be at least '0'\n}\n```\n\nNow, let's build out a validator:\n\n```ts\nconst userSchema = jason.object({\n  id: jason.string({ startsWith: \"user-\" }),\n  username: jason.string({ length: { min: 4, max: 16 } }), // these are inclusive\n  age: jason.number({ min: 0 }),\n});\n```\n\nAfter that, you might want to be able to create a TypeScript interface/type that\ndescribes the shape of the `User` object. With `jason`, it's extremely simple to\ndo by just importing our utility `GetJasonType` type:\n\n```ts\nimport type { GetJasonType } from \"https://deno.land/x/jason/mod.ts\";\n\n// ~~~ user schema definition ~~~\n\ntype User = GetJasonType\u003ctypeof userSchema\u003e;\n```\n\nNow, we you get the type information for `User`, it should look like this:\n\n```ts\ntype User = {\n  id: string;\n  username: string;\n  age: number;\n} \u0026 {}; // the `\u0026 {}` is from a TypeScript trick used to create optional properties\n```\n\nAnd now, let's finally validate our original data with our schema definition:\n\n```ts\nuserSchema\n  .validate({\n    id: \"user-0128432\",\n    username: \"4to16characters\",\n    age: 23,\n  })\n  .tryThrowErrors();\n```\n\nAfter you run that, you will see that we get no errors! However, now let's test\nwith some incorrect data:\n\n```ts\nuserSchema\n  .validate({\n    id: \"2353\",\n    username: \"asd\",\n    age: -3,\n  })\n  .tryThrowErrors();\n```\n\nAfter that, you'll see the following messages:\n\n```\n'id': '2353' did not start with 'user-'\n\n'username': 'asd' had a length less than the minimum length of '4'\n\n'age': '-3' is not greater than or equal to '0'\n\nerror: Uncaught TypeError: Failed to validate data. There is most likely more information above.\n```\n\n## FAQ\n\n### How do I have optional properties?\n\nAll you need to do is wrap the given property with the `jason.optional`\ncomposable:\n\n```ts\nconst schema = jason.object({\n  name: jason.string(),\n  friends: jason.optional(jason.array(jason.string())),\n});\n\nschema\n  .validate({\n    name: \"Jon Doe\",\n    friends: [\"Jane Doe\", \"Billy Bob\"],\n  })\n  .tryThrowErrors();\n\nschema\n  .validate({\n    name: \"Jon Doe\",\n  })\n  .tryThrowErrors();\n```\n\n### Am I restricted to having objects as the root in the schema?\n\nNo! Jason includes no magic. Therefore, you can use all of our utility types by\nthemselves for matching primitive and other types (i.e. `string`).\n\n```ts\nconst schema = jason.string({ length: { min: 6, max: 14 } });\n\nschema.validate(\"123456\").tryThrowErrors(); // no errors\nschema.validate(\"1234\").tryThrowErrors(); // too short - error\n```\n\n### I am getting type errors when passing in parameters into `Validator.validate`. What am I doing wrong?\n\nThe `Validator.validate` method is actually type-checked to only take in values\nthat match the TypeScript version of your schema. This is to help by giving\neditor hints on properties and also help you find errors when you are validating\ndata that would never pass validation. However, you can always just cast your\nvalue to `any` if you want to forgo type-checking.\n\n### How do I use it with JSON?\n\nAll you need to do is first parse the JSON with `JSON.parse()` (or just pass in\n`Response#json` if you're using the Fetch API) into the `validate` method of\nyour schema:\n\n```ts\nconst schema = jason.labelled(\n  \"User\",\n  jason.object({\n    name: jason.string(),\n    age: jason.number({ min: 0 }),\n  }),\n);\n\nconst body = '{ \"name\": \"awesomeguy23\", \"age\": 23 }';\nconst data = JSON.parse(body);\n\nschema.validate(data).tryThrowErrors();\n```\n\n## Contributing\n\nPlease format your code using `deno fmt` and make sure you run `deno test`\nbefore sending a pull request.\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsno2%2Fjason","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsno2%2Fjason","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsno2%2Fjason/lists"}