{"id":22382392,"url":"https://github.com/jcoreio/sequelize-validate-subfields","last_synced_at":"2025-03-26T19:40:48.756Z","repository":{"id":40707457,"uuid":"117783989","full_name":"jcoreio/sequelize-validate-subfields","owner":"jcoreio","description":"simple framework for validating subfields of JSON attributes of Sequelize models","archived":false,"fork":false,"pushed_at":"2023-11-28T19:04:26.000Z","size":3639,"stargazers_count":0,"open_issues_count":18,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-23T15:41:06.673Z","etag":null,"topics":["es2015"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/jcoreio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2018-01-17T04:29:21.000Z","updated_at":"2023-11-28T19:03:36.000Z","dependencies_parsed_at":"2024-10-24T10:48:17.038Z","dependency_job_id":"5b1ddf59-23a2-441f-8e13-5cc6e04ac663","html_url":"https://github.com/jcoreio/sequelize-validate-subfields","commit_stats":{"total_commits":217,"total_committers":4,"mean_commits":54.25,"dds":"0.18433179723502302","last_synced_commit":"68c0568805c42c3f76d6a589d98e8fa04e701632"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-validate-subfields","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-validate-subfields/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-validate-subfields/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-validate-subfields/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcoreio","download_url":"https://codeload.github.com/jcoreio/sequelize-validate-subfields/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245726710,"owners_count":20662544,"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":["es2015"],"created_at":"2024-12-05T00:12:49.951Z","updated_at":"2025-03-26T19:40:48.732Z","avatar_url":"https://github.com/jcoreio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sequelize-validate-subfields\n\n[![CircleCI](https://circleci.com/gh/jcoreio/sequelize-validate-subfields.svg?style=svg)](https://circleci.com/gh/jcoreio/sequelize-validate-subfields)\n[![Coverage Status](https://codecov.io/gh/jcoreio/sequelize-validate-subfields/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/sequelize-validate-subfields)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![npm version](https://badge.fury.io/js/sequelize-validate-subfields.svg)](https://badge.fury.io/js/sequelize-validate-subfields)\n\nsimple framework for validating subfields of JSON attributes of Sequelize models\n\n# Introduction\n\nIf you ever use JSON attributes in Sequelize models, you'll probably want to validate that the JSON matches some\nschema. And if values within the JSON come from a form filled out by a user, you'll probably want to be able to\nshow validation errors from the server associated with the correct form field on the client.\n\nBut Sequelize `ValidationError`s be default only identify the top-level attribute of each validation error. For\nexample if you have the following validator:\n\n```js\n  address: {\n    type: Sequelize.JSON,\n    validate: {\n      isValid(address) {\n        if (/^\\d{5}$/.test(address.postalCode)) throw new Error('invalid postal code')\n      }\n    }\n  }\n```\n\nYou'll get an error including the following information:\n\n```js\nValidationError {\n  errors: [\n    ValidationErrorItem {\n      message: 'invalid postal code',\n      type: 'Validation error',\n      path: 'address',\n      __raw: Error {\n        message: 'invalid postal code',\n      },\n      ...\n    }\n  ]\n}\n```\n\nIf you use this package to perform validation, you can tag each validation error with a specific field path instead:\n\n```js\n  address: {\n    type: Sequelize.JSON,\n    validate: {\n      isValid: validateSubfields(function * (address) {\n        if (/^\\d{5}$/.test(address.postalCode)) yield {path: ['postalCode'], message: 'invalid postal code'}\n        if (states.has(address.state)) yield {path: ['state'], message: 'invalid state'}\n        // etc.\n      })\n    }\n  }\n```\n\nAnd you'll get an error including those paths:\n\n```\nValidationError {\n  errors: [\n    ValidationErrorItem {\n      message: 'validation failed',\n      type: 'Validation error',\n      path: 'address',\n      __raw: Error {\n        message: 'validation failed',\n        validation: {\n          errors: [\n            {path: ['postalCode'], message: 'invalid postal code'},\n            {path: ['state'], message: 'invalid state'},\n          ]\n        }\n      }\n    }\n  ]\n}\n```\n\nThis package also provides a `flattenValidationErrors` function to combine subfield validation errors like in the\nexample above with normal validation errors on non-JSON fields:\n\n```js\n;[\n  { path: ['address', 'postalCode'], message: 'invalid postal code' },\n  { path: ['address', 'state'], message: 'invalid state' },\n]\n```\n\n# Installation\n\n```sh\nnpm install --save sequelize-validate-subfields\n```\n\n# API\n\n## `validateSubfields(validator)`\n\n```js\nconst { validateSubfields } = require('sequelize-validate-subfields')\n```\n\n### Arguments\n\n#### `validator: (value: any) =\u003e Iterable\u003cFieldValidation\u003e`\n\na generator function which receives the attribute `value` and may `yield` as many validation\n`FieldValidation` objects as you wish, each specifying a validation error in the following form:\n\n```js\n{\n  path: Array\u003cstring | number\u003e,\n  message: string,\n}\n```\n\n`path` is the `lodash.get`-style path within the attribute (i.e. not including the attribute name itself) that\n`message` is associated with. For example if you define an `address` attribute and validator on a model like this:\n\n```js\n  address: {\n    type: Sequelize.JSON,\n    validate: {\n      isValid: validateSubfields(function * (address) {\n        if (/^\\d{5}$/.test(address.postalCode)) yield {path: ['postalCode'], message: 'invalid postal code'}\n        if (states.has(address.state)) yield {path: ['state'], message: 'invalid state'}\n        // etc.\n      })\n    }\n  }\n```\n\nNotice that the yielded `path`s don't contain `'address'` (the name of the attribute) themselves; they must be relative\nto the root of the attribute, instead of the root of the model instance.\n\n### Returns: `(value: any) =\u003e void`\n\nA Sequelize custom validator function that delegates to your `validator` function and combines any `FieldValidation`s\nit `yield`ed into the appropriate thrown error.\n\n## `flattenValidationErrors(error, [options])`\n\n```js\nconst { flattenValidationErrors } = require('sequelize-validate-subfields')\n```\n\n### Arguments\n\n#### `error: Sequelize.ValidationError`\n\nA `ValidationError` hich may contain zero or more `ValidationErrorItems` -- some may be from non-JSON field\nvalidation errors, and some may contain `FieldValidation`s from `validateSubfields`.\n\n#### `options?: {formatItemMessage?: (item: ValidationErrorItem) =\u003e string}`\n\n`formatItemMessage` allows you to override the error `message`s output for `ValidationErrorItem`s that don't\ncontain `FieldValidation`s using your own custom function.\n\n### Returns: `Array\u003cFieldValidation\u003e`\n\nA flattened array of `FieldValidation`s with `path`s relative to the root of the model instance instead of\nbeing relative to specific attributes. For instance, if you have the following model:\n\n```js\nconst User = sequelize.define('User', {\n  username: {\n    type: Sequelize.STRING,\n    validate: {\n      notEmpty: {\n        msg: 'required',\n      },\n    },\n  },\n  address: {\n    type: Sequelize.JSON,\n    validate: {\n      isValid: validateSubfields(function* (address) {\n        if (/^\\d{5}$/.test(address.postalCode))\n          yield { path: ['postalCode'], message: 'invalid postal code' }\n        if (states.has(address.state))\n          yield { path: ['state'], message: 'invalid state' }\n        // etc.\n      }),\n    },\n  },\n})\n```\n\nAnd you validate the following fields:\n\n```js\n{\n  username: '',\n  address: {\n    postalCode: '123',\n    state: 'KG',\n  }\n}\n```\n\nYou will get a `ValidationError` with the following structure:\n\n```\nValidationError {\n  errors: [\n    ValidationErrorItem {\n      message: 'required',\n      type: 'Validation error',\n      path: 'username',\n    },\n    ValidationErrorItem {\n      message: 'validation failed',\n      type: 'Validation error',\n      path: 'address',\n      __raw: Error {\n        message: 'validation failed',\n        validation: {\n          errors: [\n            {path: ['postalCode'], message: 'invalid postal code'},\n            {path: ['state'], message: 'invalid state'},\n          ]\n        }\n      }\n    }\n  ]\n}\n```\n\nCalling `flattenValidationErrors` on this will produce:\n\n```js\n;[\n  { path: ['username'], message: 'required' },\n  { path: ['address', 'postalCode'], message: 'invalid postal code' },\n  { path: ['address', 'state'], message: 'invalid state' },\n]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fsequelize-validate-subfields","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcoreio%2Fsequelize-validate-subfields","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fsequelize-validate-subfields/lists"}