@hedystia/validations 1.2.5 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -6
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,11 +4,7 @@ type SchemaPrimitive = "string" | "number" | "boolean" | "any";
|
|
|
4
4
|
interface SchemaLike {
|
|
5
5
|
[key: string]: SchemaPrimitive | SchemaLike | BaseSchema<any, any>;
|
|
6
6
|
}
|
|
7
|
-
type InferSchema<S> = S extends BaseSchema<any, infer O> ? O extends
|
|
8
|
-
[K in keyof O as undefined extends O[K] ? K : never]?: O[K];
|
|
9
|
-
} & {
|
|
10
|
-
[K in keyof O as undefined extends O[K] ? never : K]: O[K];
|
|
11
|
-
} : O : S extends "string" ? string : S extends "number" ? number : S extends "boolean" ? boolean : S extends {
|
|
7
|
+
type InferSchema<S> = S extends BaseSchema<any, infer O> ? O : S extends "string" ? string : S extends "number" ? number : S extends "boolean" ? boolean : S extends {
|
|
12
8
|
[key: string]: any;
|
|
13
9
|
} ? {
|
|
14
10
|
[K in keyof S as undefined extends InferSchema<S[K]> ? K : never]?: InferSchema<S[K]>;
|
|
@@ -116,7 +112,7 @@ declare const h: {
|
|
|
116
112
|
null: () => NullSchemaType;
|
|
117
113
|
any: () => AnySchemaType;
|
|
118
114
|
literal: <T extends string | number | boolean>(value: T) => LiteralSchema<T>;
|
|
119
|
-
object: <S extends SchemaDefinition>(schemaDef?: S) => ObjectSchemaType<InferSchema<S
|
|
115
|
+
object: <S extends SchemaDefinition>(schemaDef?: S) => ObjectSchemaType<{ [K in keyof S]: S[K] extends BaseSchema<any, infer O> ? O : S[K] extends SchemaPrimitive ? S[K] extends "string" ? string : S[K] extends "number" ? number : S[K] extends "boolean" ? boolean : unknown : S[K] extends Record<string, any> ? InferSchema<S[K]> : unknown; }>;
|
|
120
116
|
array: <S extends AnySchema>(schema: S) => ArraySchema<unknown, InferSchema<S>[]>;
|
|
121
117
|
enum: <T extends readonly [any, ...any[]]>(values: T) => EnumSchema<unknown, T[number]>;
|
|
122
118
|
optional: <S extends AnySchema>(schema: S) => OptionalSchema<unknown, InferSchema<S> | undefined>;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var w=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var V=(n,e)=>{for(var a in e)w(n,a,{get:e[a],enumerable:!0})},E=(n,e,a,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of j(e))!P.call(n,s)&&s!==a&&w(n,s,{get:()=>e[s],enumerable:!(r=T(e,s))||r.enumerable});return n};var A=n=>E(w({},"__esModule",{value:!0}),n);var C={};V(C,{h:()=>u});module.exports=A(C);var i=class{jsonSchema={};get inferred(){return null}schema=this;_coerce=!1;coerce(){return this._coerce=!0,this}optional(){return new d(this)}enum(e){return new
|
|
1
|
+
"use strict";var w=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var V=(n,e)=>{for(var a in e)w(n,a,{get:e[a],enumerable:!0})},E=(n,e,a,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of j(e))!P.call(n,s)&&s!==a&&w(n,s,{get:()=>e[s],enumerable:!(r=T(e,s))||r.enumerable});return n};var A=n=>E(w({},"__esModule",{value:!0}),n);var C={};V(C,{h:()=>u});module.exports=A(C);var i=class{jsonSchema={};get inferred(){return null}schema=this;_coerce=!1;coerce(){return this._coerce=!0,this}optional(){return new d(this)}enum(e){return new g(this,e)}array(){return new O(this)}instanceOf(e){return new x(this,e)}};function K(n,e){return typeof e=="string"&&n==="string"||typeof e=="number"&&n==="number"&&!Number.isNaN(e)||typeof e=="boolean"&&n==="boolean"}var l=class n extends i{type="string";_validateEmail=!1;_validatePhone=!1;constructor(){super(),this.jsonSchema={type:"string"}}primitive(){return this.type}email(){let e=new n;return e._validateEmail=!0,e.jsonSchema={...this.jsonSchema,format:"email"},e}phone(){let e=new n;return e._validatePhone=!0,e.jsonSchema={...this.jsonSchema,format:"phone"},e}"~standard"={version:1,vendor:"h-schema",validate:e=>(this._coerce&&typeof e!="string"&&(e=String(e)),typeof e!="string"?{issues:[{message:"Expected string, received "+typeof e}]}:this._validateEmail&&!this._isValidEmail(e)?{issues:[{message:"Invalid email format"}]}:this._validatePhone&&!this._isValidPhone(e)?{issues:[{message:"Invalid phone number format"}]}:{value:e}),types:{input:{},output:{}}};_isValidEmail(e){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)}_isValidPhone(e){return/^\+?[0-9]{7,15}$/.test(e)}},S=class extends i{type="number";constructor(){super(),this.jsonSchema={type:"number"}}primitive(){return this.type}"~standard"={version:1,vendor:"h-schema",validate:e=>{if(this._coerce&&typeof e!="number"){let a=Number(e);Number.isNaN(a)||(e=a)}return typeof e!="number"||Number.isNaN(e)?{issues:[{message:"Expected number, received "+typeof e}]}:{value:e}},types:{input:{},output:{}}}},p=class extends i{type="boolean";constructor(){super(),this.jsonSchema={type:"boolean"}}primitive(){return this.type}"~standard"={version:1,vendor:"h-schema",validate:e=>(this._coerce&&typeof e!="boolean"&&(e==="true"||e===1||e==="1"?e=!0:(e==="false"||e===0||e==="0")&&(e=!1)),typeof e!="boolean"?{issues:[{message:"Expected boolean, received "+typeof e}]}:{value:e}),types:{input:{},output:{}}}},b=class extends i{type="any";"~standard"={version:1,vendor:"h-schema",validate:e=>({value:e}),types:{input:{},output:{}}}},v=class extends i{value;constructor(e){super(),this.value=e,this.jsonSchema={const:e,type:typeof e}}"~standard"={version:1,vendor:"h-schema",validate:e=>e!==this.value?{issues:[{message:`Expected literal value ${this.value}, received ${e}`}]}:{value:e},types:{input:{},output:{}}}},d=class extends i{innerSchema;constructor(e){super(),this.innerSchema=e,this.jsonSchema={...e.jsonSchema}}"~standard"={version:1,vendor:"h-schema",validate:async e=>e==null?{value:void 0}:await this.innerSchema["~standard"].validate(e),types:{input:{},output:{}}}},I=class extends i{type="null";constructor(){super(),this.jsonSchema={type:"null"}}"~standard"={version:1,vendor:"h-schema",validate:e=>e!==null?{issues:[{message:`Expected null, received ${e===void 0?"undefined":typeof e}`}]}:{value:null},types:{input:{},output:{}}}},k=class extends i{schemas;constructor(...e){super(),this.schemas=e,this.jsonSchema={anyOf:e.map(a=>a.jsonSchema)}}"~standard"={version:1,vendor:"h-schema",validate:async e=>{let a=[];for(let r of this.schemas){let s=await r["~standard"].validate(e);if(!("issues"in s))return{value:s.value};a.push(...s.issues)}return{issues:a}},types:{input:{},output:{}}}},g=class extends i{innerSchema;values;constructor(e,a){super(),this.innerSchema=e,this.values=a,this.jsonSchema={...e.jsonSchema,enum:a}}"~standard"={version:1,vendor:"h-schema",validate:async e=>{let a=await this.innerSchema["~standard"].validate(e);return"issues"in a?a:this.values.includes(a.value)?{value:a.value}:{issues:[{message:`Invalid enum value. Expected one of: ${this.values.join(", ")}`}]}},types:{input:{},output:{}}}},O=class extends i{innerSchema;constructor(e){super(),this.innerSchema=e,this.jsonSchema={type:"array",items:e.jsonSchema}}"~standard"={version:1,vendor:"h-schema",validate:async e=>{if(!Array.isArray(e))return{issues:[{message:"Expected array, received "+typeof e}]};let a=await Promise.all(e.map(async(s,t)=>{let o=await this.innerSchema["~standard"].validate(s);return"issues"in o?{index:t,issues:o.issues?.map(m=>({...m,path:m.path?[t,...m.path]:[t]}))}:{index:t,value:o.value}})),r=a.filter(s=>"issues"in s);return r.length>0?{issues:r.flatMap(s=>s.issues)}:{value:a.map(s=>"value"in s?s.value:null)}},types:{input:{},output:{}}}},x=class extends i{innerSchema;classConstructor;constructor(e,a){super(),this.innerSchema=e,this.classConstructor=a,this.jsonSchema={...e.jsonSchema,instanceOf:a.name}}"~standard"={version:1,vendor:"h-schema",validate:async e=>e instanceof this.classConstructor?await this.innerSchema["~standard"].validate(e):{issues:[{message:`Expected instance of ${this.classConstructor.name}`}]},types:{input:{},output:{}}}},y=class extends i{definition;constructor(e){super(),this.definition=e;let a={},r=[];for(let s in e){let t=e[s];t instanceof d||r.push(s),typeof t=="string"?a[s]={type:t}:t instanceof i?a[s]=t.jsonSchema:typeof t=="object"&&t!==null&&(a[s]={type:"object",properties:{}})}this.jsonSchema={type:"object",properties:a,required:r.length>0?r:void 0}}"~standard"={version:1,vendor:"h-schema",validate:async e=>{if(typeof e!="object"||e===null||Array.isArray(e))return{issues:[{message:"Expected object, received "+(e===null?"null":Array.isArray(e)?"array":typeof e)}]};let a=e,r={},s=[];for(let t in this.definition){let o=this.definition[t],m=o instanceof d;if(!(t in a)&&!m){s.push({message:`Missing required property: ${t}`,path:[t]});continue}if(t in a){if(typeof o=="string"&&o in["string","number","boolean"]){let c=o;K(c,a[t])?r[t]=a[t]:s.push({message:`Invalid type for property ${t}: expected ${c}`,path:[t]})}else if(o instanceof i){let c=await o["~standard"].validate(a[t]);"issues"in c?c.issues&&s.push(...c.issues.map(f=>({...f,path:f.path?[t,...f.path]:[t]}))):r[t]=c.value}}}return s.length>0?{issues:s}:{value:r}},types:{input:{},output:{}}}};function h(n){let e;if(n instanceof i)e=n;else if(typeof n=="string")if(n==="string")e=new l;else if(n==="number")e=new S;else if(n==="boolean")e=new p;else throw new Error("Invalid schema type provided to toStandard");else if(typeof n=="object"&&n!==null&&!Array.isArray(n))e=new y(n);else throw new Error("Invalid schema type provided to toStandard");let a={toJSONSchema:r=>r.jsonSchema};return{...e,inferred:null,"~standard":e["~standard"],jsonSchema:a.toJSONSchema(e),schema:e,optional:()=>new d(e),enum:e.enum.bind(e),array:e.array.bind(e),instanceOf:e.instanceOf.bind(e)}}var u={string:()=>new l,number:()=>new S,boolean:()=>new p,null:()=>new I,any:()=>new b,literal:n=>new v(n),object:n=>new y(n||{}),array:n=>h(n).array(),enum:n=>{let e=n[0],a;if(typeof e=="string")a=u.string();else if(typeof e=="number")a=u.number();else if(typeof e=="boolean")a=u.boolean();else throw new Error("Enum values must be primitives");return a.enum(n)},optional:n=>h(n).optional(),options:(...n)=>{let e=n.map(a=>h(a).schema);return new k(...e)},instanceOf:n=>u.object({}).instanceOf(n),email:()=>u.string().email(),phone:()=>u.string().phone(),toStandard:h};0&&(module.exports={h});
|