@hedystia/validations 1.4.0 → 1.4.2

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 CHANGED
@@ -90,7 +90,7 @@ type InferSchema<S> = S extends BaseSchema<any, infer O> ? O : S extends "string
90
90
  type SchemaDefinition = SchemaLike;
91
91
  interface Schema<I, O> extends StandardSchemaV1<I, O> {
92
92
  optional(): OptionalSchema<I, O | undefined>;
93
- enum<Values extends readonly [O, ...O[]]>(values: Values): EnumSchema<I, Values[number]>;
93
+ enum<V extends O & (string | number | boolean), Values extends readonly [V, ...V[]]>(values: Values): UnionSchema<I, Values[number]>;
94
94
  array(): ArraySchema<I, O[]>;
95
95
  instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
96
96
  jsonSchema: any;
@@ -105,7 +105,7 @@ declare abstract class BaseSchema<I, O> implements Schema<I, O> {
105
105
  protected _coerce: boolean;
106
106
  coerce(): this;
107
107
  optional(): OptionalSchema<I, O | undefined>;
108
- enum<Values extends readonly [O, ...O[]]>(values: Values): EnumSchema<I, Values[number]>;
108
+ enum<V extends O & (string | number | boolean), Values extends readonly [V, ...V[]]>(values: Values): UnionSchema<I, Values[number]>;
109
109
  array(): ArraySchema<I, O[]>;
110
110
  instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
111
111
  }
@@ -155,10 +155,10 @@ declare class AnySchemaType extends BaseSchema<unknown, any> {
155
155
  readonly type: SchemaPrimitive;
156
156
  readonly "~standard": StandardSchemaV1.Props<unknown, any>;
157
157
  }
158
- declare class LiteralSchema<T extends string | number | boolean> extends BaseSchema<unknown, T> {
158
+ declare class LiteralSchema<I, T extends string | number | boolean> extends BaseSchema<I, T> {
159
159
  private readonly value;
160
160
  constructor(value: T);
161
- readonly "~standard": StandardSchemaV1.Props<unknown, T>;
161
+ readonly "~standard": StandardSchemaV1.Props<I, T>;
162
162
  }
163
163
  declare class OptionalSchema<I, O> extends BaseSchema<I, O | undefined> {
164
164
  private readonly innerSchema;
@@ -175,12 +175,6 @@ declare class UnionSchema<I, O> extends BaseSchema<I, O> {
175
175
  constructor(...schemas: Schema<I, any>[]);
176
176
  readonly "~standard": StandardSchemaV1.Props<I, O>;
177
177
  }
178
- declare class EnumSchema<I, O> extends BaseSchema<I, O> {
179
- private readonly innerSchema;
180
- private readonly values;
181
- constructor(schema: Schema<I, any>, values: readonly O[]);
182
- readonly "~standard": StandardSchemaV1.Props<I, O>;
183
- }
184
178
  declare class ArraySchema<I, O extends any[]> extends BaseSchema<I, O> {
185
179
  private readonly innerSchema;
186
180
  constructor(schema: Schema<I, O[number]>);
@@ -232,9 +226,9 @@ declare const h: {
232
226
  /**
233
227
  * Create literal schema type
234
228
  * @param {T} value - Literal value
235
- * @returns {LiteralSchema<T>} Literal schema type
229
+ * @returns {LiteralSchema<unknown, T>} Literal schema type
236
230
  */
237
- literal: <T extends string | number | boolean>(value: T) => LiteralSchema<T>;
231
+ literal: <T extends string | number | boolean>(value: T) => LiteralSchema<unknown, T>;
238
232
  /**
239
233
  * Create object schema type
240
234
  * @param {S} [schemaDef] - Schema definition
@@ -248,11 +242,11 @@ declare const h: {
248
242
  */
249
243
  array: <S extends AnySchema>(schema: S) => ArraySchema<unknown, SchemaType<S>[]>;
250
244
  /**
251
- * Create enum schema type
252
- * @param {T} values - Enum values
253
- * @returns {EnumSchema<unknown, T[number]>} Enum schema type
245
+ * Create enum schema type from a list of string, number or boolean values.
246
+ * @param {Values} values - An array of literal values.
247
+ * @returns {UnionSchema<unknown, Values[number]>} A schema that validates against one of the provided literal values.
254
248
  */
255
- enum: <T, Values extends readonly [T, ...T[]]>(values: Values) => EnumSchema<unknown, Values[number]>;
249
+ enum: <T extends string | number | boolean, Values extends readonly [T, ...T[]]>(values: Values) => UnionSchema<unknown, Values[number]>;
256
250
  /**
257
251
  * Create optional schema type
258
252
  * @param {S} schema - Schema
@@ -305,4 +299,4 @@ declare const h: {
305
299
  toStandard: typeof toStandard;
306
300
  };
307
301
 
308
- export { type AnySchema, AnySchemaType, ArraySchema, BaseSchema, BooleanSchemaType, EnumSchema, InstanceOfSchema, LiteralSchema, NullSchemaType, NumberSchemaType, ObjectSchemaType, OptionalSchema, StringSchemaType, UnionSchema, h };
302
+ export { type AnySchema, AnySchemaType, ArraySchema, BaseSchema, BooleanSchemaType, InstanceOfSchema, LiteralSchema, NullSchemaType, NumberSchemaType, ObjectSchemaType, OptionalSchema, StringSchemaType, UnionSchema, h };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var k=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var V=(a,e)=>{for(var n in e)k(a,n,{get:e[n],enumerable:!0})},P=(a,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of j(e))!_.call(a,t)&&t!==n&&k(a,t,{get:()=>e[t],enumerable:!(i=T(e,t))||i.enumerable});return a};var R=a=>P(k({},"__esModule",{value:!0}),a);var K={};V(K,{AnySchemaType:()=>f,ArraySchema:()=>x,BaseSchema:()=>r,BooleanSchemaType:()=>p,EnumSchema:()=>v,InstanceOfSchema:()=>I,LiteralSchema:()=>g,NullSchemaType:()=>b,NumberSchemaType:()=>S,ObjectSchemaType:()=>l,OptionalSchema:()=>u,StringSchemaType:()=>d,UnionSchema:()=>w,h:()=>m});module.exports=R(K);var r=class{jsonSchema={};get inferred(){return null}schema=this;_coerce=!1;coerce(){return this._coerce=!0,this}optional(){return new u(this)}enum(e){return new v(this,e)}array(){return new x(this)}instanceOf(e){return new I(this,e)}};function E(a,e){return typeof e=="string"&&a==="string"||typeof e=="number"&&a==="number"&&!Number.isNaN(e)||typeof e=="boolean"&&a==="boolean"}var d=class a extends r{type="string";_validateUUID=!1;_validateRegex=!1;_validateEmail=!1;_validatePhone=!1;_validateDomain=!1;_requireHttpOrHttps=!1;_minLength;_maxLength;constructor(){super(),this.jsonSchema={type:"string"}}primitive(){return this.type}minLength(e){let n=new a;return Object.assign(n,this),n._minLength=e,n.jsonSchema={...this.jsonSchema,minLength:e},n}maxLength(e){let n=new a;return Object.assign(n,this),n._maxLength=e,n.jsonSchema={...this.jsonSchema,maxLength:e},n}uuid(){let e=new a;return e._validateUUID=!0,e.jsonSchema={...this.jsonSchema,format:"uuid"},e}regex(e){let n=new a;return n._validateRegex=!0,n.jsonSchema={...this.jsonSchema,pattern:e.source},n}email(){let e=new a;return e._validateEmail=!0,e.jsonSchema={...this.jsonSchema,format:"email"},e}phone(){let e=new a;return e._validatePhone=!0,e.jsonSchema={...this.jsonSchema,format:"phone"},e}domain(e=!0){let n=new a;return n._validateDomain=!0,n.jsonSchema={...this.jsonSchema,format:"domain"},n._requireHttpOrHttps=e,n}"~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._minLength!==void 0&&e.length<this._minLength?{issues:[{message:`String shorter than ${this._minLength}`}]}:this._maxLength!==void 0&&e.length>this._maxLength?{issues:[{message:`String longer than ${this._maxLength}`}]}:this._validateUUID&&!this._isValidUUID(e)?{issues:[{message:"Invalid UUID format"}]}:this._validateRegex&&!this._isValidRegex(e)?{issues:[{message:"Invalid regex format"}]}:this._validateEmail&&!this._isValidEmail(e)?{issues:[{message:"Invalid email format"}]}:this._validatePhone&&!this._isValidPhone(e)?{issues:[{message:"Invalid phone number format"}]}:this._validateDomain&&!this._isValidDomain(e)?{issues:[{message:"Invalid domain format"}]}:{value:e}),types:{input:{},output:{}}};_isValidUUID(e){return/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(e)}_isValidRegex(e){return new RegExp(this.jsonSchema.pattern).test(e)}_isValidEmail(e){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)}_isValidPhone(e){return/^\+?[0-9]{7,15}$/.test(e)}_isValidDomain(e){let n=/^[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/;return this._requireHttpOrHttps&&(n=/^https?:\/\/[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/),n.test(e)}},S=class a extends r{type="number";_min;_max;constructor(){super(),this.jsonSchema={type:"number"}}primitive(){return this.type}min(e){let n=new a;return Object.assign(n,this),n._min=e,n.jsonSchema={...this.jsonSchema,minimum:e},n}max(e){let n=new a;return Object.assign(n,this),n._max=e,n.jsonSchema={...this.jsonSchema,maximum:e},n}"~standard"={version:1,vendor:"h-schema",validate:e=>{if(this._coerce&&typeof e!="number"){let n=Number(e);Number.isNaN(n)||(e=n)}return typeof e!="number"||Number.isNaN(e)?{issues:[{message:"Expected number, received "+typeof e}]}:this._min!==void 0&&e<this._min?{issues:[{message:`Number less than ${this._min}`}]}:this._max!==void 0&&e>this._max?{issues:[{message:`Number greater than ${this._max}`}]}:{value:e}},types:{input:{},output:{}}}},p=class extends r{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:{}}}},f=class extends r{type="any";"~standard"={version:1,vendor:"h-schema",validate:e=>({value:e}),types:{input:{},output:{}}}},g=class extends r{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:{}}}},u=class extends r{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:{}}}},b=class extends r{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:{}}}},w=class extends r{schemas;constructor(...e){super(),this.schemas=e,this.jsonSchema={anyOf:e.map(n=>n.jsonSchema)}}"~standard"={version:1,vendor:"h-schema",validate:async e=>{let n=[];for(let i of this.schemas){let t=await i["~standard"].validate(e);if(!("issues"in t))return{value:t.value};n.push(...t.issues)}return{issues:n}},types:{input:{},output:{}}}},v=class extends r{innerSchema;values;constructor(e,n){super(),this.innerSchema=e,this.values=n,this.jsonSchema={...e.jsonSchema,enum:n}}"~standard"={version:1,vendor:"h-schema",validate:async e=>{let n=await this.innerSchema["~standard"].validate(e);return"issues"in n?n:this.values.includes(n.value)?{value:n.value}:{issues:[{message:`Invalid enum value. Expected one of: ${this.values.join(", ")}`}]}},types:{input:{},output:{}}}},x=class extends r{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 n=await Promise.all(e.map(async(t,s)=>{let o=await this.innerSchema["~standard"].validate(t);return"issues"in o?{index:s,issues:o.issues?.map(h=>({...h,path:h.path?[s,...h.path]:[s]}))}:{index:s,value:o.value}})),i=n.filter(t=>"issues"in t);return i.length>0?{issues:i.flatMap(t=>t.issues)}:{value:n.map(t=>"value"in t?t.value:null)}},types:{input:{},output:{}}}},I=class extends r{innerSchema;classConstructor;constructor(e,n){super(),this.innerSchema=e,this.classConstructor=n,this.jsonSchema={...e.jsonSchema,instanceOf:n.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:{}}}},l=class extends r{definition;constructor(e){super(),this.definition=e;let n={},i=[];for(let t in e){let s=e[t];s instanceof u||i.push(t),typeof s=="string"?n[t]={type:s}:s instanceof r?n[t]=s.jsonSchema:typeof s=="object"&&s!==null&&(n[t]={type:"object",properties:{}})}this.jsonSchema={type:"object",properties:n,required:i.length>0?i: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 n=e,i={},t=[];for(let s in this.definition){let o=this.definition[s],h=o instanceof u;if(!(s in n)&&!h){t.push({message:`Missing required property: ${s}`,path:[s]});continue}if(s in n){if(typeof o=="string"&&o in["string","number","boolean"]){let c=o;E(c,n[s])?i[s]=n[s]:t.push({message:`Invalid type for property ${s}: expected ${c}`,path:[s]})}else if(o instanceof r){let c=await o["~standard"].validate(n[s]);"issues"in c?c.issues&&t.push(...c.issues.map(O=>({...O,path:O.path?[s,...O.path]:[s]}))):i[s]=c.value}}}return t.length>0?{issues:t}:{value:i}},types:{input:{},output:{}}}};function y(a){let e;if(a instanceof r)e=a;else if(typeof a=="string")if(a==="string")e=new d;else if(a==="number")e=new S;else if(a==="boolean")e=new p;else throw new Error("Invalid schema type provided to toStandard");else if(typeof a=="object"&&a!==null&&!Array.isArray(a))e=new l(a);else throw new Error("Invalid schema type provided to toStandard");let n={toJSONSchema:i=>i.jsonSchema};return{...e,inferred:null,"~standard":e["~standard"],jsonSchema:n.toJSONSchema(e),schema:e,optional:()=>new u(e),enum:e.enum.bind(e),array:e.array.bind(e),instanceOf:e.instanceOf.bind(e)}}var m={string:()=>new d,number:()=>new S,boolean:()=>new p,null:()=>new b,any:()=>new f,literal:a=>new g(a),object:a=>new l(a||{}),array:a=>y(a).array(),enum:a=>{let e=a[0],n;if(typeof e=="string")n=m.string();else if(typeof e=="number")n=m.number();else if(typeof e=="boolean")n=m.boolean();else throw new Error("Enum values must be primitives");return n.enum(a)},optional:a=>y(a).optional(),options:(...a)=>{let e=a.map(n=>y(n).schema);return new w(...e)},instanceOf:a=>m.object({}).instanceOf(a),uuid:()=>m.string().uuid(),regex:a=>m.string().regex(a),email:()=>m.string().email(),phone:()=>m.string().phone(),domain:(a=!0)=>m.string().domain(a),toStandard:y};0&&(module.exports={AnySchemaType,ArraySchema,BaseSchema,BooleanSchemaType,EnumSchema,InstanceOfSchema,LiteralSchema,NullSchemaType,NumberSchemaType,ObjectSchemaType,OptionalSchema,StringSchemaType,UnionSchema,h});
1
+ "use strict";var O=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var V=(t,e)=>{for(var n in e)O(t,n,{get:e[n],enumerable:!0})},_=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of T(e))!j.call(t,a)&&a!==n&&O(t,a,{get:()=>e[a],enumerable:!(r=k(e,a))||r.enumerable});return t};var P=t=>_(O({},"__esModule",{value:!0}),t);var K={};V(K,{AnySchemaType:()=>b,ArraySchema:()=>x,BaseSchema:()=>i,BooleanSchemaType:()=>p,InstanceOfSchema:()=>I,LiteralSchema:()=>l,NullSchemaType:()=>w,NumberSchemaType:()=>S,ObjectSchemaType:()=>f,OptionalSchema:()=>u,StringSchemaType:()=>d,UnionSchema:()=>y,h:()=>m});module.exports=P(K);var i=class{jsonSchema={};get inferred(){return null}schema=this;_coerce=!1;coerce(){return this._coerce=!0,this}optional(){return new u(this)}enum(e){let n=e.map(r=>new l(r));return new y(...n)}array(){return new x(this)}instanceOf(e){return new I(this,e)}};function R(t,e){return typeof e=="string"&&t==="string"||typeof e=="number"&&t==="number"&&!Number.isNaN(e)||typeof e=="boolean"&&t==="boolean"}var d=class t extends i{type="string";_validateUUID=!1;_validateRegex=!1;_validateEmail=!1;_validatePhone=!1;_validateDomain=!1;_requireHttpOrHttps=!1;_minLength;_maxLength;constructor(){super(),this.jsonSchema={type:"string"}}primitive(){return this.type}minLength(e){let n=new t;return Object.assign(n,this),n._minLength=e,n.jsonSchema={...this.jsonSchema,minLength:e},n}maxLength(e){let n=new t;return Object.assign(n,this),n._maxLength=e,n.jsonSchema={...this.jsonSchema,maxLength:e},n}uuid(){let e=new t;return e._validateUUID=!0,e.jsonSchema={...this.jsonSchema,format:"uuid"},e}regex(e){let n=new t;return n._validateRegex=!0,n.jsonSchema={...this.jsonSchema,pattern:e.source},n}email(){let e=new t;return e._validateEmail=!0,e.jsonSchema={...this.jsonSchema,format:"email"},e}phone(){let e=new t;return e._validatePhone=!0,e.jsonSchema={...this.jsonSchema,format:"phone"},e}domain(e=!0){let n=new t;return n._validateDomain=!0,n.jsonSchema={...this.jsonSchema,format:"domain"},n._requireHttpOrHttps=e,n}"~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._minLength!==void 0&&e.length<this._minLength?{issues:[{message:`String shorter than ${this._minLength}`}]}:this._maxLength!==void 0&&e.length>this._maxLength?{issues:[{message:`String longer than ${this._maxLength}`}]}:this._validateUUID&&!this._isValidUUID(e)?{issues:[{message:"Invalid UUID format"}]}:this._validateRegex&&!this._isValidRegex(e)?{issues:[{message:"Invalid regex format"}]}:this._validateEmail&&!this._isValidEmail(e)?{issues:[{message:"Invalid email format"}]}:this._validatePhone&&!this._isValidPhone(e)?{issues:[{message:"Invalid phone number format"}]}:this._validateDomain&&!this._isValidDomain(e)?{issues:[{message:"Invalid domain format"}]}:{value:e}),types:{input:{},output:{}}};_isValidUUID(e){return/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(e)}_isValidRegex(e){return new RegExp(this.jsonSchema.pattern).test(e)}_isValidEmail(e){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)}_isValidPhone(e){return/^\+?[0-9]{7,15}$/.test(e)}_isValidDomain(e){let n=/^[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/;return this._requireHttpOrHttps&&(n=/^https?:\/\/[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/),n.test(e)}},S=class t extends i{type="number";_min;_max;constructor(){super(),this.jsonSchema={type:"number"}}primitive(){return this.type}min(e){let n=new t;return Object.assign(n,this),n._min=e,n.jsonSchema={...this.jsonSchema,minimum:e},n}max(e){let n=new t;return Object.assign(n,this),n._max=e,n.jsonSchema={...this.jsonSchema,maximum:e},n}"~standard"={version:1,vendor:"h-schema",validate:e=>{if(this._coerce&&typeof e!="number"){let n=Number(e);Number.isNaN(n)||(e=n)}return typeof e!="number"||Number.isNaN(e)?{issues:[{message:"Expected number, received "+typeof e}]}:this._min!==void 0&&e<this._min?{issues:[{message:`Number less than ${this._min}`}]}:this._max!==void 0&&e>this._max?{issues:[{message:`Number greater than ${this._max}`}]}:{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:{}}}},l=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:{}}}},u=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:{}}}},w=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:{}}}},y=class extends i{schemas;constructor(...e){super(),this.schemas=e,this.jsonSchema={anyOf:e.map(n=>n.jsonSchema)}}"~standard"={version:1,vendor:"h-schema",validate:async e=>{let n=[];for(let r of this.schemas){let a=await r["~standard"].validate(e);if(!("issues"in a))return{value:a.value};n.push(...a.issues)}return{issues:n}},types:{input:{},output:{}}}},x=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 n=await Promise.all(e.map(async(a,s)=>{let o=await this.innerSchema["~standard"].validate(a);return"issues"in o?{index:s,issues:o.issues?.map(h=>({...h,path:h.path?[s,...h.path]:[s]}))}:{index:s,value:o.value}})),r=n.filter(a=>"issues"in a);return r.length>0?{issues:r.flatMap(a=>a.issues)}:{value:n.map(a=>"value"in a?a.value:null)}},types:{input:{},output:{}}}},I=class extends i{innerSchema;classConstructor;constructor(e,n){super(),this.innerSchema=e,this.classConstructor=n,this.jsonSchema={...e.jsonSchema,instanceOf:n.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:{}}}},f=class extends i{definition;constructor(e){super(),this.definition=e;let n={},r=[];for(let a in e){let s=e[a];s instanceof u||r.push(a),typeof s=="string"?n[a]={type:s}:s instanceof i?n[a]=s.jsonSchema:typeof s=="object"&&s!==null&&(n[a]={type:"object",properties:{}})}this.jsonSchema={type:"object",properties:n,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 n=e,r={},a=[];for(let s in this.definition){let o=this.definition[s],h=o instanceof u;if(!(s in n)&&!h){a.push({message:`Missing required property: ${s}`,path:[s]});continue}if(s in n){if(typeof o=="string"&&o in["string","number","boolean"]){let c=o;R(c,n[s])?r[s]=n[s]:a.push({message:`Invalid type for property ${s}: expected ${c}`,path:[s]})}else if(o instanceof i){let c=await o["~standard"].validate(n[s]);"issues"in c?c.issues&&a.push(...c.issues.map(v=>({...v,path:v.path?[s,...v.path]:[s]}))):r[s]=c.value}}}return a.length>0?{issues:a}:{value:r}},types:{input:{},output:{}}}};function g(t){let e;if(t instanceof i)e=t;else if(typeof t=="string")if(t==="string")e=new d;else if(t==="number")e=new S;else if(t==="boolean")e=new p;else throw new Error("Invalid schema type provided to toStandard");else if(typeof t=="object"&&t!==null&&!Array.isArray(t))e=new f(t);else throw new Error("Invalid schema type provided to toStandard");let n={toJSONSchema:r=>r.jsonSchema};return{...e,inferred:null,"~standard":e["~standard"],jsonSchema:n.toJSONSchema(e),schema:e,optional:()=>new u(e),enum:e.enum.bind(e),array:e.array.bind(e),instanceOf:e.instanceOf.bind(e)}}var m={string:()=>new d,number:()=>new S,boolean:()=>new p,null:()=>new w,any:()=>new b,literal:t=>new l(t),object:t=>new f(t||{}),array:t=>g(t).array(),enum:t=>{if(!t||t.length===0)throw new Error("h.enum() requires a non-empty array of values.");let e=t.map(n=>m.literal(n));return m.options(...e)},optional:t=>g(t).optional(),options:(...t)=>{let e=t.map(n=>g(n).schema);return new y(...e)},instanceOf:t=>m.object({}).instanceOf(t),uuid:()=>m.string().uuid(),regex:t=>m.string().regex(t),email:()=>m.string().email(),phone:()=>m.string().phone(),domain:(t=!0)=>m.string().domain(t),toStandard:g};0&&(module.exports={AnySchemaType,ArraySchema,BaseSchema,BooleanSchemaType,InstanceOfSchema,LiteralSchema,NullSchemaType,NumberSchemaType,ObjectSchemaType,OptionalSchema,StringSchemaType,UnionSchema,h});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hedystia/validations",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "devDependencies": {
5
5
  "@standard-schema/spec": "^1.0.0",
6
6
  "@types/bun": "latest",