{"_id":"@cordis-tavern/schemastery","name":"@cordis-tavern/schemastery","dist-tags":{"rc":"3.18.0","latest":"3.18.0"},"versions":{"3.18.0":{"name":"@cordis-tavern/schemastery","description":"Type driven schema validator","version":"3.18.0","main":"lib/index.cjs","module":"lib/index.mjs","typings":"lib/index.d.ts","repository":{"type":"git","url":"git+https://github.com/shigma/schemastery.git","directory":"packages/core"},"bugs":{"url":"https://github.com/shigma/schemastery/issues"},"homepage":"https://github.com/shigma/schemastery","author":{"name":"Shigma","email":"shigma10826@gmail.com"},"license":"MIT","dependencies":{"@standard-schema/spec":"^1.1.0","@cordis-tavern/cosmokit":"^1.8.1"},"exports":{".":{"types":"./lib/index.d.ts","require":"./lib/index.cjs","import":"./lib/index.cjs","default":"./lib/index.cjs"},"./package.json":"./package.json"},"_id":"@cordis-tavern/schemastery@3.18.0","_nodeVersion":"24.18.0","_npmVersion":"11.16.0","dist":{"integrity":"sha512-rh0n02aQL/eKJO6/if+MrPYO7Dnydf6DIuDxKyER2EUZfwZjU7qE2Dt/1TwaOf618P2zin+EUjGYMN2ckBp1HA==","shasum":"145c014900b557179934e13c4966249892fbf485","tarball":"https://registry.npmjs.org/@cordis-tavern/schemastery/-/schemastery-3.18.0.tgz","fileCount":8,"unpackedSize":133776,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCw1stKrKc/vQgpseoSv68SaAQI6huDjQkCDA39kVEBRQIhAOJGrqH/KTP1NT04LB0LY7lyiN6MQNazW9kFr59VZAyp"}]},"_npmUser":{"name":"lianxiuqing","email":"shb1348331639@163.com"},"directories":{},"maintainers":[{"name":"lianxiuqing","email":"shb1348331639@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/schemastery_3.18.0_1789148316023_0.5337062901863514"},"_hasShrinkwrap":false}},"time":{"created":"2026-09-11T17:38:35.827Z","3.18.0":"2026-09-11T17:38:36.150Z","modified":"2026-09-11T17:38:36.420Z"},"maintainers":[{"name":"lianxiuqing","email":"shb1348331639@163.com"}],"description":"Type driven schema validator","homepage":"https://github.com/shigma/schemastery","repository":{"type":"git","url":"git+https://github.com/shigma/schemastery.git","directory":"packages/core"},"author":{"name":"Shigma","email":"shigma10826@gmail.com"},"bugs":{"url":"https://github.com/shigma/schemastery/issues"},"license":"MIT","readme":"# Schemastery\n\n[![Codecov](https://img.shields.io/codecov/c/github/shigma/schemastery?style=flat-square)](https://codecov.io/gh/shigma/schemastery)\n[![downloads](https://img.shields.io/npm/dm/schemastery?style=flat-square)](https://www.npmjs.com/package/schemastery)\n[![npm](https://img.shields.io/npm/v/schemastery?style=flat-square)](https://www.npmjs.com/package/schemastery)\n[![GitHub](https://img.shields.io/github/license/shigma/schemastery?style=flat-square)](https://github.com/shigma/schemastery/blob/master/LICENSE)\n\nType Driven Schema Validator.\n\n## Features\n\n- **Lightweight.** Much smaller than other validation libraries.\n- **Easy to use.** You can use any schema as a function or constructor directly.\n- **Powerful.** Schemastery supports some advanced types such as `union`, `intersect` and `transform`.\n- **Extensible.** You can create your own schema types via `Schema.extend()`.\n- **Serializable.** Schema objects can be serialized into JSON and then be hydrated in another environment.\n\n## Basic Examples\n\n### use as validator (JavaScript)\n\n```js\nconst Schema = require('schemastery')\n\nconst validate = Schema.number().default(10)\n\nvalidate(0)     // 0\nvalidate(null)  // 10\nvalidate('')    // TypeError\n```\n\n### use as constructor (TypeScript)\n\n```ts\nimport Schema from 'schemastery'\n\ninterface Config {\n  foo: Record<string, string>\n  bar: string[]\n}\n\nconst Config = Schema.object({\n  foo: Schema.dict(Schema.string()).default({}),\n  bar: Schema.array(Schema.string()).default([]),\n})\n\n// config is an instance of Config\n// in this case, that is { foo: {}, bar: [] }\nconst config = new Config()\n```\n\n## General Types\n\n### Schema.any()\n\nAssert that the value is of any type.\n\n```js\nconst validate = Schema.any()\n\nvalidate()            // undefined\nvalidate(0)           // 0\nvalidate({})          // {}\n```\n\n### Schema.never()\n\nAssert that the value is nullable.\n\n```js\nconst validate = Schema.never()\n\nvalidate()            // undefined\nvalidate(0)           // TypeError\nvalidate({})          // TypeError\n```\n\n### Schema.const(value)\n\nAssert that the value is equal to the given constant.\n\n```js\nconst validate = Schema.const(10)\n\nvalidate(10)          // 10\nvalidate(0)           // TypeError\n```\n\n### Schema.number()\n\nAssert that the value is a number.\n\n```js\nconst validate = Schema.number()\n\nvalidate()            // undefined\nvalidate(1)           // 1\nvalidate('')          // TypeError\n```\n\n### Schema.string()\n\nAssert that the value is a string.\n\n```js\nconst validate = Schema.string()\n\nvalidate()            // undefined\nvalidate(0)           // TypeError\nvalidate('foo')       // 'foo'\n```\n\n### Schema.boolean()\n\nAssert that the value is a boolean.\n\n```js\nconst validate = Schema.boolean()\n\nvalidate()            // undefined\nvalidate(0)           // TypeError\nvalidate(true)        // true\n```\n\n### Schema.is(constructor)\n\nAssert that the value is an instance of the given constructor.\n\n```js\nconst validate = Schema.is(RegExp)\n\nvalidate()            // undefined\nvalidate(/foo/)       // /foo/\nvalidate('foo')       // TypeError\n```\n\n### Schema.array(inner)\n\nAssert that the value is an array of `inner`. The default value will be `[]` if not specified.\n\n```js\nconst validate = Schema.array(Schema.number())\n\nvalidate()                  // []\nvalidate(0)                 // TypeError\nvalidate([0, 1])            // [0, 1]\nvalidate([0, '1'])          // TypeError\n```\n\n### Schema.dict(inner)\n\nAssert that the value is a dictionary of `inner`. The default value will be `{}` if not specified.\n\n```js\nconst validate = Schema.dict(Schema.number())\n\nvalidate()                  // {}\nvalidate(0)                 // TypeError\nvalidate({ a: 0, b: 1 })    // { a: 0, b: 1 }\nvalidate({ a: 0, b: '1' })  // TypeError\n```\n\n### Schema.tuple(list)\n\nAssert that the value is a tuple whose each element is of corresponding subtype. The default value will be `[]` if not specified.\n\n```js\nconst validate = Schema.tuple([\n  Schema.number(),\n  Schema.string(),\n])\n\nvalidate()                  // []\nvalidate([0])               // { a: 0 }\nvalidate([0, 1])            // TypeError\nvalidate([0, '1'])          // [0, '1']\n```\n\n### Schema.object(dict)\n\nAssert that the value is an object whose each property is of corresponding subtype. The default value will be `{}` if not specified.\n\n```js\nconst validate = Schema.object({\n  a: Schema.number(),\n  b: Schema.string(),\n})\n\nvalidate()                  // {}\nvalidate({ a: 0 })          // { a: 0 }\nvalidate({ a: 0, b: 1 })    // TypeError\nvalidate({ a: 0, b: '1' })  // { a: 0, b: '1' }\n```\n\n### Schema.union(list)\n\nAssert that the value is one of the specified types.\n\n```js\nconst validate = Schema.union([\n  Schema.number(),\n  Schema.string(),\n])\n\nvalidate()                  // undefined\nvalidate(0)                 // 0\nvalidate('1')               // '1'\nvalidate(true)              // TypeError\n```\n\n### Schema.intersect(list)\n\nAssert that the value should match each specified type.\n\n```js\nconst validate = Schema.intersect([\n  Schema.object({ a: Schema.string().required() }),\n  Schema.object({ b: Schema.number().default(0) }),\n])\n\nvalidate()                  // TypeError\nvalidate({ a: '' })         // { a: '', b: 0 }\nvalidate({ a: '', b: 1 })   // { a: '', b: 1 }\nvalidate({ a: '', b: '2' }) // TypeError\n```\n\n### Schema.transform(inner, callback)\n\nAssert that the value is of the specified subtype and then transformed by `callback`.\n\n```js\nconst validate = Schema.transform(Schema.number().default(0), n => n + 1)\n\nvalidate()                  // 1\nvalidate('0')               // TypeError\nvalidate(10)                // 11\n```\n\n## Instance Methods\n\nNote: `default` and `required` are mutually exclusive.\n\n### schema.required()\n\nAssert that the value is not nullable.\n\n### schema.default(value)\n\nSet the fallback value when nullable.\n\n### schema.description(text)\n\nSet the description of the schema.\n\n## Shorthand Syntax\n\nSome shorthand syntax is available for inner types.\n\n- `undefined` -> `Schema.any()`\n- `String` -> `Schema.string()`\n- `Number` -> `Schema.number()`\n- `Boolean` -> `Schema.boolean()`\n- `1` -> `Schema.const(1)` (only for primitive types)\n- `Date` -> `Schema.is(Date)`\n\n```js\nSchema.array(String)        // Schema.array(Schema.string())\nSchema.dict(RegExp)         // Schema.dict(Schema.is(RegExp))\nSchema.union([1, 2])        // Schema.union([Schema.const(1), Schema.const(2)])\n```\n\nYou can also use `Schema.from()` to get the inferred schema from a shorthand value.\n\n```js\nSchema.from()               // Schema.any()\nSchema.from(Date)           // Schema.is(Date)\nSchema.from('foo')          // Schema.const('foo')\n```\n\n## Advanced Examples\n\nHere are some examples which demonstrate how to define advanced types.\n\n### Enumeration\n\n```js\nconst Enum = Schema.union(['red', 'blue'])\n\nEnum('red')                 // 'red'\nEnum('blue')                // 'blue'\nEnum('green')               // TypeError\n```\n\n### ToString\n\n```js\nconst ToString = Schema.transform(Schema.any(), v => String(v))\n\nToString('')                // ''\nToString(0)                 // '0'\nToString({})                // '{}'\n```\n\n### Listable\n\n```js\nconst Listable = Schema.union([\n  Schema.array(Number),\n  Schema.transform(Number, n => [n]),\n]).default([])\n\nListable()                  // []\nListable(0)                 // [0]\nListable([1, 2])            // [1, 2]\n```\n\n### Alias\n\n```js\nconst Config = Schema.dict(Number, Schema.union([\n  'foo',\n  Schema.transform('bar', () => 'foo'),\n]))\n\nConfig({ foo: 1 })          // { foo: 1 }\nConfig({ bar: 2 })          // { foo: 2 }\nConfig({ bar: '3' })        // TypeError\n```\n\n## Extensibility\n\n## Serializability\n\n```js\nconst schema1 = Schema.object({\n  foo: Schema.string(),\n  bar: Schema.number(),\n})\n\n// should have the same effect as schema1\nconst schema2 = new Schema(JSON.parse(JSON.stringify(schema1)))\n```\n","readmeFilename":"README.md","_rev":"1-c58d68efc641037d8294f9fd5f385ac0"}