{"_id":"@birchill/discriminator","_rev":"4-51c771afbf7eb760af017aab703b1295","name":"@birchill/discriminator","dist-tags":{"latest":"0.3.1"},"versions":{"0.1.0":{"name":"@birchill/discriminator","version":"0.1.0","author":{"name":"Birchill, Inc."},"license":"MIT","_id":"@birchill/discriminator@0.1.0","maintainers":[{"name":"birtles","email":"birtles@gmail.com"}],"homepage":"https://github.com/birchill/discriminator#readme","bugs":{"url":"https://github.com/birchill/discriminator/issues"},"dist":{"shasum":"a6f6ccc55701d2072e6b8b7e9199c38d16cc5749","tarball":"https://registry.npmjs.org/@birchill/discriminator/-/discriminator-0.1.0.tgz","fileCount":8,"integrity":"sha512-VAPyxxm8/VDBjsttMR6SEZOiW5tgzmoASHxDJ+Jk3V6bN2PVsS+ooClYXfDpNN/b6NdmGyYS95tXREcO+GXung==","signatures":[{"sig":"MEYCIQDbk1p1FztRiAVFVY3ei3FW79zYn/RSPQy1Bu4avYTb5QIhAOsdiEvjZeY82+VF3ZewIHeW8+lY0XYNAWF3/LvPK13F","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":10462,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhJ1WgCRA9TVsSAnZWagAAH1cQAIdE10YuCfcOTWGkhXEG\ngb7DSH5WTQ25nVxpkMyebepjRas6/m0l4y+Hg4GN9qN/XRlAaNBK4BBENCPx\nYlorKkPvMbJhdFiijzKMbFxTYDUdvv0vPcZqyIZ3DqE+dbaGpmFVA1M2e8AB\nN9nu2zFXnaULvs05t/BzlrbE9GRoxWiFz3K+uJhRaMTQ+m5CDMiiAdgxGz3N\nE9K7z3mqlnD7pdKcl6eTnMMerwwqvSqjk1gGnLHgfMg22mtRtzuGRM75Moyq\n7spT9h5carqn7BWtdnpwSNzjYbHSkTv69k/KDDuUdhZYwNbGD6pPHJXDkNyK\n08/T3X1DmzRHY7rQCFb0AmaHwLP8eaFJWqxoXJQyGO+IrVFqb6rwCaOwkg3X\nEJm/NizCNqPj+E4RFmxSOtYsgbswygwAIQYR3paO432sGJnUFlDTtj2xSIIv\nzqvHeKVmvq3SilHq9me/8GZ8BqDOxFzTwyqdO69QmVXtqYy92YOM9zr8e8Se\ndwUEFy0tKKQPKFEdnc3dVC8RbGADg3WVIGIOVhW/FM2lSmwn3Z5gZyFt/NLq\nTFWUOn0906hZ56GY8fOFoJHG0/neHneIfh8KOuF8xMWvaBh6jsi75w+KQVcp\nj/UYxDecpGJUbtx7greDoCUSHX+wxDUpKWuPPWw00APPRRvSgN2q9Zmj+24q\njh7Y\r\n=/Inr\r\n-----END PGP SIGNATURE-----\r\n"},"main":"dist/index.js","engines":{"node":">=12.0.0"},"scripts":{"test":"mocha","build":"tsc","release":"standard-version","postinstall":"husky install","postpublish":"pinst --enable","prepublishOnly":"pinst --disable && tsc"},"typings":"dist/index.d.ts","_npmUser":{"name":"birtles","email":"birtles@gmail.com"},"prettier":{"singleQuote":true,"trailingComma":"es5","bracketSpacing":true},"repository":{"url":"https://github.com/birchill/discriminator.git","type":"git"},"description":"A tagged union type for superstruct based on JSON typedef's discriminator type","directories":{},"licenseText":"MIT License\n\nCopyright (c) 2021 Birchill, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","lint-staged":{"*.{js,ts,md}":["prettier --write"]},"_hasShrinkwrap":false,"devDependencies":{"husky":"^7.0.2","mocha":"^9.1.0","pinst":"^2.1.6","ts-node":"^10.2.1","prettier":"^2.3.2","typescript":"^4.3.5","lint-staged":"^11.1.2","superstruct":"^0.15.2","@types/mocha":"^9.0.0","@types/assert":"^1.5.6","standard-version":"^9.3.1"},"peerDependencies":{"superstruct":"^0.15.0"},"_npmOperationalInternal":{"tmp":"tmp/discriminator_0.1.0_1629967776115_0.385284795219925","host":"s3://npm-registry-packages"}},"0.2.0":{"name":"@birchill/discriminator","version":"0.2.0","keywords":["superstruct","tagged union","discriminated union","sum type","JSON typedef"],"author":{"name":"Birchill, Inc."},"license":"MIT","_id":"@birchill/discriminator@0.2.0","maintainers":[{"name":"birtles","email":"birtles@gmail.com"}],"homepage":"https://github.com/birchill/discriminator#readme","bugs":{"url":"https://github.com/birchill/discriminator/issues"},"dist":{"shasum":"9dc19b0b737f38f3ac641c40777be6991b908fae","tarball":"https://registry.npmjs.org/@birchill/discriminator/-/discriminator-0.2.0.tgz","fileCount":11,"integrity":"sha512-Gd5FjdCLO4FRMOquCNIFgdJvR0vlq7cNmQJpfql2ftOnJnnzoP/egqcoKoU2EEfpNIAQiw6e2JAhEeaezR1RXQ==","signatures":[{"sig":"MEYCIQC7OUj8YR/zzWac92LVbJ0Wrr/C1WQ1RomIOc0EZhz6BwIhANJhdD7uz6TrOINpM0brqgF8wyE6/5VTcUqnZkzoDpcB","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":28138,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjKTgQACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmp6VQ//Yt7a8zmlwZNo4S0KEDGUoFChw6YkbuhiXa9GtO9dm33fszY5\r\nU2Jp5hDswbLAYSzWg1Lwd9Y6lUgguTNdijYnwAn60wUGW/lPMoLkJWWj4K6E\r\nzpVlnh+qbp7MwHasmku/wE4s7EeE8ZKOREywbEpJcjSt7IK/TBTV5xFFS84a\r\nL3Cla6WTDEhY+RCPZuZsHdr8UzOh/j/AuS0Mp47vdHmFK1eGNZnc6NyW4NKG\r\nvyKvg/Q0H8d/W++Xw2YxEa8nAEDJNsfcyysHd6QQgjf5l/FmPflSAXv79AiM\r\n4+bol4gqh9ep9Ir4g4iTwLkmXdu9o8NH/YU5PSEqrLeM7UHWg4vv1iGJCvEA\r\nAWiiMgS53+wHSRnZV5+oU5tce84YL5cxf5s08yAF3R5+s3E81R8ztK7e4dlO\r\nuG+6R4AejxftwQ4Jw9tsmLJq5DW/Zdcp+Rb5TND9IwVMGu5wq65N2YtyWn5C\r\n2pBKUztnd4TO++sa+yRXILrw39zDLoE78uNM06/7fRgM/baJN8eY9aLQcrjO\r\ntkBis8ehotZdscP3EHx0t02l5iLOsu2sl03ySr3IQ3VnNWouZrhDXsraDVch\r\n3INKHJcAyyTU5ZVQpkE/rMchEjTRDSbQ+dEU8mEgYdWgyxbjZn43CH76+W0H\r\n91jLgQAFKwuLOCvE/B3/1ycGd6EiYLGUf+4=\r\n=vhcC\r\n-----END PGP SIGNATURE-----\r\n"},"main":"dist/index.cjs","type":"module","module":"dist/index.js","engines":{"node":">=12.0.0"},"scripts":{"test":"cross-env TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\", \"target\": \"es2019\" }' ts-mocha -p ./tsconfig.json","build":"rollup -c","release":"standard-version","postinstall":"husky install","postpublish":"pinst --enable","prepublishOnly":"pinst --disable && rollup -c"},"typings":"dist/index.d.ts","_npmUser":{"name":"birtles","email":"birtles@gmail.com"},"prettier":{"singleQuote":true,"trailingComma":"es5","bracketSpacing":true},"repository":{"url":"https://github.com/birchill/discriminator.git","type":"git"},"description":"A tagged union type for superstruct based on JSON typedef's discriminator type","directories":{},"licenseText":"MIT License\n\nCopyright (c) 2021 Birchill, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","lint-staged":{"*.{js,ts,md}":["prettier --write"]},"_hasShrinkwrap":false,"devDependencies":{"husky":"^7.0.2","mocha":"^9.1.0","pinst":"^2.1.6","rollup":"^2.79.0","ts-node":"^10.2.1","prettier":"^2.3.2","ts-mocha":"^10.0.0","cross-env":"^7.0.3","typescript":"^4.3.5","lint-staged":"^11.1.2","superstruct":"^0.15.2","@types/mocha":"^9.0.0","@types/assert":"^1.5.6","standard-version":"^9.3.1","rollup-plugin-dts":"^4.2.2","rollup-plugin-cleaner":"^1.0.0","@rollup/plugin-typescript":"^8.5.0"},"peerDependencies":{"superstruct":"^0.16.0"},"_npmOperationalInternal":{"tmp":"tmp/discriminator_0.2.0_1663645712700_0.749941461172112","host":"s3://npm-registry-packages"}},"0.3.0":{"name":"@birchill/discriminator","version":"0.3.0","keywords":["superstruct","tagged union","discriminated union","sum type","JSON typedef"],"author":{"name":"Birchill, Inc."},"license":"MIT","_id":"@birchill/discriminator@0.3.0","maintainers":[{"name":"birtles","email":"birtles@gmail.com"}],"homepage":"https://github.com/birchill/discriminator#readme","bugs":{"url":"https://github.com/birchill/discriminator/issues"},"dist":{"shasum":"77d09d754e1ed14fc6fbc94af16724bc4f46d97b","tarball":"https://registry.npmjs.org/@birchill/discriminator/-/discriminator-0.3.0.tgz","fileCount":9,"integrity":"sha512-IXkLc1FZLSjFUcPYUnAdg+AWPHC9vPz8hS2IDwPAvnAipeNKIkzQ/B4WY5z14UO1Ex0aOdlHtgwUUXc/Ngkhyg==","signatures":[{"sig":"MEQCIFUh4ngdOkxb3l/RjqESGZIm/i0/Bs17DJoy8hGz7fhQAiBOfzt8l40T0rb/REfkkOzK1fGk6p9Qa4l8seC14jJONQ==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":24864},"main":"./dist/index.cjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.js","engines":{"node":">=12.0.0"},"exports":{".":{"import":"./dist/index.js","require":"./dist/index.cjs"}},"gitHead":"ce6ebbd74e038af505b5d871f4e641e4aa1ddc07","scripts":{"test":"vitest run","build":"tsup","prepare":"husky install","postpublish":"pinst --enable","_postinstall":"husky install","prepublishOnly":"pinst --disable && tsup"},"_npmUser":{"name":"birtles","email":"birtles@gmail.com"},"prettier":{"singleQuote":true,"trailingComma":"es5","bracketSpacing":true},"repository":{"url":"git+https://github.com/birchill/discriminator.git","type":"git"},"_npmVersion":"9.5.1","description":"A tagged union type for superstruct based on JSON typedef's discriminator type","directories":{},"lint-staged":{"*.{js,ts,md}":["prettier --write"]},"_nodeVersion":"18.16.0","publishConfig":{"registry":"https://registry.npmjs.org"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"7.2.0","husky":"^8.0.3","pinst":"^3.0.0","vitest":"0.34.6","prettier":"^3.0.3","release-it":"16.2.1","typescript":"^5.2.2","lint-staged":"^14.0.1","superstruct":"^1.0.3","vitest-github-actions-reporter":"0.10.0","@release-it/conventional-changelog":"7.0.2"},"peerDependencies":{"superstruct":">0.16.0 <2.0.0"},"_npmOperationalInternal":{"tmp":"tmp/discriminator_0.3.0_1697082378499_0.7986882046735428","host":"s3://npm-registry-packages"}},"0.3.1":{"name":"@birchill/discriminator","version":"0.3.1","description":"A tagged union type for superstruct based on JSON typedef's discriminator type","keywords":["superstruct","tagged union","discriminated union","sum type","JSON typedef"],"repository":{"type":"git","url":"git+https://github.com/birchill/discriminator.git"},"author":{"name":"Birchill, Inc."},"license":"MIT","bugs":{"url":"https://github.com/birchill/discriminator/issues"},"publishConfig":{"registry":"https://registry.npmjs.org"},"type":"module","main":"./dist/index.cjs","module":"./dist/index.js","exports":{".":{"import":"./dist/index.js","require":"./dist/index.cjs"}},"homepage":"https://github.com/birchill/discriminator#readme","engines":{"node":">=12.0.0"},"scripts":{"build":"tsup","test":"vitest run","prepublishOnly":"pinst --disable && tsup","postpublish":"pinst --enable","_postinstall":"husky","prepare":"husky"},"lint-staged":{"*.{js,ts,md}":["prettier --write"]},"prettier":{"singleQuote":true,"trailingComma":"es5","bracketSpacing":true},"devDependencies":{"@release-it/conventional-changelog":"9.0.2","husky":"9.1.6","lint-staged":"15.2.10","pinst":"3.0.0","prettier":"3.3.3","release-it":"17.10.0","superstruct":"2.0.2","tsup":"8.3.5","typescript":"5.6.3","vitest":"2.1.4","vitest-github-actions-reporter":"0.11.1"},"peerDependencies":{"superstruct":">0.16.0 <3.0.0"},"_id":"@birchill/discriminator@0.3.1","gitHead":"8d2b336c7513b8fb688cdae8bb5fa34887cb7805","types":"./dist/index.d.ts","_nodeVersion":"20.11.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-vc7dbXbcURpbFUBFnoa++Cb6ywoI3Ky6lyIL/pp7YuhcDn2gQUSoeWTeUZa02vUmEF7r/NH2YY2jj1irsNa7Ww==","shasum":"e4df84c3393420518db3e2668a1a0695339a3ad5","tarball":"https://registry.npmjs.org/@birchill/discriminator/-/discriminator-0.3.1.tgz","fileCount":9,"unpackedSize":24851,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIH8O5/BIjMduVDHyDXPPR4OWC6SIyB9/wGhLoEmsIiiIAiB49LdhCTCFBcGjJTzRKL5LWidUo/rCHPJEtCCOpK5G6A=="}]},"_npmUser":{"name":"birtles","email":"birtles@gmail.com"},"directories":{},"maintainers":[{"name":"birtles","email":"birtles@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/discriminator_0.3.1_1730945181737_0.6269520441101437"},"_hasShrinkwrap":false}},"time":{"created":"2021-08-26T08:49:36.046Z","modified":"2024-11-07T02:06:22.111Z","0.1.0":"2021-08-26T08:49:36.261Z","0.2.0":"2022-09-20T03:48:32.842Z","0.3.0":"2023-10-12T03:46:18.833Z","0.3.1":"2024-11-07T02:06:21.926Z"},"bugs":{"url":"https://github.com/birchill/discriminator/issues"},"author":{"name":"Birchill, Inc."},"license":"MIT","homepage":"https://github.com/birchill/discriminator#readme","keywords":["superstruct","tagged union","discriminated union","sum type","JSON typedef"],"repository":{"type":"git","url":"git+https://github.com/birchill/discriminator.git"},"description":"A tagged union type for superstruct based on JSON typedef's discriminator type","maintainers":[{"name":"birtles","email":"birtles@gmail.com"}],"readme":"# discriminator\n\nA tagged union type for [superstruct](https://docs.superstructjs.org/) based on\n[JSON typedef's\ndiscriminator](https://jsontypedef.com/docs/jtd-in-5-minutes/#discriminator-schemas)\ntype.\n\n```typescript\nimport { boolean, Infer, enums, string, object } from 'superstruct';\nimport { discriminator } from '@birchill/discriminator';\n\nconst schema = discriminator('eventType', {\n  USER_CREATED: object({ id: string() }),\n  USER_PAYMENT_PLAN_CHANGED: object({\n    id: string(),\n    plan: enums(['FREE', 'PAID']),\n  }),\n  USER_DELETED: object({ id: string(), softDelete: boolean() }),\n});\n\ntype SchemaType = Infer<typeof schema>;\n\n// SchemaType =\n// {\n//   eventType: \"USER_CREATED\";\n//   id: string;\n// } | {\n//   eventType: \"USER_PAYMENT_PLAN_CHANGED\";\n//   id: string;\n//   plan: \"FREE\" | \"PAID\";\n// } | {\n//   eventType: \"USER_DELETED\";\n//   id: string;\n//   softDelete: boolean;\n// }\n```\n\n## Why?\n\n- If you try to model the above using `union()` objects and validation fails you\n  get errors like:\n\n  ` Expected the value to satisfy a union of ``object | object | object``, but received: [object Object]`.\n\n  Using `discriminator()` you get errors like:\n\n  `At path: value.name -- Expected a string with a length between ``0``and ``256`` but received one with a length of ``257`` `.\n\n- Better semantics.\n- Easier translation to and from [JSON typedef](https://jsontypedef.com/) should\n  that be useful.\n\n## Specifics\n\n`discriminator()` takes two parameters:\n\n1. A string representing the tagged union's tag field.\n2. An object where the keys are the tag values and the values are `object()`,\n   `type()`, or `discriminator()` structs.\n\nIf you need to model a branch where there are no other properties just use an\nempty `object()` or `type()`.\nThis is important because you're indicating whether or not that branch is\nallowed to have extra values on it (`type()`) or not (`object()`).\n\ne.g.\n\n```typescript\ndiscriminator('action', {\n  signin: type({ email: string(), token: string() }),\n  signout: type(),\n});\n```\n\nYou can nest `discriminator()` objects like so:\n\n```typescript\ndiscriminator('result', {\n  success: discriminator('task', {\n    upload: type({\n      filename: string(),\n    }),\n    download: type({\n      filename: string(),\n      bytes: number(),\n    }),\n  }),\n  failure: type({\n    code: number(),\n  }),\n});\n\n// `Infer` here produces the type:\n//\n// {\n//   result: \"success\";\n//   task: \"upload\";\n//   filename: string;\n// } | {\n//   result: \"success\";\n//   task: \"download\";\n//   filename: string;\n//   bytes: number;\n// } | {\n//   result: \"failure\";\n//   code: number;\n// }\n```\n\n## Developing\n\n### Building\n\n```\npnpm build\n```\n\n### Testing\n\n```\npnpm test\n```\n\n### Releasing\n\n```\npnpm release-it\n```\n","readmeFilename":"README.md"}