@decs/typeschema
Advanced tools
Comparing version 0.7.4 to 0.8.0
@@ -18,4 +18,4 @@ import { SchemaObject } from 'ajv'; | ||
type Schema = { | ||
[K in keyof TypeSchemaRegistry]: IfDefined<InferSchema<TypeSchemaRegistry[K], any>>; | ||
}[keyof TypeSchemaRegistry]; | ||
[K in keyof Registry]: IfDefined<InferSchema<Registry[K], any>>; | ||
}[keyof Registry]; | ||
interface Resolver<TSchema = unknown> { | ||
@@ -122,3 +122,3 @@ type: unknown; | ||
interface TypeSchemaRegistry { | ||
type Registry = { | ||
ajv: AjvResolver; | ||
@@ -137,11 +137,11 @@ arktype: ArkTypeResolver; | ||
zod: ZodResolver; | ||
} | ||
}; | ||
type Infer<TSchema extends Schema> = UnknownIfNever<{ | ||
[K in keyof TypeSchemaRegistry]: IfDefined<InferOutput<TypeSchemaRegistry[K], TSchema>>; | ||
}[keyof TypeSchemaRegistry]>; | ||
[K in keyof Registry]: IfDefined<InferOutput<Registry[K], TSchema>>; | ||
}[keyof Registry]>; | ||
type InferIn<TSchema extends Schema> = UnknownIfNever<{ | ||
[K in keyof TypeSchemaRegistry]: IfDefined<InferInput<TypeSchemaRegistry[K], TSchema>>; | ||
}[keyof TypeSchemaRegistry]>; | ||
[K in keyof Registry]: IfDefined<InferInput<Registry[K], TSchema>>; | ||
}[keyof Registry]>; | ||
export { Infer, InferIn, Schema, ValidationIssue, assert, createAssert, validate }; |
@@ -1,1 +0,1 @@ | ||
"use strict";var z=Object.create;var d=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var E=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty;var M=(t,e)=>{for(var r in e)d(t,r,{get:e[r],enumerable:!0})},f=(t,e,r,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of N(e))!K.call(t,o)&&o!==r&&d(t,o,{get:()=>e[o],enumerable:!(a=J(e,o))||a.enumerable});return t};var p=(t,e,r)=>(r=t!=null?z(E(t)):{},f(e||!t||!t.__esModule?d(r,"default",{value:t,enumerable:!0}):r,t)),Z=t=>f(d({},"__esModule",{value:!0}),t);var Ve={};M(Ve,{ValidationIssue:()=>s,assert:()=>y,createAssert:()=>j,validate:()=>l});module.exports=Z(Ve);function m(t){let e,r=async()=>(e===void 0&&(e=await t()),e);return r.clear=()=>e=void 0,r}function h(t){let e=new Map,r=async a=>(e.has(a)||e.set(a,await t(a)),e.get(a));return r.clear=()=>e.clear(),r}function n(t){return Symbol.for("TypeBox.Kind")in t}function i(t){return typeof t=="object"&&!("validate"in t)&&!("parse"in t)&&!("kind"in t)&&!n(t)}var _=[];function v(t){let e=h(async r=>{let a=await Promise.all(t.map(o=>o(r)).filter(Boolean));if(a.length===0)throw new Error("Missing adapters for schema: "+r);if(a.length>1)throw new Error("Conflicting adapters for schema: "+r);return a[0]});return _.push(e),e}var D=m(async()=>{let{default:t}=await require("ajv");return{ajv:new t}}),Y=t=>e=>i(e)?t(e):void 0,S=Y(async t=>{let{ajv:e}=await D(),r=e.compile(t);return async a=>r(a)?{data:a}:{issues:(r.errors??[]).map(({message:o,schemaPath:c})=>new s(o??"",[c]))}});var W=t=>e=>"infer"in e&&!n(e)&&!i(e)?t(e):void 0,x=W(async t=>async e=>{let r=t(e);return r.problems==null?{data:r.data}:{issues:Array.from(r.problems).map(({message:a,path:o})=>new s(a,o))}});var q=t=>e=>typeof e=="function"&&!("assert"in e)?t(e):void 0,T=q(async t=>async e=>{try{return{data:await t(e)}}catch(r){if(r instanceof Error)return{issues:[new s(r.message)]};throw r}});var H=m(async()=>{let{validate:t}=await require("@deepkit/type");return{validate:t}}),L=t=>e=>"kind"in e&&!n(e)&&!i(e)?t(e):void 0,V=L(async t=>{let{validate:e}=await H();return async r=>{let a=e(r,t);return a.length===0?{data:r}:{issues:a.map(({message:o,path:c})=>new s(o,[c]))}}});var X=m(async()=>{let{isRight:t}=await require("fp-ts/Either");return{isRight:t}}),$=t=>e=>"encode"in e&&!n(e)&&!i(e)?t(e):void 0,w=$(async t=>{let{isRight:e}=await X();return async r=>{let a=t.decode(r);return e(a)?{data:a.right}:{issues:a.left.map(({message:o,context:c})=>new s(o??"",c.map(({key:u})=>u)))}}});var te=t=>e=>"_flags"in e&&!n(e)&&!i(e)?t(e):void 0,R=te(async t=>async e=>{let r=t.validate(e);return r.error==null?{data:r.value}:{issues:r.error.details.map(({message:a,path:o})=>new s(a,o))}});var ae=m(async()=>{let{ArgumentError:t,default:e}=await require("ow");return{ArgumentError:t,ow:e}}),oe=t=>e=>"context"in e&&!n(e)&&!i(e)?t(e):void 0,C=oe(async t=>{let{ow:e,ArgumentError:r}=await ae(),a=e.create(t);return async o=>{try{return a(o),{data:o}}catch(c){if(c instanceof r)return{issues:Array.from(c.validationErrors.values()).flatMap(u=>Array.from(u).map(P=>new s(P)))};throw c}}});var ne=t=>e=>"reflect"in e&&!n(e)&&!i(e)?t(e):void 0,I=ne(async t=>async e=>{let r=t.validate(e);return r.success?{data:r.value}:{issues:[new s(r.message)]}});var ce=t=>e=>"refiner"in e&&!n(e)&&!i(e)?t(e):void 0,k=ce(async t=>async e=>{let r=t.validate(e,{coerce:!0});if(r[0]==null)return{data:r[1]};let{message:a,path:o}=r[0];return{issues:[new s(a,o)]}});var pe=m(async()=>{let{TypeCompiler:t}=await require("@sinclair/typebox/compiler");return{TypeCompiler:t}}),ue=t=>e=>n(e)?t(e):void 0,b=ue(async t=>{let{TypeCompiler:e}=await pe(),r=e.Compile(t);return async a=>r.Check(a)?{data:a}:{issues:[...r.Errors(a)].map(({message:o,path:c})=>new s(o,[c]))}});var le=m(async()=>{let{safeParseAsync:t}=await require("valibot");return{safeParseAsync:t}}),ye=t=>e=>"async"in e&&!n(e)&&!i(e)?t(e):void 0,A=ye(async t=>{let{safeParseAsync:e}=await le();return async r=>{let a=await e(t,r);return a.success?{data:a.data}:{issues:a.error.issues.map(({message:o,path:c})=>new s(o,c?.map(({key:u})=>u)))}}});var he=m(async()=>{let{ValidationError:t}=await require("yup");return{ValidationError:t}}),ve=t=>e=>"__isYupSchema__"in e&&!n(e)&&!i(e)?t(e):void 0,g=ve(async t=>{let{ValidationError:e}=await he();return async r=>{try{return{data:await t.validate(r,{strict:!0})}}catch(a){if(a instanceof e){let{message:o,path:c}=a;return{issues:[new s(o,c!=null&&c!==""?[c]:void 0)]}}throw a}}});var xe=t=>e=>"_def"in e&&!n(e)&&!i(e)?t(e):void 0,O=xe(async t=>async e=>{let r=await t.safeParseAsync(e);return r.success?{data:r.data}:{issues:r.error.issues.map(({message:a,path:o})=>new s(a,o))}});var B=v([S,x,T,V,w,R,C,I,k,b,A,g,O]);var s=class extends Error{constructor(r,a){super(r);this.path=a}};async function l(t,e){return(await B(t))(e)}async function y(t,e){let r=await l(t,e);if("issues"in r)throw new AggregateError(r.issues,"Assertion failed");return r.data}function j(t){return async e=>y(t,e)}0&&(module.exports={ValidationIssue,assert,createAssert,validate}); | ||
"use strict";var P=Object.create;var d=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var N=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var M=(t,e)=>{for(var r in e)d(t,r,{get:e[r],enumerable:!0})},f=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of J(e))!E.call(t,s)&&s!==r&&d(t,s,{get:()=>e[s],enumerable:!(o=z(e,s))||o.enumerable});return t};var p=(t,e,r)=>(r=t!=null?P(N(t)):{},f(e||!t||!t.__esModule?d(r,"default",{value:t,enumerable:!0}):r,t)),K=t=>f(d({},"__esModule",{value:!0}),t);var Se={};M(Se,{ValidationIssue:()=>a,assert:()=>y,createAssert:()=>B,validate:()=>l});module.exports=K(Se);function h(t){return async e=>{let r=await Promise.all(t.map(o=>o(e)).filter(Boolean));if(r.length===0)throw new Error("Missing adapters for schema: "+e);if(r.length>1)throw new Error("Conflicting adapters for schema: "+e);return r[0]}}function m(t){let e,r=async()=>(e===void 0&&(e=await t()),e);return r.clear=()=>e=void 0,r}function n(t){return Symbol.for("TypeBox.Kind")in t}function i(t){return typeof t=="object"&&!("validate"in t)&&!("parse"in t)&&!("kind"in t)&&!n(t)}var Z=m(async()=>{let{default:t}=await require("ajv");return{ajv:new t}}),_=t=>e=>i(e)?t(e):void 0,v=_(async t=>{let{ajv:e}=await Z(),r=e.compile(t);return async o=>r(o)?{data:o}:{issues:(r.errors??[]).map(({message:s,schemaPath:c})=>new a(s??"",[c]))}});var Y=t=>e=>"infer"in e&&!n(e)&&!i(e)?t(e):void 0,x=Y(async t=>async e=>{let r=t(e);return r.problems==null?{data:r.data}:{issues:Array.from(r.problems).map(({message:o,path:s})=>new a(o,s))}});var U=t=>e=>typeof e=="function"&&!("assert"in e)?t(e):void 0,S=U(async t=>async e=>{try{return{data:await t(e)}}catch(r){if(r instanceof Error)return{issues:[new a(r.message)]};throw r}});var q=m(async()=>{let{validate:t}=await require("@deepkit/type");return{validate:t}}),G=t=>e=>"kind"in e&&!n(e)&&!i(e)?t(e):void 0,T=G(async t=>{let{validate:e}=await q();return async r=>{let o=e(r,t);return o.length===0?{data:r}:{issues:o.map(({message:s,path:c})=>new a(s,[c]))}}});var L=m(async()=>{let{isRight:t}=await require("fp-ts/Either");return{isRight:t}}),Q=t=>e=>"encode"in e&&!n(e)&&!i(e)?t(e):void 0,V=Q(async t=>{let{isRight:e}=await L();return async r=>{let o=t.decode(r);return e(o)?{data:o.right}:{issues:o.left.map(({message:s,context:c})=>new a(s??"",c.map(({key:u})=>u)))}}});var $=t=>e=>"_flags"in e&&!n(e)&&!i(e)?t(e):void 0,w=$(async t=>async e=>{let r=t.validate(e);return r.error==null?{data:r.value}:{issues:r.error.details.map(({message:o,path:s})=>new a(o,s))}});var te=m(async()=>{let{ArgumentError:t,default:e}=await require("ow");return{ArgumentError:t,ow:e}}),re=t=>e=>"context"in e&&!n(e)&&!i(e)?t(e):void 0,R=re(async t=>{let{ow:e,ArgumentError:r}=await te(),o=e.create(t);return async s=>{try{return o(s),{data:s}}catch(c){if(c instanceof r)return{issues:Array.from(c.validationErrors.values()).flatMap(u=>Array.from(u).map(j=>new a(j)))};throw c}}});var ae=t=>e=>"reflect"in e&&!n(e)&&!i(e)?t(e):void 0,C=ae(async t=>async e=>{let r=t.validate(e);return r.success?{data:r.value}:{issues:[new a(r.message)]}});var ne=t=>e=>"refiner"in e&&!n(e)&&!i(e)?t(e):void 0,I=ne(async t=>async e=>{let r=t.validate(e,{coerce:!0});if(r[0]==null)return{data:r[1]};let{message:o,path:s}=r[0];return{issues:[new a(o,s)]}});var ce=m(async()=>{let{TypeCompiler:t}=await require("@sinclair/typebox/compiler");return{TypeCompiler:t}}),me=t=>e=>n(e)?t(e):void 0,k=me(async t=>{let{TypeCompiler:e}=await ce(),r=e.Compile(t);return async o=>r.Check(o)?{data:o}:{issues:[...r.Errors(o)].map(({message:s,path:c})=>new a(s,[c]))}});var ue=m(async()=>{let{safeParseAsync:t}=await require("valibot");return{safeParseAsync:t}}),de=t=>e=>"async"in e&&!n(e)&&!i(e)?t(e):void 0,b=de(async t=>{let{safeParseAsync:e}=await ue();return async r=>{let o=await e(t,r);return o.success?{data:o.data}:{issues:o.error.issues.map(({message:s,path:c})=>new a(s,c?.map(({key:u})=>u)))}}});var ye=m(async()=>{let{ValidationError:t}=await require("yup");return{ValidationError:t}}),fe=t=>e=>"__isYupSchema__"in e&&!n(e)&&!i(e)?t(e):void 0,A=fe(async t=>{let{ValidationError:e}=await ye();return async r=>{try{return{data:await t.validate(r,{strict:!0})}}catch(o){if(o instanceof e){let{message:s,path:c}=o;return{issues:[new a(s,c!=null&&c!==""?[c]:void 0)]}}throw o}}});var ve=t=>e=>"_def"in e&&!n(e)&&!i(e)?t(e):void 0,g=ve(async t=>async e=>{let r=await t.safeParseAsync(e);return r.success?{data:r.data}:{issues:r.error.issues.map(({message:o,path:s})=>new a(o,s))}});var O=h([v,x,S,T,V,w,R,C,I,k,b,A,g]);var a=class extends Error{constructor(r,o){super(r);this.path=o}};async function l(t,e){return(await O(t))(e)}async function y(t,e){let r=await l(t,e);if("issues"in r)throw new AggregateError(r.issues,"Assertion failed");return r.data}function B(t){return async e=>y(t,e)}0&&(module.exports={ValidationIssue,assert,createAssert,validate}); |
{ | ||
"name": "@decs/typeschema", | ||
"version": "0.7.4", | ||
"version": "0.8.0", | ||
"description": "Universal adapter for schema validation", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -25,3 +25,3 @@ <div id="header"> | ||
- π Decouple from validation libraries | ||
- π Tiny client footprint | ||
- π Tiny client footprint, tree-shakable | ||
- β¨ Easy-to-use, minimal API | ||
@@ -81,22 +81,21 @@ | ||
| Project | Popularity | Example schema | Support | | ||
| -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ | ------- | | ||
| [zod](https://zod.dev) | <a href="https://github.com/colinhacks/zod" rel="nofollow"><img src="https://img.shields.io/github/stars/colinhacks/zod?style=social" alt="GitHub stars"></a> | `z.string()` | β | | ||
| [yup](https://github.com/jquense/yup) | <a href="https://github.com/jquense/yup" rel="nofollow"><img src="https://img.shields.io/github/stars/jquense/yup?style=social" alt="GitHub stars"></a> | `string()` | β | | ||
| [joi](https://joi.dev) | <a href="https://github.com/hapijs/joi" rel="nofollow"><img src="https://img.shields.io/github/stars/hapijs/joi?style=social" alt="GitHub stars"></a> | `Joi.string()` | β [^1] | | ||
| [ajv](https://ajv.js.org) | <a href="https://github.com/ajv-validator/ajv" rel="nofollow"><img src="https://img.shields.io/github/stars/ajv-validator/ajv?style=social" alt="GitHub stars"></a> | `{type: "string"}` | β [^1] | | ||
| [superstruct](https://docs.superstructjs.org) | <a href="https://github.com/ianstormtaylor/superstruct" rel="nofollow"><img src="https://img.shields.io/github/stars/ianstormtaylor/superstruct?style=social" alt="GitHub stars"></a> | `string()` | β [^2] | | ||
| [io-ts](https://gcanti.github.io/io-ts) | <a href="https://github.com/gcanti/io-ts" rel="nofollow"><img src="https://img.shields.io/github/stars/gcanti/io-ts?style=social" alt="GitHub stars"></a> | `t.string` | β | | ||
| [ow](https://sindresorhus.com/ow) | <a href="https://github.com/sindresorhus/ow" rel="nofollow"><img src="https://img.shields.io/github/stars/sindresorhus/ow?style=social" alt="GitHub stars"></a> | `ow.string` | β [^3] | | ||
| [typia](https://typia.io) | <a href="https://github.com/samchon/typia" rel="nofollow"><img src="https://img.shields.io/github/stars/samchon/typia?style=social" alt="GitHub stars"></a> | `typia.createAssert<string>()` | β | | ||
| [typebox](https://github.com/sinclairzx81/typebox) | <a href="https://github.com/sinclairzx81/typebox" rel="nofollow"><img src="https://img.shields.io/github/stars/sinclairzx81/typebox?style=social" alt="GitHub stars"></a> | `Type.String()` | β | | ||
| [deepkit](https://deepkit.io) | <a href="https://github.com/deepkit/deepkit-framework" rel="nofollow"><img src="https://img.shields.io/github/stars/deepkit/deepkit-framework?style=social" alt="GitHub stars"></a> | `typeOf<string>()` | β [^1] | | ||
| [runtypes](https://github.com/pelotom/runtypes) | <a href="https://github.com/pelotom/runtypes" rel="nofollow"><img src="https://img.shields.io/github/stars/pelotom/runtypes?style=social" alt="GitHub stars"></a> | `String` | β | | ||
| [arktype](https://arktype.io) | <a href="https://github.com/arktypeio/arktype" rel="nofollow"><img src="https://img.shields.io/github/stars/arktypeio/arktype?style=social" alt="GitHub stars"></a> | `type('string')` | β | | ||
| [valibot](https://valibot.dev) | <a href="https://github.com/fabian-hiller/valibot" rel="nofollow"><img src="https://img.shields.io/github/stars/fabian-hiller/valibot?style=social" alt="GitHub stars"></a> | `string()` | β | | ||
| Project | Popularity | Example schema | Validation | `Infer` | `InferIn` | | ||
|----------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------|------------|---------|-----------| | ||
| [zod](https://zod.dev) | <a href="https://github.com/colinhacks/zod" rel="nofollow"><img src="https://img.shields.io/github/stars/colinhacks/zod?style=social" alt="GitHub stars"></a> | `z.string()` | β | β | β | | ||
| [yup](https://github.com/jquense/yup) | <a href="https://github.com/jquense/yup" rel="nofollow"><img src="https://img.shields.io/github/stars/jquense/yup?style=social" alt="GitHub stars"></a> | `string()` | β | β | β | | ||
| [joi](https://joi.dev) | <a href="https://github.com/hapijs/joi" rel="nofollow"><img src="https://img.shields.io/github/stars/hapijs/joi?style=social" alt="GitHub stars"></a> | `Joi.string()` | β | β | β | | ||
| [ajv](https://ajv.js.org) | <a href="https://github.com/ajv-validator/ajv" rel="nofollow"><img src="https://img.shields.io/github/stars/ajv-validator/ajv?style=social" alt="GitHub stars"></a> | `{type: "string"}` | β | β | β | | ||
| [superstruct](https://docs.superstructjs.org) | <a href="https://github.com/ianstormtaylor/superstruct" rel="nofollow"><img src="https://img.shields.io/github/stars/ianstormtaylor/superstruct?style=social" alt="GitHub stars"></a> | `string()` | β | β | β | | ||
| [io-ts](https://gcanti.github.io/io-ts) | <a href="https://github.com/gcanti/io-ts" rel="nofollow"><img src="https://img.shields.io/github/stars/gcanti/io-ts?style=social" alt="GitHub stars"></a> | `t.string` | β | β | β | | ||
| [ow](https://sindresorhus.com/ow)[^1] | <a href="https://github.com/sindresorhus/ow" rel="nofollow"><img src="https://img.shields.io/github/stars/sindresorhus/ow?style=social" alt="GitHub stars"></a> | `ow.string` | β | β | β | | ||
| [typia](https://typia.io) | <a href="https://github.com/samchon/typia" rel="nofollow"><img src="https://img.shields.io/github/stars/samchon/typia?style=social" alt="GitHub stars"></a> | `typia.createAssert<string>()` | β | β | β | | ||
| [typebox](https://github.com/sinclairzx81/typebox) | <a href="https://github.com/sinclairzx81/typebox" rel="nofollow"><img src="https://img.shields.io/github/stars/sinclairzx81/typebox?style=social" alt="GitHub stars"></a> | `Type.String()` | β | β | β | | ||
| [deepkit](https://deepkit.io) | <a href="https://github.com/deepkit/deepkit-framework" rel="nofollow"><img src="https://img.shields.io/github/stars/deepkit/deepkit-framework?style=social" alt="GitHub stars"></a> | `typeOf<string>()` | β | β | β | | ||
| [valibot](https://valibot.dev) | <a href="https://github.com/fabian-hiller/valibot" rel="nofollow"><img src="https://img.shields.io/github/stars/fabian-hiller/valibot?style=social" alt="GitHub stars"></a> | `string()` | β | β | β | | ||
| [runtypes](https://github.com/pelotom/runtypes) | <a href="https://github.com/pelotom/runtypes" rel="nofollow"><img src="https://img.shields.io/github/stars/pelotom/runtypes?style=social" alt="GitHub stars"></a> | `String` | β | β | β | | ||
| [arktype](https://arktype.io) | <a href="https://github.com/arktypeio/arktype" rel="nofollow"><img src="https://img.shields.io/github/stars/arktypeio/arktype?style=social" alt="GitHub stars"></a> | `type('string')` | β | β | β | | ||
[^1]: Type inference is not yet supported for [joi](https://joi.dev), [ajv](https://ajv.js.org), and [deepkit](https://deepkit.io) | ||
[^2]: Input type inference is not yet supported for [superstruct](https://docs.superstructjs.org) | ||
[^3]: For [ow](https://sindresorhus.com/ow), only v0.28.2 is supported (sindresorhus/ow#248) | ||
[^1]: For [ow](https://sindresorhus.com/ow), only v0.28.2 is supported (sindresorhus/ow#248) | ||
Custom validations are also supported: | ||
@@ -103,0 +102,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
40574
160
175