{"id":18361771,"url":"https://github.com/jomik/cerberus","last_synced_at":"2025-10-17T19:52:50.990Z","repository":{"id":57195942,"uuid":"119505746","full_name":"Jomik/cerberus","owner":"Jomik","description":null,"archived":false,"fork":false,"pushed_at":"2020-06-01T01:01:38.000Z","size":2177,"stargazers_count":14,"open_issues_count":6,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-09-16T11:27:21.853Z","etag":null,"topics":["type-safety","typescript-library","validation-library"],"latest_commit_sha":null,"homepage":null,"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/Jomik.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":"2018-01-30T08:27:08.000Z","updated_at":"2023-09-12T13:56:12.000Z","dependencies_parsed_at":"2022-09-16T12:12:41.919Z","dependency_job_id":null,"html_url":"https://github.com/Jomik/cerberus","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jomik%2Fcerberus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jomik%2Fcerberus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jomik%2Fcerberus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jomik%2Fcerberus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jomik","download_url":"https://codeload.github.com/Jomik/cerberus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223255025,"owners_count":17114534,"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":["type-safety","typescript-library","validation-library"],"created_at":"2024-11-05T22:35:23.825Z","updated_at":"2025-10-17T19:52:45.935Z","avatar_url":"https://github.com/Jomik.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cerberus\n\n_The typescript object validator_ \\\nExperimental, work in progress.\n\n[![Build Status](https://travis-ci.org/Jomik/cerberus.svg?branch=master)](https://travis-ci.org/Jomik/cerberus)\n[![codecov](https://codecov.io/gh/jomik/cerberus/branch/master/graph/badge.svg)](https://codecov.io/gh/jomik/cerberus)\n[![dependencies Status](https://david-dm.org/jomik/cerberus/status.svg)](https://david-dm.org/jomik/cerberus)\n[![devDependencies Status](https://david-dm.org/jomik/cerberus/dev-status.svg)](https://david-dm.org/jomik/cerberus?type=dev)\n[![Greenkeeper badge](https://badges.greenkeeper.io/Jomik/cerberus.svg)](https://greenkeeper.io/)\n\nHere is a basic example.\n\n```ts\n// We can import the schema `types` to match the types of Typescript.\nimport { object, any, string, number } from \"cerberus\";\n// Create our schema\nconst schema = object({\n  a: string,\n  b: number,\n  c: any\n});\n// Get an object to test\nconst obj: any = { a: \"foo\", b: 42, c: \"bar\" };\n// Validate the object against the schema\nconst result = schema.validate(obj);\nif (result.valid) {\n  // A valid result gives us\n  // result.obj: { a: string, b: number, c: any }\n}\n```\n\n## Features\n\n- Validation against types and exact values\n- Correctly typed result object\n- Relevant constraints for each type, e.g. string.length\n- Optional and default values\n- Referencing values within objects\n- Asynchronously validate values with mapAsync\n\n## Installation\n\n```\nnpm install cerberus\n```\n\n## Examples\n\n```ts\nimport { object, array, string, integer } from \"cerberus\";\n\nconst person = object({ name: string, id: integer.positive() });\nconst schema = object({\n  ...person.schema,\n  mother: person,\n  father: person,\n  children: array(person)\n});\nconst result = schema.validate({\n  name: \"foo jr bar\",\n  id: 3,\n  mother: { name: \"baz buz bar\", id: 1 },\n  father: { name: \"foo bar\", id: 2 },\n  children: [{ name: \"bum bar\", id: 4 }, { name: \"baz bar\", id: 5 }]\n});\n```\n\n## API Documentation\n\n_Temporary_\n\n### Types\n\nThe below functions are exposed to create a validator.\n\n- `boolean: Validator\u003cboolean\u003e`\n- `number: Validator\u003cnumber\u003e`\n- `string: Validator\u003cstring\u003e`\n- `any: Validator\u003cany\u003e`\n- `integer: Validator\u003cnumber\u003e` - not a decimal\n- `nil: Validator\u003cnull\u003e`\n- `required: Validator\u003cany\u003e` - not undefined and not null\n- `forbidden: Validator\u003cundefined\u003e`\n- `array(Validator\u003cA\u003e): Validator\u003cA[]\u003e`\n- `object(Schema\u003cA\u003e): Validator\u003cA\u003e`\n\nA schema is an object where each property is a validator or a function from `A` to a validator.\n\n### Runners\n\nBelow are the two methods of running a validator. Both of these also have an async version, called by appending `Async`.\n\n#### `validate(Validator\u003cA\u003e, value): Result\u003cA\u003e`, `validateAsync(Validator\u003cA\u003e, value): Result\u003cA\u003e`\n\nResult contains an `info` property, which is an object containing whether the result is valid or not, and if it is valid, it holds the validated value, else it holds an error.\n\n`info: { valid: true; object: A } | { valid: false; error: ValidationError }`\n\n#### `assert(Validator\u003cA\u003e, value): A`, `assertAsync(Validator\u003cA\u003e, value): A`\n\nReturns the validated value, or throws an error if invalid.\n\n### Logical operators\n\nThe below functions are exposed for logical operations on validators.\nThey are also methods on the class, if you prefer chaining.\n\n#### `and(Validator\u003cA\u003e, Validator\u003cB\u003e): Validator\u003cA \u0026 B\u003e`\n\nShort circuits if the first is invalid.\n\n#### `or(Validator\u003cA\u003e, Validator\u003cB\u003e): Validator\u003cA | B\u003e`\n\nShort circuits if the first is valid.\n\n#### `xor(Validator\u003cA\u003e, Validator\u003cB\u003e): Validator\u003cA | B\u003e`\n\nIs valid if only one is valid, invalid in all other cases.\n\n### Methods\n\n#### `#default(a: A): Validator\u003cA\u003e`\n\nAllows the value to be undefined, and returns `a` in that case.\n\n#### `#optional\u003cA\u003e(): Validator\u003cA | undefined\u003e`\n\nAllows the value to be undefined.\n\n#### `#not(a: A): Validator\u003cA\u003e`\n\nEnsures that the value is not strictly equal to `a`.\n\n#### `#map(A -\u003e B): Validator\u003cB\u003e`, `mapAsync(A -\u003e Promise\u003cB\u003e): Validator\u003cB\u003e`\n\nMaps the result through the function if the validator returns valid.\n\n## Contribute\n\nPlease submit an issue with outlining your idea. If small, a pull request can be submitted immediately.\n\n- [Pull Requests](https://github.com/Jomik/cerberus/pulls)\n- [Issues](https://github.com/Jomik/cerberus/issues)\n\n## License\n\nThis project is licensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjomik%2Fcerberus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjomik%2Fcerberus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjomik%2Fcerberus/lists"}