{"id":15681678,"url":"https://github.com/jonschlinkert/map-schema","last_synced_at":"2026-03-14T09:09:05.709Z","repository":{"id":60774789,"uuid":"49839396","full_name":"jonschlinkert/map-schema","owner":"jonschlinkert","description":"Normalize an object by running normalizers and validators that are mapped to a schema.","archived":false,"fork":false,"pushed_at":"2020-03-02T01:47:48.000Z","size":72,"stargazers_count":10,"open_issues_count":4,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-19T21:23:17.635Z","etag":null,"topics":["normalize","object","schema","validate"],"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/jonschlinkert.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":"2016-01-17T22:49:36.000Z","updated_at":"2021-04-12T22:19:35.000Z","dependencies_parsed_at":"2022-10-04T16:32:56.941Z","dependency_job_id":null,"html_url":"https://github.com/jonschlinkert/map-schema","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fmap-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fmap-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fmap-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fmap-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonschlinkert","download_url":"https://codeload.github.com/jonschlinkert/map-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252856834,"owners_count":21814905,"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":["normalize","object","schema","validate"],"created_at":"2024-10-03T16:58:26.825Z","updated_at":"2026-03-14T09:09:00.671Z","avatar_url":"https://github.com/jonschlinkert.png","language":"JavaScript","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=W8YFZ425KND68"],"categories":[],"sub_categories":[],"readme":"# map-schema [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=W8YFZ425KND68) [![NPM version](https://img.shields.io/npm/v/map-schema.svg?style=flat)](https://www.npmjs.com/package/map-schema) [![NPM monthly downloads](https://img.shields.io/npm/dm/map-schema.svg?style=flat)](https://npmjs.org/package/map-schema) [![NPM total downloads](https://img.shields.io/npm/dt/map-schema.svg?style=flat)](https://npmjs.org/package/map-schema) [![Build Status](https://travis-ci.org/jonschlinkert/map-schema.svg?branch=master)](https://travis-ci.org/jonschlinkert/map-schema)\n\n\u003e Normalize an object by running normalizers and validators that are mapped to a schema.\n\nYou might also be interested in [normalize-pkg](https://github.com/jonschlinkert/normalize-pkg).\n\nPlease consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.\n\n## Table of Contents\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eDetails\u003c/strong\u003e\u003c/summary\u003e\n\n- [Install](#install)\n- [Usage](#usage)\n- [API](#api)\n- [About](#about)\n\n\u003c/details\u003e\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/) (requires [Node.js](https://nodejs.org/en/) \u003e=10):\n\n```sh\n$ npm install --save map-schema\n```\n\n## Usage\n\n```js\nvar schema = require('map-schema');\n```\n\n**Example**\n\nThis is a basic example schema for normalizing and validating fields on `package.json` (a full version of this will be available on [normalize-pkg](https://github.com/jonschlinkert/normalize-pkg) when complete):\n\n```js\nvar fs = require('fs');\nvar isObject = require('isobject');\nvar Schema = require('map-schema');\n\n// create a schema\nvar schema = new Schema()\n  .field('name', 'string')\n  .field('description', 'string')\n  .field('repository', ['object', 'string'], {\n    normalize: function(val) {\n      return isObject(val) ? val.url : val;\n    }\n  })\n  .field('main', 'string', {\n    validate: function(filepath) {\n      return fs.existsSync(filepath);\n    }\n  })\n  .field('version', 'string', {\n    default: '0.1.0'\n  })\n  .field('license', 'string', {\n    default: 'MIT'\n  })\n\nvar pkg = require('./package');\n// normalize an object\nconsole.log(schema.normalize(pkg));\n// validation errors array\nconsole.log(schema.errors);\n```\n\n**Errors**\n\nValidation errors are exposed on `schema.errors`. Error reporting is pretty basic right now but I plan to implement something better soon.\n\n## API\n\n**Params**\n\n* `options` **{Object}**\n\n**Example**\n\n```js\nvar schema = new Schema()\n  .field('name', 'string')\n  .field('version', 'string')\n  .field('license', 'string')\n  .field('licenses', 'array', {\n    normalize: function(val, key, config) {\n       // convert license array to `license` string\n       config.license = val[0].type;\n       delete config[key];\n    }\n  })\n  .normalize(require('./package'))\n```\n\nSet `key` on the instance with the given `value`.\n\n**Params**\n\n* `key` **{String}**\n* `value` **{Object}**\n\nPush a warning onto the `schema.warnings` array. Placeholder for\nbetter message handling and a reporter (planned).\n\n**Params**\n\n* `method` **{String}**: The name of the method where the warning is recorded.\n* `prop` **{String}**: The name of the field for which the warning is being created.\n* `message` **{String}**: The warning message.\n* `value` **{String}**: The value associated with the warning.\n* `returns` **{any}**\n\n**Params**\n\n* `name` **{String}**\n* `type` **{String|Array}**\n* `options` **{Object}**\n* `returns` **{Object}**: Returns the instance for chaining.\n\n**Example**\n\n```js\nvar semver = require('semver');\n\nschema\n  .field('keywords', 'array')\n  .field('version', 'string', {\n    validate: function(val, key, config, schema) {\n      return semver.valid(val) !== null;\n    }\n  })\n```\n\n**Params**\n\n* `name` **{Strign}**\n* `prop` **{String}**\n* `returns` **{Object|any}**: Returns the field instance or the value of `prop` if specified.\n\n**Example**\n\n```js\nschema.field('bugs', ['object', 'string']);\nvar field = schema.get('bugs', 'types');\n//=\u003e ['object', 'string']\n```\n\nOmit a property from the returned object. This method can be used\nin normalize functions as a way of removing undesired properties.\n\n**Params**\n\n* `key` **{String}**: The property to remove\n* `returns` **{Object}**: Returns the instance for chaining.\n\nUpdate a property on the returned object. This method will trigger validation\nand normalization of the updated property.\n\n**Params**\n\n* `key` **{String}**: The property to update.\n* `val` **{any}**: Value of the property to update.\n* `returns` **{Object}**: Returns the instance for chaining.\n\nReturns true if field `name` is an optional field.\n\n**Params**\n\n* `name` **{String}**\n* `returns` **{Boolean}**\n\nReturns true if field `name` was defined as a required field.\n\n**Params**\n\n* `name` **{String}**\n* `returns` **{Boolean}**\n\nChecks the config object for missing fields and. If found,\na warning message is pushed onto the `schema.warnings` array,\nwhich can be used for reporting.\n\n**Params**\n\n* `config` **{Object}**\n* `returns` **{Array}**\n\n**Params**\n\n* `config` **{Object}**\n* `returns` **{Object}**: Returns the config object with keys sorted to match the given array of keys.\n\n**Example**\n\n```js\nschema.sortObject({z: '', a: ''}, ['a', 'z']);\n//=\u003e {a: '', z: ''}\n```\n\nWhen `options.sortArrays` _is not false_, sorts all arrays in the\ngiven `config` object using JavaScript's native `.localeCompare`\nmethod.\n\n**Params**\n\n* `config` **{Object}**\n* `returns` **{Object}**: returns the config object with sorted arrays\n\nReturns true if the given value is valid for field `key`.\n\n**Params**\n\n* `key` **{String}**\n* `val` **{any}**\n* `config` **{Object}**\n* `returns` **{Boolean}**\n\nNormalize the given `config` object.\n\n**Params**\n\n* **{String}**: key\n* **{any}**: value\n* **{Object}**: config\n* `returns` **{Object}**\n\nNormalize a field on the schema.\n\n**Params**\n\n* **{String}**: key\n* **{any}**: value\n* **{Object}**: config\n* `returns` **{Object}**\n\nVisit `method` over the given object or array.\n\n**Params**\n\n* `method` **{String}**\n* `value` **{Object|Array}**\n* `returns` **{Object}**: Returns the instance for chaining.\n\n### [Field](lib/field.js#L22)\n\nCreate a new `Field` of the given `type` to validate against, and optional `config` object.\n\n**Params**\n\n* `type` **{String|Array}**: One more JavaScript native types to use for validation.\n* `config` **{Object}**\n\n**Example**\n\n```js\nconst field = new Field('string', {\n  normalize: function(val) {\n    // do stuff to `val`\n    return val;\n  }\n});\n```\n\n### [.isValidType](lib/field.js#L67)\n\nReturns true if the given `type` is a valid type.\n\n**Params**\n\n* `type` **{String}**\n* `returns` **{Boolean}**\n\n### [.validate](lib/field.js#L89)\n\nCalled in `schema.validate`, returns true if the given `value` is valid. This default validate method returns true unless overridden with a custom `validate` method.\n\n* `returns` **{Boolean}**\n\n**Example**\n\n```js\nvar field = new Field({\n  types: ['string']\n});\n\nfield.validate('name', {});\n//=\u003e false\n```\n\n### [.normalize](lib/field.js#L108)\n\nNormalize the field's value.\n\n**Example**\n\n```js\nvar field = new Field({\n  types: ['string'],\n  normalize: function(val, key, config, schema) {\n    // do stuff to `val`\n    return val;\n  }\n});\n```\n\n## About\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eContributing\u003c/strong\u003e\u003c/summary\u003e\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRunning Tests\u003c/strong\u003e\u003c/summary\u003e\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eBuilding docs\u003c/strong\u003e\u003c/summary\u003e\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n\u003c/details\u003e\n\n### Related projects\n\nYou might also be interested in these projects:\n\n* [get-value](https://www.npmjs.com/package/get-value): Use property paths like 'a.b.c' to get a nested value from an object. Even works… [more](https://github.com/jonschlinkert/get-value) | [homepage](https://github.com/jonschlinkert/get-value \"Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!).\")\n* [normalize-pkg](https://www.npmjs.com/package/normalize-pkg): Normalize values in package.json using the map-schema library. | [homepage](https://github.com/jonschlinkert/normalize-pkg \"Normalize values in package.json using the map-schema library.\")\n* [object.omit](https://www.npmjs.com/package/object.omit): Return a copy of an object excluding the given key, or array of keys. Also… [more](https://github.com/jonschlinkert/object.omit) | [homepage](https://github.com/jonschlinkert/object.omit \"Return a copy of an object excluding the given key, or array of keys. Also accepts an optional filter function as the last argument.\")\n* [object.pick](https://www.npmjs.com/package/object.pick): Returns a filtered copy of an object with only the specified keys, similar to `_.pick… [more](https://github.com/jonschlinkert/object.pick) | [homepage](https://github.com/jonschlinkert/object.pick \"Returns a filtered copy of an object with only the specified keys, similar to`_.pick` from lodash / underscore.\")\n* [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value \"Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.\")\n\n### Author\n\n**Jon Schlinkert**\n\n* [GitHub Profile](https://github.com/jonschlinkert)\n* [Twitter Profile](https://twitter.com/jonschlinkert)\n* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)\n\n### License\n\nCopyright © 2020, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on March 01, 2020._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fmap-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonschlinkert%2Fmap-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fmap-schema/lists"}