{"id":13781233,"url":"https://github.com/toksdotdev/adonis-class-validator","last_synced_at":"2026-01-16T05:29:54.119Z","repository":{"id":48373546,"uuid":"363881079","full_name":"toksdotdev/adonis-class-validator","owner":"toksdotdev","description":"🚧 Easily validate AdonisJS request data using a class decorated schema.","archived":false,"fork":false,"pushed_at":"2023-03-06T02:29:06.000Z","size":1944,"stargazers_count":10,"open_issues_count":8,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-01T13:01:58.718Z","etag":null,"topics":["adonis","decorator","validator-schema"],"latest_commit_sha":null,"homepage":"","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/toksdotdev.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}},"created_at":"2021-05-03T09:37:33.000Z","updated_at":"2023-08-29T13:29:42.000Z","dependencies_parsed_at":"2024-01-05T12:07:22.125Z","dependency_job_id":"a530e215-361d-489e-912e-63c698bb30c1","html_url":"https://github.com/toksdotdev/adonis-class-validator","commit_stats":null,"previous_names":["toksdotdev/adonis-class-validator","tnkemdilim/adonis-class-validator"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toksdotdev%2Fadonis-class-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toksdotdev%2Fadonis-class-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toksdotdev%2Fadonis-class-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toksdotdev%2Fadonis-class-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toksdotdev","download_url":"https://codeload.github.com/toksdotdev/adonis-class-validator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252954386,"owners_count":21830902,"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":["adonis","decorator","validator-schema"],"created_at":"2024-08-03T18:01:24.106Z","updated_at":"2026-01-16T05:29:54.085Z","avatar_url":"https://github.com/toksdotdev.png","language":"TypeScript","readme":"# Adonis Class Validator\n\nAdonis Class Validator provides a means to validate a request data using a class schema.\n\nOn successful validation, the data returned from validation is an instance of the class schema used to validate the request.\n\n## 🎁 Features\n\n- Convenient nesting of class rules.\n- Easy declaration of custom messages.\n- In-built caching of class schema.\n- Validate with existing V5 validator.\n- Support for all V5 validator features (`custom messages`, `creating custom rules`, `profiling`, `reporting` etc).\n\n## 📦 Installing\n\nSimply run the following commands on your shell\n\n```bash\nnpm install adonis-class-validator\nnode ace invoke adonis-class-validator\n```\n\n## 📌 Example\n\n\u003e We're making use of all the schemas and rules baked into Adonis. 😃\n\n```ts\n// SignupPayload.ts\nimport { validate, schema, rules } from \"@ioc:Adonis/ClassValidator\";\n\nclass SignupPayload {\n  @validate(schema.string({}, [rules.required(), rules.email()]), {\n    required: \"Field {{name}} is required.\",\n    email: \"Invalid email address\",\n  })\n  public email!: string;\n}\n```\n\n```ts\n// SignupController.ts\nimport { SignupPayload } from \"App/Validators\";\nimport { HttpContextContract } from \"@ioc:Adonis/Core/HttpContext\";\n\nclass SignupController {\n  public async index({ request }: HttpContextContract) {\n    const payload = await request.classValidate(SignupPayload);\n    console.log(payload instanceof SignupPayload); // true\n  }\n}\n```\n\n\u003e For more examples, check [here](./test/cases/classes.ts)\n\n## Standalone Validator\n\nIf you want to make use of the validator class schema to validate any form of data (outside the controller), you can easily rely on the standalone `ClassValidator.validate(...)` helper function.\n\n\u003e NOTE: If the validation fails, an instance of ValidationException is thrown.\n\n```ts\nimport {\n  ClassValidator,\n  ValidationException,\n} from \"@ioc:Adonis/ClassValidator\";\n\nasync function sendEmail() {\n  try {\n    const payload = await ClassValidator.validate(SignupPayload, {\n      email: \"hello@stallsone.com\",\n    });\n  } catch (err) {\n    // if validation error occurs, `err` is an instance of `ValidationException`\n  }\n}\n```\n\n## ⚓️ Going Deeper\n\nThere are currently 2 decorators supported for validation. They include:\n\n- `@validate()` : To validate primitive schemas such as `string`, `boolean`, `number`, `date`, `enum/enumSet`, `file`, `array([string|boolean|number|date|enum|file])`.\n- `@validate.nested()`: To nest class validator schemas through `array` and `object`.\n\n### Nested Validation\n\nTo nest a class validator schema, simply rely on the `@validate.nested()` decorator. It requires:\n\n- The `class validator schema` of the nested field.\n- A `callback` that whose:\n  - `parameter`: is a adonis member equivalent of the validator schema.\n  - `return type`: is the adonis schema to use to validate the nested field (which is either an `array` or `object`).\n- An `optional` custom message object.\n\n\u003e Custom messages also support interpolation e.g. `Field {{name}} is required`.\n\n```ts\nimport { validate } from \"@ioc:Adonis/ClassValidator\";\n\nclass Address {\n  @validate(schema.number([\n    rules.unique({ table: \"users\", column: \"email\" })\n  ]),\n  { unique: 'Field must be unique.' })\n  public id!: number;\n}\n\nclass User {\n  @validate.nested(\n    Address,\n    (address) =\u003e schema\n      .array([rules.minLength(2)])\n      .members(schema.object().members(address)),\n    { minLength: \"Field {{name}} must contain at least 2 addresses.\" }\n  )\n  public addresses!: Address[];\n```\n\n#### Custom Messages\n\nWhen `request.classValidate(...)` is called against the `User` schema [above](#nested-validation), the custom message generated and used for the failed validation will be:\n\n```json\n{\n  \"addresses.minLength\": \"Field addresses must contain at least 2 addresses.\",\n  \"addresses.*.unique\": \"Field must be unique.\"\n}\n```\n\n\u003e As far as the decorated field schema is a `schema.array()` with a `.members(...) of nested validation class`, it infers that it as the deep matching (`.*.`) patter matcher.\n\n### Empty Classes\n\nIf no property in a class was decorated with `validate()`, an empty data will be returned (where each field will be undefined).\n\n```ts\n// Notice there's no schema rule.\nclass UserPayload {\n  public firstname!: string;\n}\n\n// UserController.ts\nexport default class UsersController {\n  public async index({ request }: HttpContextContract) {\n    const data = await request.classValidate(UserPayload);\n\n    /**\n     * Payload wasn't validated because the class doesn't\n     * have a property decorated with a schema.\n     */\n    console.log(data instanceof SignupPayload); // true\n\n    /**\n     * Data is empty because no property has a validator schema decorator.\n     */\n    console.log(payload); // {}\n  }\n}\n```\n\n## 📝 Contributing\n\nIf you find any issue, bug or missing feature, please kindly create an issue or submit a pull request.\n\n## 🔖 License\n\nAdonis Class Validator is open-sourced software under MIT license.\n","funding_links":[],"categories":["Packages"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoksdotdev%2Fadonis-class-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoksdotdev%2Fadonis-class-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoksdotdev%2Fadonis-class-validator/lists"}