{"id":15022481,"url":"https://github.com/apideck-libraries/better-ajv-errors","last_synced_at":"2025-05-16T03:06:14.588Z","repository":{"id":39802420,"uuid":"367330191","full_name":"apideck-libraries/better-ajv-errors","owner":"apideck-libraries","description":"Human-friendly JSON Schema validation for APIs 👮‍♀️","archived":false,"fork":false,"pushed_at":"2025-02-03T12:22:22.000Z","size":1835,"stargazers_count":43,"open_issues_count":5,"forks_count":7,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-08T15:04:37.192Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/apideck-libraries.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-14T10:37:51.000Z","updated_at":"2025-04-22T21:56:28.000Z","dependencies_parsed_at":"2024-06-18T14:02:33.090Z","dependency_job_id":"29905d58-0b2d-4dce-836d-42a8775b767c","html_url":"https://github.com/apideck-libraries/better-ajv-errors","commit_stats":{"total_commits":49,"total_committers":7,"mean_commits":7.0,"dds":"0.20408163265306123","last_synced_commit":"026206038919c1fb73b4e8ef258a2e4a01813c4a"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apideck-libraries%2Fbetter-ajv-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apideck-libraries%2Fbetter-ajv-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apideck-libraries%2Fbetter-ajv-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apideck-libraries%2Fbetter-ajv-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apideck-libraries","download_url":"https://codeload.github.com/apideck-libraries/better-ajv-errors/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254459088,"owners_count":22074605,"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":"2024-09-24T19:58:00.744Z","updated_at":"2025-05-16T03:06:09.572Z","avatar_url":"https://github.com/apideck-libraries.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm (scoped)](https://img.shields.io/npm/v/@apideck/better-ajv-errors?color=brightgreen)](https://npmjs.com/@apideck/better-ajv-errors) [![npm](https://img.shields.io/npm/dm/@apideck/better-ajv-errors)](https://npmjs.com/@apideck/better-ajv-errors) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/apideck-libraries/better-ajv-errors/CI)](https://github.com/apideck-libraries/better-ajv-errors/actions/workflows/main.yml?query=branch%3Amain++)\n\n# @apideck/better-ajv-errors 👮‍♀️\n\n\u003e Human-friendly JSON Schema validation for APIs\n\n\n- Readable and helpful [ajv](https://github.com/ajv-validator/ajv) errors\n- API-friendly format\n- Suggestions for spelling mistakes\n- Minimal footprint: 1.56 kB (gzip + minified)\n\n![better-ajv-errors output Example](https://user-images.githubusercontent.com/8850410/118274790-e0529e80-b4c5-11eb-8188-9097c8064c61.png)\n\n## Install\n\n```bash\n$ yarn add @apideck/better-ajv-errors\n```\n\nor\n\n```bash\n$ npm i @apideck/better-ajv-errors\n```\n\nAlso make sure that you've installed [ajv](https://www.npmjs.com/package/ajv) at version 8 or higher.\n\n## Usage\n\nAfter validating some data with ajv, pass the errors to `betterAjvErrors`\n\n```ts\nimport Ajv from 'ajv';\nimport { betterAjvErrors } from '@apideck/better-ajv-errors';\n\n// Without allErrors: true, ajv will only return the first error\nconst ajv = new Ajv({ allErrors: true });\n\nconst valid = ajv.validate(schema, data);\n\nif (!valid) {\n  const betterErrors = betterAjvErrors({ schema, data, errors: ajv.errors });\n}\n```\n\n## API\n\n### betterAjvErrors\n\nFunction that formats ajv validation errors in a human-friendly format.\n\n#### Parameters\n\n- `options: BetterAjvErrorsOptions`\n  - `errors: ErrorObject[] | null | undefined` Your ajv errors, you will find these in the `errors` property of your ajv instance (`ErrorObject` is a type from the ajv package).\n  - `data: Object` The data you passed to ajv to be validated.\n  - `schema: JSONSchema` The schema you passed to ajv to validate against.\n  - `basePath?: string` An optional base path to prefix paths returned by `betterAjvErrors`. For example, in APIs, it could be useful to use `'{requestBody}'` or `'{queryParemeters}'` as a basePath. This will make it clear to users where exactly the error occurred.\n\n#### Return Value\n\n- `ValidationError[]` Array of formatted errors (properties of `ValidationError` below)\n  - `message: string` Formatted error message\n  - `suggestion?: string` Optional suggestion based on provided data and schema\n  - `path: string` Object path where the error occurred (example: `.foo.bar.0.quz`)\n  - `context: { errorType: DefinedError['keyword']; [additionalContext: string]: unknown }` `errorType` is `error.keyword` proxied from `ajv`. `errorType` can be used as a key for i18n if needed. There might be additional properties on context, based on the type of error.\n\n## Related\n\n- [atlassian/better-ajv-errors](https://github.com/atlassian/better-ajv-errors) was the inspiration for this library. Atlassian's library is more focused on CLI errors, this library is focused on developer-friendly API error messages.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapideck-libraries%2Fbetter-ajv-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapideck-libraries%2Fbetter-ajv-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapideck-libraries%2Fbetter-ajv-errors/lists"}