{"id":27057289,"url":"https://github.com/iakovmarkov/json-schema-normalizer","last_synced_at":"2025-10-07T05:03:32.298Z","repository":{"id":57285207,"uuid":"100746027","full_name":"iakovmarkov/json-schema-normalizer","owner":"iakovmarkov","description":"A small JS library to validate \u0026 normalize your data according to JSON Schemas","archived":false,"fork":false,"pushed_at":"2017-11-15T23:16:40.000Z","size":56,"stargazers_count":15,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-04T18:33:35.999Z","etag":null,"topics":["frontend","javascript","json-schema","normalization"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/iakovmarkov.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}},"created_at":"2017-08-18T20:00:29.000Z","updated_at":"2024-04-09T02:14:02.000Z","dependencies_parsed_at":"2022-09-17T13:42:11.223Z","dependency_job_id":null,"html_url":"https://github.com/iakovmarkov/json-schema-normalizer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iakovmarkov%2Fjson-schema-normalizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iakovmarkov%2Fjson-schema-normalizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iakovmarkov%2Fjson-schema-normalizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iakovmarkov%2Fjson-schema-normalizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iakovmarkov","download_url":"https://codeload.github.com/iakovmarkov/json-schema-normalizer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276096,"owners_count":20912287,"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":["frontend","javascript","json-schema","normalization"],"created_at":"2025-04-05T11:18:24.592Z","updated_at":"2025-10-07T05:03:27.262Z","avatar_url":"https://github.com/iakovmarkov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON Schema Normalizer\n\n## What?\nJSON Schema Normalizer is a small library built on top of [ajv](https://github.com/epoberezkin/ajv)\nand [normalizr](https://github.com/paularmstrong/normalizr). It seamlessly merges the functionality of\nthose two libraries, allowing you to validate \u0026 normalize your data by JSON Schema.\n\n## Why?\nBecause writing Normalizr schemas is not fun. JSON Schema is a widely supported standard, and most ORMs\ncan export their models as JSON Schemas. This library allows you to load auto-generated schemas and use them\nto validate and normalize your data, saving you a ton of time.\n\n## Installation\n\n    npm install --save json-schema-normalizer\n    \n## Usage\nConsider your API returns the following blog post:\n```json\n{\n  \"id\": 42,\n  \"title\": \"Lorem Ipsum\",\n  \"content\": \"Lorem ipsum dolor sit amet.\",\n  \"author\": {\n    \"id\": 515,\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\"\n  },\n  \"comments\": [\n    {\n      \"id\": 1,\n      \"content\": \"This is really good\",\n      \"author\": {\n        \"id\": 313,\n        \"firstName\": \"Jane\",\n        \"lastName\": \"Doe\"\n      }\n    },\n    {\n      \"id\": 2,\n      \"content\": \"So helpful, much wow\",\n      \"author\": {\n        \"id\": 211,\n        \"firstName\": \"John\",\n        \"lastName\": \"Snow\"\n      }\n    },\n    {\n      \"id\": 3,\n      \"content\": \"Thanks for the kind words\",\n      \"author\": {\n        \"id\": 515,\n        \"firstName\": \"John\",\n        \"lastName\": \"Doe\"\n      }\n    }\n  ],\n  \"tags\": [\n    \"lorem\",\n    \"ipsum\"\n  ]\n}\n```\n\nWe've got a few entity types: `Post` itself, a `User`, and a `Comment`.\nNow, our API also gives us JSON Schemas for all those entity types:\n\n```json\n{\n  \"title\": \"Post\",\n  \"type\": \"object\",\n  \"description\": \"A blog post containing title, content, author \u0026 comments\",\n  \"required\": [\n    \"id\",\n    \"title\",\n    \"author\"\n  ],\n  \"additionalProperties\": false,\n  \"properties\": {\n    \"id\": {\n      \"type\": \"integer\"\n    },\n    \"title\": {\n      \"type\": \"string\"\n    },\n    \"content\": {\n      \"type\": \"string\"\n    },\n    \"author\": {\n      \"$ref\": \"#/definitions/Person\"\n    },\n    \"comments\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"$ref\": \"#/definitions/Comment\"\n      }\n    }\n  }\n}\n```\n```json\n{\n  \"title\": \"Comment\",\n  \"type\": \"object\",\n  \"description\": \"A comment containing content \u0026 author\",\n  \"required\": [\n    \"id\",\n    \"author\",\n    \"content\"\n  ],\n  \"properties\": {\n    \"id\": {\n      \"type\": \"integer\"\n    },\n    \"content\": {\n      \"type\": \"string\"\n    },\n    \"author\": {\n      \"$ref\": \"#/definitions/Person\"\n    }\n  }\n}\n```\n\nUsing this library, you can validate \u0026 normalize your data like this:\n```js\n// Import library\nconst { loadSchemas, normalize } = require('json-schema-normalizer')\n\n// Pass an array of schemas to define them\nloadSchemas([personSchema, postSchema, commentSchema])\n\n// Then call normalize with schema name \u0026 your denormalized data\nconst normalizedData = normalize('Post', rawData)\n```\n\nThat will give you the normal normalizr output:\n```js\n{\n  entities: {\n    Person: {\n      515: {\n        id: 515,\n          firstName: 'John',\n          lastName: 'Doe'\n      },\n      211: {\n        id: 211,\n          firstName: 'John',\n          lastName: 'Snow'\n      },\n      313: {\n        id: 313,\n          firstName: 'Jane',\n          lastName: 'Doe'\n      }\n    },\n    Post: {\n      42: {\n        id: 42,\n          title: 'Lorem Ipsum',\n          content: 'Lorem ipsum dolor sit amet.',\n          author: 515,\n          comments: [1, 2, 3]\n      }\n    },\n    Comment: {\n      1: { id: 1, content: 'This is really good', author: 313 },\n      2: { id: 2, content: 'So helpful, much wow', author: 211 },\n      3: { id: 3, content: 'Thanks for the kind words', author: 515 }\n    }\n  },\n  result: 42\n}\n```\n\nLook into `tests/index.js` for more examples.\n\n## API\n\n### `loadSchemas(schemas: Array\u003cobject\u003e): void`\nLoads an array of schemas into AJV and parses them for usage in Normalizr.\n\nYou probably need to call it only once when you get your schemas from API\nor load them from some file.\n\n### `normalize(schemaName: string, data: any): object`\nValidates your data against JSON Schema using AJV and normalizes it using Normalizr.\n\nReturned value is exactly the same as return value in Normalizr.\n\n### `reset(): void`\n\nA function that removes AJV schemas and parsen Normalizr schemas.\n\nYou probably won't need it, it is used in tests to reset storage after each test.\n    \n## Caveats\nIn order to work, this library requires your JSON Schemas to have a `title` property,\nthat will be a unique name of this schema. It is used to store Normalizer schemas and\nto get JSON Schema for AJV validation.\n\n## Dependencies\n- [ajv](https://github.com/epoberezkin/ajv)\n- [normalizr](https://github.com/paularmstrong/normalizr)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiakovmarkov%2Fjson-schema-normalizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiakovmarkov%2Fjson-schema-normalizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiakovmarkov%2Fjson-schema-normalizer/lists"}