{"name":"@eslint/object-schema","version":"2.1.4","description":"An object schema merger/validator","type":"module","main":"dist/esm/index.js","types":"dist/esm/index.d.ts","exports":{"require":{"types":"./dist/cjs/index.d.cts","default":"./dist/cjs/index.cjs"},"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"}},"publishConfig":{"access":"public"},"directories":{"test":"tests"},"scripts":{"build:cts":"node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"","build":"rollup -c && tsc -p tsconfig.esm.json && npm run build:cts","test:jsr":"npx jsr@latest publish --dry-run","test":"mocha tests/","test:coverage":"c8 npm test"},"repository":{"type":"git","url":"git+https://github.com/eslint/rewrite.git"},"keywords":["object","validation","schema","merge"],"author":{"name":"Nicholas C. Zakas"},"license":"Apache-2.0","bugs":{"url":"https://github.com/eslint/rewrite/issues"},"homepage":"https://github.com/eslint/rewrite#readme","devDependencies":{"c8":"^9.1.0","mocha":"^10.4.0","rollup":"^4.16.2","rollup-plugin-copy":"^3.5.0","typescript":"^5.4.5"},"engines":{"node":"^18.18.0 || ^20.9.0 || >=21.1.0"},"_id":"@eslint/object-schema@2.1.4","gitHead":"2a7bed41976f94495f7ba46f459f1de9d3305664","_nodeVersion":"20.14.0","_npmVersion":"10.7.0","dist":{"integrity":"sha512-BsWiH1yFGjXXS2yvrf5LyuoSIIbPrGUWob917o+BTKuZ7qJdxX8aJLRxs1fS9n6r7vESrq1OUqb68dANcFXuQQ==","shasum":"9e69f8bb4031e11df79e03db09f9dbbae1740843","tarball":"http://registry-npm.ywwl.com/@eslint/object-schema/download/@eslint/object-schema-2.1.4.tgz","fileCount":10,"unpackedSize":55487,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEvN7PZafbjBuQghL/76RRRPeYJRdB6MAOlRRh6QPhg8AiEAgwf1Pz2DMSBIcyWcfAYXy7nk6Uy2HzkX9AFgsbuPEho="}],"size":10586,"noattachment":false,"key":"/@eslint/object-schema/-/@eslint/object-schema-2.1.4.tgz"},"_npmUser":{"name":"eslintbot","email":"nicholas+eslint@nczconsulting.com"},"maintainers":[{"name":"openjsfoundation","email":"npm@openjsf.org"},{"name":"eslintbot","email":"nicholas+eslint@nczconsulting.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/object-schema_2.1.4_1718202693724_0.9989908493722703"},"_hasShrinkwrap":false,"readme":"# ObjectSchema Package\n\n## Overview\n\nA JavaScript object merge/validation utility where you can define a different merge and validation strategy for each key. This is helpful when you need to validate complex data structures and then merge them in a way that is more complex than `Object.assign()`. This is used in the [`@eslint/config-array`](https://npmjs.com/package/@eslint/config-array) package but can also be used on its own.\n\n## Installation\n\nFor Node.js and compatible runtimes:\n\n```shell\nnpm install @eslint/object-schema\n# or\nyarn add @eslint/object-schema\n# or\npnpm install @eslint/object-schema\n# or\nbun install @eslint/object-schema\n```\n\nFor Deno:\n\n```shell\ndeno add @eslint/object-schema\n```\n\n## Usage\n\nImport the `ObjectSchema` constructor:\n\n```js\n// using ESM\nimport { ObjectSchema } from \"@eslint/object-schema\";\n\n// using CommonJS\nconst { ObjectSchema } = require(\"@eslint/object-schema\");\n\nconst schema = new ObjectSchema({\n\t// define a definition for the \"downloads\" key\n\tdownloads: {\n\t\trequired: true,\n\t\tmerge(value1, value2) {\n\t\t\treturn value1 + value2;\n\t\t},\n\t\tvalidate(value) {\n\t\t\tif (typeof value !== \"number\") {\n\t\t\t\tthrow new Error(\"Expected downloads to be a number.\");\n\t\t\t}\n\t\t},\n\t},\n\n\t// define a strategy for the \"versions\" key\n\tversion: {\n\t\trequired: true,\n\t\tmerge(value1, value2) {\n\t\t\treturn value1.concat(value2);\n\t\t},\n\t\tvalidate(value) {\n\t\t\tif (!Array.isArray(value)) {\n\t\t\t\tthrow new Error(\"Expected versions to be an array.\");\n\t\t\t}\n\t\t},\n\t},\n});\n\nconst record1 = {\n\tdownloads: 25,\n\tversions: [\"v1.0.0\", \"v1.1.0\", \"v1.2.0\"],\n};\n\nconst record2 = {\n\tdownloads: 125,\n\tversions: [\"v2.0.0\", \"v2.1.0\", \"v3.0.0\"],\n};\n\n// make sure the records are valid\nschema.validate(record1);\nschema.validate(record2);\n\n// merge together (schema.merge() accepts any number of objects)\nconst result = schema.merge(record1, record2);\n\n// result looks like this:\n\nconst result = {\n\tdownloads: 75,\n\tversions: [\"v1.0.0\", \"v1.1.0\", \"v1.2.0\", \"v2.0.0\", \"v2.1.0\", \"v3.0.0\"],\n};\n```\n\n## Tips and Tricks\n\n### Named merge strategies\n\nInstead of specifying a `merge()` method, you can specify one of the following strings to use a default merge strategy:\n\n-   `\"assign\"` - use `Object.assign()` to merge the two values into one object.\n-   `\"overwrite\"` - the second value always replaces the first.\n-   `\"replace\"` - the second value replaces the first if the second is not `undefined`.\n\nFor example:\n\n```js\nconst schema = new ObjectSchema({\n\tname: {\n\t\tmerge: \"replace\",\n\t\tvalidate() {},\n\t},\n});\n```\n\n### Named validation strategies\n\nInstead of specifying a `validate()` method, you can specify one of the following strings to use a default validation strategy:\n\n-   `\"array\"` - value must be an array.\n-   `\"boolean\"` - value must be a boolean.\n-   `\"number\"` - value must be a number.\n-   `\"object\"` - value must be an object.\n-   `\"object?\"` - value must be an object or null.\n-   `\"string\"` - value must be a string.\n-   `\"string!\"` - value must be a non-empty string.\n\nFor example:\n\n```js\nconst schema = new ObjectSchema({\n\tname: {\n\t\tmerge: \"replace\",\n\t\tvalidate: \"string\",\n\t},\n});\n```\n\n### Subschemas\n\nIf you are defining a key that is, itself, an object, you can simplify the process by using a subschema. Instead of defining `merge()` and `validate()`, assign a `schema` key that contains a schema definition, like this:\n\n```js\nconst schema = new ObjectSchema({\n\tname: {\n\t\tschema: {\n\t\t\tfirst: {\n\t\t\t\tmerge: \"replace\",\n\t\t\t\tvalidate: \"string\",\n\t\t\t},\n\t\t\tlast: {\n\t\t\t\tmerge: \"replace\",\n\t\t\t\tvalidate: \"string\",\n\t\t\t},\n\t\t},\n\t},\n});\n\nschema.validate({\n\tname: {\n\t\tfirst: \"n\",\n\t\tlast: \"z\",\n\t},\n});\n```\n\n### Remove Keys During Merge\n\nIf the merge strategy for a key returns `undefined`, then the key will not appear in the final object. For example:\n\n```js\nconst schema = new ObjectSchema({\n\tdate: {\n\t\tmerge() {\n\t\t\treturn undefined;\n\t\t},\n\t\tvalidate(value) {\n\t\t\tDate.parse(value); // throws an error when invalid\n\t\t},\n\t},\n});\n\nconst object1 = { date: \"5/5/2005\" };\nconst object2 = { date: \"6/6/2006\" };\n\nconst result = schema.merge(object1, object2);\n\nconsole.log(\"date\" in result); // false\n```\n\n### Requiring Another Key Be Present\n\nIf you'd like the presence of one key to require the presence of another key, you can use the `requires` property to specify an array of other properties that any key requires. For example:\n\n```js\nconst schema = new ObjectSchema();\n\nconst schema = new ObjectSchema({\n\tdate: {\n\t\tmerge() {\n\t\t\treturn undefined;\n\t\t},\n\t\tvalidate(value) {\n\t\t\tDate.parse(value); // throws an error when invalid\n\t\t},\n\t},\n\ttime: {\n\t\trequires: [\"date\"],\n\t\tmerge(first, second) {\n\t\t\treturn second;\n\t\t},\n\t\tvalidate(value) {\n\t\t\t// ...\n\t\t},\n\t},\n});\n\n// throws error: Key \"time\" requires keys \"date\"\nschema.validate({\n\ttime: \"13:45\",\n});\n```\n\nIn this example, even though `date` is an optional key, it is required to be present whenever `time` is present.\n\n## License\n\nApache 2.0\n\n## Sponsors\n\nThe following companies, organizations, and individuals support ESLint's ongoing maintenance and development. [Become a Sponsor](https://eslint.org/donate) to get your logo on our README and website.\n\n<!-- NOTE: This section is autogenerated. Do not manually edit.-->\n<!--sponsorsstart-->\n<h3>Platinum Sponsors</h3>\n<p><a href=\"https://automattic.com\"><img src=\"https://images.opencollective.com/automattic/d0ef3e1/logo.png\" alt=\"Automattic\" height=\"undefined\"></a></p><h3>Gold Sponsors</h3>\n<p><a href=\"#\"><img src=\"https://images.opencollective.com/guest-bf377e88/avatar.png\" alt=\"Eli Schleifer\" height=\"96\"></a> <a href=\"https://engineering.salesforce.com\"><img src=\"https://images.opencollective.com/salesforce/ca8f997/logo.png\" alt=\"Salesforce\" height=\"96\"></a> <a href=\"https://www.airbnb.com/\"><img src=\"https://images.opencollective.com/airbnb/d327d66/logo.png\" alt=\"Airbnb\" height=\"96\"></a></p><h3>Silver Sponsors</h3>\n<p><a href=\"https://www.jetbrains.com/\"><img src=\"https://images.opencollective.com/jetbrains/fe76f99/logo.png\" alt=\"JetBrains\" height=\"64\"></a> <a href=\"https://liftoff.io/\"><img src=\"https://images.opencollective.com/liftoff/5c4fa84/logo.png\" alt=\"Liftoff\" height=\"64\"></a> <a href=\"https://americanexpress.io\"><img src=\"https://avatars.githubusercontent.com/u/3853301?v=4\" alt=\"American Express\" height=\"64\"></a> <a href=\"https://www.workleap.com\"><img src=\"https://avatars.githubusercontent.com/u/53535748?u=d1e55d7661d724bf2281c1bfd33cb8f99fe2465f&v=4\" alt=\"Workleap\" height=\"64\"></a></p><h3>Bronze Sponsors</h3>\n<p><a href=\"https://www.notion.so\"><img src=\"https://images.opencollective.com/notion/bf3b117/logo.png\" alt=\"notion\" height=\"32\"></a> <a href=\"https://www.crosswordsolver.org/anagram-solver/\"><img src=\"https://images.opencollective.com/anagram-solver/2666271/logo.png\" alt=\"Anagram Solver\" height=\"32\"></a> <a href=\"https://icons8.com/\"><img src=\"https://images.opencollective.com/icons8/7fa1641/logo.png\" alt=\"Icons8\" height=\"32\"></a> <a href=\"https://discord.com\"><img src=\"https://images.opencollective.com/discordapp/f9645d9/logo.png\" alt=\"Discord\" height=\"32\"></a> <a href=\"https://www.ignitionapp.com\"><img src=\"https://avatars.githubusercontent.com/u/5753491?v=4\" alt=\"Ignition\" height=\"32\"></a> <a href=\"https://nx.dev\"><img src=\"https://avatars.githubusercontent.com/u/23692104?v=4\" alt=\"Nx\" height=\"32\"></a> <a href=\"https://herocoders.com\"><img src=\"https://avatars.githubusercontent.com/u/37549774?v=4\" alt=\"HeroCoders\" height=\"32\"></a></p>\n<!--sponsorsend-->\n","publish_time":1718202693901,"_cnpm_publish_time":1718202693901,"dist-tags":{"latest":"3.0.5"}}