@hedystia/validations 1.6.10 → 1.7.1

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
@@ -55,10 +55,39 @@ declare namespace StandardSchemaV1 {
55
55
  export { };
56
56
  }
57
57
 
58
- type SchemaPrimitive = "string" | "number" | "boolean" | "any";
59
- interface SchemaLike {
60
- [key: string]: SchemaPrimitive | SchemaLike | BaseSchema<any, any>;
58
+ /* tslint:disable */
59
+ /* eslint-disable */
60
+ declare class HSchema {
61
+ private constructor();
62
+ free(): void;
63
+ [Symbol.dispose](): void;
64
+ static string(): HSchema;
65
+ static number(): HSchema;
66
+ static boolean(): HSchema;
67
+ static any(): HSchema;
68
+ static null_type(): HSchema;
69
+ static literal(val: any): HSchema;
70
+ static object(): HSchema;
71
+ static array(item: HSchema): HSchema;
72
+ static union(schemas_arr: HSchema[]): HSchema;
73
+ static instance_of(ctor: Function, name: string): HSchema;
74
+ optional(): HSchema;
75
+ coerce(): HSchema;
76
+ min_length(n: number): HSchema;
77
+ max_length(n: number): HSchema;
78
+ uuid(): HSchema;
79
+ email(): HSchema;
80
+ regex(pattern: string): HSchema;
81
+ phone(): HSchema;
82
+ domain(require_protocol: boolean): HSchema;
83
+ min(n: number): HSchema;
84
+ max(n: number): HSchema;
85
+ add_prop(key: string, schema: HSchema): void;
86
+ validate(value: any): any;
87
+ get_json_schema(): any;
61
88
  }
89
+
90
+ type SchemaPrimitive = "string" | "number" | "boolean" | "any";
62
91
  type Simplify<T> = T extends any ? {
63
92
  [K in keyof T]: T[K];
64
93
  } : never;
@@ -87,7 +116,12 @@ type InferSchema<S> = S extends BaseSchema<any, infer O> ? O : S extends "string
87
116
  } & {
88
117
  [K in keyof S as undefined extends InferSchema<S[K]> ? never : K]: InferSchema<S[K]>;
89
118
  } : unknown;
90
- type SchemaDefinition = SchemaLike;
119
+ type SchemaDefinition = {
120
+ [key: string]: SchemaPrimitive | SchemaLike | BaseSchema<any, any>;
121
+ };
122
+ interface SchemaLike {
123
+ [key: string]: SchemaPrimitive | SchemaLike | BaseSchema<any, any>;
124
+ }
91
125
  interface Schema<I, O> extends StandardSchemaV1<I, O> {
92
126
  optional(): OptionalSchema<I, O | undefined>;
93
127
  enum<V extends O & (string | number | boolean), Values extends readonly [V, ...V[]]>(values: Values): UnionSchema<I, Values[number]>;
@@ -95,208 +129,183 @@ interface Schema<I, O> extends StandardSchemaV1<I, O> {
95
129
  instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
96
130
  jsonSchema: any;
97
131
  readonly inferred: O;
98
- schema: Schema<I, O>;
99
132
  }
100
133
  declare abstract class BaseSchema<I, O> implements Schema<I, O> {
101
- abstract readonly "~standard": StandardSchemaV1.Props<I, O>;
102
- jsonSchema: any;
134
+ protected wasmSchema: HSchema;
135
+ constructor(wasmSchema: HSchema);
136
+ get "~standard"(): StandardSchemaV1.Props<I, O>;
137
+ get jsonSchema(): any;
103
138
  get inferred(): O;
104
- schema: Schema<I, O>;
105
- protected _coerce: boolean;
106
139
  coerce(): this;
107
140
  optional(): OptionalSchema<I, O | undefined>;
108
- enum<V extends O & (string | number | boolean), Values extends readonly [V, ...V[]]>(values: Values): UnionSchema<I, Values[number]>;
109
141
  array(): ArraySchema<I, O[]>;
142
+ enum<V extends O & (string | number | boolean), Values extends readonly [V, ...V[]]>(values: Values): UnionSchema<I, Values[number]>;
110
143
  instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
111
144
  }
112
145
  declare class StringSchemaType extends BaseSchema<unknown, string> {
113
- readonly type: SchemaPrimitive;
114
- private _validateUUID;
115
- private _validateRegex;
116
- private _validateEmail;
117
- private _validatePhone;
118
- private _validateDomain;
119
- private _requireHttpOrHttps;
120
- private _minLength?;
121
- private _maxLength?;
122
- constructor();
123
- primitive(): SchemaPrimitive;
146
+ constructor(s?: HSchema);
124
147
  minLength(n: number): StringSchemaType;
125
148
  maxLength(n: number): StringSchemaType;
126
149
  uuid(): StringSchemaType;
127
- regex(regex: RegExp): StringSchemaType;
128
150
  email(): StringSchemaType;
151
+ regex(r: RegExp): StringSchemaType;
129
152
  phone(): StringSchemaType;
130
- domain(requireHttpOrHttps?: boolean): StringSchemaType;
131
- readonly "~standard": StandardSchemaV1.Props<unknown, string>;
132
- private _isValidUUID;
133
- private _isValidRegex;
134
- private _isValidEmail;
135
- private _isValidPhone;
136
- private _isValidDomain;
153
+ domain(r?: boolean): StringSchemaType;
137
154
  }
138
155
  declare class NumberSchemaType extends BaseSchema<unknown, number> {
139
- readonly type: SchemaPrimitive;
140
- private _min?;
141
- private _max?;
142
- constructor();
143
- primitive(): SchemaPrimitive;
156
+ constructor(s?: HSchema);
144
157
  min(n: number): NumberSchemaType;
145
158
  max(n: number): NumberSchemaType;
146
- readonly "~standard": StandardSchemaV1.Props<unknown, number>;
147
159
  }
148
160
  declare class BooleanSchemaType extends BaseSchema<unknown, boolean> {
149
- readonly type: SchemaPrimitive;
150
161
  constructor();
151
- primitive(): SchemaPrimitive;
152
- readonly "~standard": StandardSchemaV1.Props<unknown, boolean>;
162
+ }
163
+ declare class NullSchemaType extends BaseSchema<unknown, null> {
164
+ constructor();
153
165
  }
154
166
  declare class AnySchemaType extends BaseSchema<unknown, any> {
155
- readonly type: SchemaPrimitive;
156
- readonly "~standard": StandardSchemaV1.Props<unknown, any>;
167
+ constructor();
157
168
  }
158
169
  declare class LiteralSchema<I, T extends string | number | boolean> extends BaseSchema<I, T> {
159
- private readonly value;
170
+ readonly value: T;
160
171
  constructor(value: T);
161
- readonly "~standard": StandardSchemaV1.Props<I, T>;
162
172
  }
163
173
  declare class OptionalSchema<I, O> extends BaseSchema<I, O | undefined> {
164
- private readonly innerSchema;
165
- constructor(schema: Schema<I, O>);
166
- readonly "~standard": StandardSchemaV1.Props<I, O | undefined>;
167
- }
168
- declare class NullSchemaType extends BaseSchema<unknown, null> {
169
- readonly type = "null";
170
- constructor();
171
- readonly "~standard": StandardSchemaV1.Props<unknown, null>;
172
- }
173
- declare class UnionSchema<I, O> extends BaseSchema<I, O> {
174
- private readonly schemas;
175
- constructor(...schemas: Schema<I, any>[]);
176
- readonly "~standard": StandardSchemaV1.Props<I, O>;
174
+ readonly innerSchema: BaseSchema<any, any>;
175
+ constructor(ws: HSchema, originalSchema?: BaseSchema<any, any>);
177
176
  }
178
177
  declare class ArraySchema<I, O extends any[]> extends BaseSchema<I, O> {
179
- private readonly innerSchema;
180
- constructor(schema: Schema<I, O[number]>);
181
- readonly "~standard": StandardSchemaV1.Props<I, O>;
182
- }
183
- declare class InstanceOfSchema<I, O> extends BaseSchema<I, O> {
184
- private readonly innerSchema;
185
- private readonly classConstructor;
186
- constructor(schema: Schema<I, any>, classConstructor: new (...args: any[]) => any);
187
- readonly "~standard": StandardSchemaV1.Props<I, O>;
178
+ readonly innerSchema: BaseSchema<any, any>;
179
+ constructor(ws: HSchema, originalSchema?: BaseSchema<any, any>);
188
180
  }
189
181
  declare class ObjectSchemaType<T extends Record<string, unknown>> extends BaseSchema<unknown, T> {
190
182
  readonly definition: SchemaDefinition;
191
183
  constructor(definition: SchemaDefinition);
192
- readonly "~standard": StandardSchemaV1.Props<unknown, T>;
184
+ }
185
+ declare class UnionSchema<I, O> extends BaseSchema<I, O> {
186
+ readonly schemas: BaseSchema<any, any>[];
187
+ constructor(schemas: BaseSchema<any, any>[]);
188
+ }
189
+ declare class InstanceOfSchema<I, O> extends BaseSchema<I, O> {
190
+ readonly classConstructor: new (...args: any[]) => any;
191
+ constructor(_baseWasm: HSchema, constructor: new (...args: any[]) => any);
193
192
  }
194
193
  type AnySchema = SchemaPrimitive | BaseSchema<any, any> | SchemaDefinition;
195
- declare function toStandard<T>(schema: AnySchema): Schema<unknown, T>;
196
194
  /**
197
- * Create standard schema types
198
- * @returns {typeof h} Standard schema types
195
+ * Collection of helper functions for creating schema types.
196
+ * @returns {typeof h} Schema type helpers
199
197
  */
200
198
  declare const h: {
201
199
  /**
202
- * Create string schema type
200
+ * Create a string schema type.
203
201
  * @returns {StringSchemaType} String schema type
204
202
  */
205
203
  string: () => StringSchemaType;
206
204
  /**
207
- * Create number schema type
205
+ * Create a number schema type.
208
206
  * @returns {NumberSchemaType} Number schema type
209
207
  */
210
208
  number: () => NumberSchemaType;
211
209
  /**
212
- * Create boolean schema type
210
+ * Create a boolean schema type.
213
211
  * @returns {BooleanSchemaType} Boolean schema type
214
212
  */
215
213
  boolean: () => BooleanSchemaType;
216
214
  /**
217
- * Create null schema type
215
+ * Create a null schema type.
218
216
  * @returns {NullSchemaType} Null schema type
219
217
  */
220
218
  null: () => NullSchemaType;
221
219
  /**
222
- * Create any schema type
220
+ * Create an "any" schema type.
223
221
  * @returns {AnySchemaType} Any schema type
224
222
  */
225
223
  any: () => AnySchemaType;
226
224
  /**
227
- * Create literal schema type
228
- * @param {T} value - Literal value
229
- * @returns {LiteralSchema<unknown, T>} Literal schema type
225
+ * Create a literal schema type.
226
+ * @template T
227
+ * @param {T} val - Literal value
228
+ * @returns {LiteralSchema<T>} Literal schema type
230
229
  */
231
- literal: <T extends string | number | boolean>(value: T) => LiteralSchema<unknown, T>;
230
+ literal: <T extends string | number | boolean>(val: T) => LiteralSchema<unknown, T>;
232
231
  /**
233
- * Create object schema type
234
- * @param {S} [schemaDef] - Schema definition
232
+ * Create an object schema type.
233
+ * @template S
234
+ * @param {S} [schemaDef] - Optional schema definition
235
235
  * @returns {ObjectSchemaType<InferObject<S>>} Object schema type
236
236
  */
237
237
  object: <S extends SchemaDefinition>(schemaDef?: S) => ObjectSchemaType<InferObject<S>>;
238
238
  /**
239
- * Create array schema type
240
- * @param {S} schema - Schema
241
- * @returns {ArraySchema<unknown, InferSchema<S>[]>} Array schema type
239
+ * Create an array schema type.
240
+ * @template S
241
+ * @param {S} schema - Schema definition for array items
242
+ * @returns {ArraySchema<unknown, InferSchema<S>>} Array schema type
242
243
  */
243
- array: <S extends AnySchema>(schema: S) => ArraySchema<unknown, SchemaType<S>[]>;
244
+ array: <S extends AnySchema>(schema: S) => ArraySchema<any, any[]>;
244
245
  /**
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.
246
+ * Create an optional schema type.
247
+ * @template S
248
+ * @param {S} schema - Schema to mark as optional
249
+ * @returns {OptionalSchema<unknown, InferSchema<S>>} Optional schema type
248
250
  */
249
- enum: <T extends string | number | boolean, Values extends readonly [T, ...T[]]>(values: Values) => UnionSchema<unknown, Values[number]>;
251
+ optional: <S extends AnySchema>(schema: S) => OptionalSchema<any, any>;
250
252
  /**
251
- * Create optional schema type
252
- * @param {S} schema - Schema
253
- * @returns {OptionalSchema<unknown, InferSchema<S> | undefined>} Optional schema type
253
+ * Create a union schema from multiple schemas.
254
+ * @template S
255
+ * @param {...S} schemas - Schemas to combine
256
+ * @returns {UnionSchema<unknown, InferSchema<S[number]>>} Union schema type
254
257
  */
255
- optional: <S extends AnySchema>(schema: S) => OptionalSchema<unknown, InferSchema<S> | undefined>;
258
+ options: <S extends AnySchema[]>(...schemas: S) => UnionSchema<unknown, InferSchema<S[number]>>;
256
259
  /**
257
- * Create options schema type
258
- * @param {S} schemas - Schemas
259
- * @returns {UnionSchema<unknown, InferSchema<S[number]>>} Options schema type
260
+ * Create an enum schema from a list of primitive values.
261
+ * @template T
262
+ * @template Values
263
+ * @param {Values} values - Array of allowed literal values
264
+ * @returns {UnionSchema<unknown, Values[number]>} Enum schema type
265
+ * @throws {Error} If the array is empty
260
266
  */
261
- options: <S extends AnySchema[]>(...schemas: S) => UnionSchema<unknown, InferSchema<S[number]>>;
267
+ enum: <T extends string | number | boolean, Values extends readonly [T, ...T[]]>(values: Values) => UnionSchema<unknown, Values[number]>;
262
268
  /**
263
- * Create instance of schema type
264
- * @param {C} constructor - Constructor function
265
- * @returns {InstanceOfSchema<unknown, InstanceType<C>>} Instance of schema type
269
+ * Create a schema that validates instances of a specific class.
270
+ * @template C
271
+ * @param {C} constructor - Constructor function or class
272
+ * @returns {InstanceOfSchema<unknown, InstanceType<C>>} InstanceOf schema type
266
273
  */
267
274
  instanceOf: <C extends new (...args: any[]) => any>(constructor: C) => InstanceOfSchema<unknown, InstanceType<C>>;
268
275
  /**
269
- * Create UUID schema type
276
+ * Create a UUID schema type.
270
277
  * @returns {StringSchemaType} UUID schema type
271
278
  */
272
279
  uuid: () => StringSchemaType;
273
280
  /**
274
- * Create regex schema type
275
- * @param {RegExp} regex - Regex
276
- * @returns {StringSchemaType} Regex schema type
277
- */
278
- regex: (regex: RegExp) => StringSchemaType;
279
- /**
280
- * Create email schema type
281
+ * Create an email schema type.
281
282
  * @returns {StringSchemaType} Email schema type
282
283
  */
283
284
  email: () => StringSchemaType;
284
285
  /**
285
- * Create phone schema type
286
+ * Create a regex-validated string schema type.
287
+ * @param {RegExp} r - Regular expression
288
+ * @returns {StringSchemaType} Regex schema type
289
+ */
290
+ regex: (r: RegExp) => StringSchemaType;
291
+ /**
292
+ * Create a phone schema type.
286
293
  * @returns {StringSchemaType} Phone schema type
287
294
  */
288
295
  phone: () => StringSchemaType;
289
- /** Create domain schema type
290
- * @param {boolean} requireHttpOrHttps - Require http or https
296
+ /**
297
+ * Create a domain schema type.
298
+ * @param {boolean} [req=true] - Whether domain must include http/https
291
299
  * @returns {StringSchemaType} Domain schema type
292
300
  */
293
- domain: (requireHttpOrHttps?: boolean) => StringSchemaType;
301
+ domain: (req?: boolean) => StringSchemaType;
294
302
  /**
295
- * Convert schema to standard schema
296
- * @param {AnySchema} schema - Schema
297
- * @returns {Schema<unknown, any>} Standard schema
303
+ * Convert any schema into a standard schema.
304
+ * @template T
305
+ * @param {AnySchema} schema - Schema to convert
306
+ * @returns {Schema<unknown, T>} Standardized schema
298
307
  */
299
- toStandard: typeof toStandard;
308
+ toStandard: <T>(schema: AnySchema) => Schema<unknown, T>;
300
309
  };
301
310
 
302
311
  export { type AnySchema, AnySchemaType, ArraySchema, BaseSchema, BooleanSchemaType, InstanceOfSchema, LiteralSchema, NullSchemaType, NumberSchemaType, ObjectSchemaType, OptionalSchema, StringSchemaType, UnionSchema, h };
package/dist/index.js CHANGED
@@ -1 +1,2 @@
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});
1
+ "use strict";var X=Object.create;var T=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var ee=Object.getPrototypeOf,ne=Object.prototype.hasOwnProperty;var te=(e,n)=>()=>(e&&(n=e(e=0)),n);var re=(e,n)=>()=>(n||e((n={exports:{}}).exports,n),n.exports),v=(e,n)=>{for(var t in n)T(e,t,{get:n[t],enumerable:!0})},P=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of Z(n))!ne.call(e,a)&&a!==t&&T(e,a,{get:()=>n[a],enumerable:!(r=Y(n,a))||r.enumerable});return e};var ae=(e,n,t)=>(t=e!=null?X(ee(e)):{},P(n||!e||!e.__esModule?T(t,"default",{value:e,enumerable:!0}):t,e)),$=e=>P(T({},"__esModule",{value:!0}),e);var H={};v(H,{is_instance_of:()=>se});function se(e,n){return e instanceof n}var U=te(()=>{"use strict"});var Q=re((o,G)=>{"use strict";var D={};D["./snippets/validations-f447b611bd96feb0/inline0.js"]=(U(),$(H));D.__wbindgen_placeholder__=G.exports;function K(e){return e==null}function M(e){let n=typeof e;if(n=="number"||n=="boolean"||e==null)return`${e}`;if(n=="string")return`"${e}"`;if(n=="symbol"){let a=e.description;return a==null?"Symbol":`Symbol(${a})`}if(n=="function"){let a=e.name;return typeof a=="string"&&a.length>0?`Function(${a})`:"Function"}if(Array.isArray(e)){let a=e.length,c="[";a>0&&(c+=M(e[0]));for(let i=1;i<a;i++)c+=", "+M(e[i]);return c+="]",c}let t=/\[object ([^\]]+)\]/.exec(toString.call(e)),r;if(t&&t.length>1)r=t[1];else return toString.call(e);if(r=="Object")try{return"Object("+JSON.stringify(e)+")"}catch{return"Object"}return e instanceof Error?`${e.name}: ${e.message}
2
+ ${e.stack}`:r}var d=0,k=null;function j(){return(k===null||k.byteLength===0)&&(k=new Uint8Array(s.memory.buffer)),k}var g=new TextEncoder;"encodeInto"in g||(g.encodeInto=function(e,n){let t=g.encode(e);return n.set(t),{read:e.length,written:t.length}});function y(e,n,t){if(t===void 0){let h=g.encode(e),p=n(h.length,1)>>>0;return j().subarray(p,p+h.length).set(h),d=h.length,p}let r=e.length,a=n(r,1)>>>0,c=j(),i=0;for(;i<r;i++){let h=e.charCodeAt(i);if(h>127)break;c[a+i]=h}if(i!==r){i!==0&&(e=e.slice(i)),a=t(a,r,r=i+e.length*3,1)>>>0;let h=j().subarray(a+i,a+r),p=g.encodeInto(e,h);i+=p.written,a=t(a,r,i,1)>>>0}return d=i,a}var f=null;function l(){return(f===null||f.buffer.detached===!0||f.buffer.detached===void 0&&f.buffer!==s.memory.buffer)&&(f=new DataView(s.memory.buffer)),f}var q=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0});q.decode();function oe(e,n){return q.decode(j().subarray(e,e+n))}function N(e,n){return e=e>>>0,oe(e,n)}function z(e){let n=s.__externref_table_alloc();return s.__wbindgen_externrefs.set(n,e),n}function J(e,n){try{return e.apply(this,n)}catch(t){let r=z(t);s.__wbindgen_exn_store(r)}}function L(e,n){if(!(e instanceof n))throw new Error(`expected instance of ${n.name}`)}function ce(e,n){let t=n(e.length*4,4)>>>0;for(let r=0;r<e.length;r++){let a=z(e[r]);l().setUint32(t+4*r,a,!0)}return d=e.length,t}var W=typeof FinalizationRegistry>"u"?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>s.__wbg_hschema_free(e>>>0,1)),b=class e{static __wrap(n){n=n>>>0;let t=Object.create(e.prototype);return t.__wbg_ptr=n,W.register(t,t.__wbg_ptr,t),t}static __unwrap(n){return n instanceof e?n.__destroy_into_raw():0}__destroy_into_raw(){let n=this.__wbg_ptr;return this.__wbg_ptr=0,W.unregister(this),n}free(){let n=this.__destroy_into_raw();s.__wbg_hschema_free(n,0)}static string(){let n=s.hschema_string();return e.__wrap(n)}static number(){let n=s.hschema_number();return e.__wrap(n)}static boolean(){let n=s.hschema_boolean();return e.__wrap(n)}static any(){let n=s.hschema_any();return e.__wrap(n)}static null_type(){let n=s.hschema_null_type();return e.__wrap(n)}static literal(n){let t=s.hschema_literal(n);return e.__wrap(t)}static object(){let n=s.hschema_object();return e.__wrap(n)}static array(n){L(n,e);let t=s.hschema_array(n.__wbg_ptr);return e.__wrap(t)}static union(n){let t=ce(n,s.__wbindgen_malloc),r=d,a=s.hschema_union(t,r);return e.__wrap(a)}static instance_of(n,t){let r=y(t,s.__wbindgen_malloc,s.__wbindgen_realloc),a=d,c=s.hschema_instance_of(n,r,a);return e.__wrap(c)}optional(){let n=s.hschema_optional(this.__wbg_ptr);return e.__wrap(n)}coerce(){let n=s.hschema_coerce(this.__wbg_ptr);return e.__wrap(n)}min_length(n){let t=s.hschema_min_length(this.__wbg_ptr,n);return e.__wrap(t)}max_length(n){let t=s.hschema_max_length(this.__wbg_ptr,n);return e.__wrap(t)}uuid(){let n=s.hschema_uuid(this.__wbg_ptr);return e.__wrap(n)}email(){let n=s.hschema_email(this.__wbg_ptr);return e.__wrap(n)}regex(n){let t=y(n,s.__wbindgen_malloc,s.__wbindgen_realloc),r=d,a=s.hschema_regex(this.__wbg_ptr,t,r);return e.__wrap(a)}phone(){let n=s.hschema_phone(this.__wbg_ptr);return e.__wrap(n)}domain(n){let t=s.hschema_domain(this.__wbg_ptr,n);return e.__wrap(t)}min(n){let t=s.hschema_min(this.__wbg_ptr,n);return e.__wrap(t)}max(n){let t=s.hschema_max(this.__wbg_ptr,n);return e.__wrap(t)}add_prop(n,t){let r=y(n,s.__wbindgen_malloc,s.__wbindgen_realloc),a=d;L(t,e),s.hschema_add_prop(this.__wbg_ptr,r,a,t.__wbg_ptr)}validate(n){return s.hschema_validate(this.__wbg_ptr,n)}get_json_schema(){return s.hschema_get_json_schema(this.__wbg_ptr)}};Symbol.dispose&&(b.prototype[Symbol.dispose]=b.prototype.free);o.HSchema=b;o.__wbg___wbindgen_boolean_get_6d5a1ee65bab5f68=function(e){let n=e,t=typeof n=="boolean"?n:void 0;return K(t)?16777215:t?1:0};o.__wbg___wbindgen_debug_string_df47ffb5e35e6763=function(e,n){let t=M(n),r=y(t,s.__wbindgen_malloc,s.__wbindgen_realloc),a=d;l().setInt32(e+4*1,a,!0),l().setInt32(e+4*0,r,!0)};o.__wbg___wbindgen_is_falsy_46b8d2f2aba49112=function(e){return!e};o.__wbg___wbindgen_is_null_5e69f72e906cc57c=function(e){return e===null};o.__wbg___wbindgen_is_object_c818261d21f283a4=function(e){let n=e;return typeof n=="object"&&n!==null};o.__wbg___wbindgen_is_string_fbb76cb2940daafd=function(e){return typeof e=="string"};o.__wbg___wbindgen_is_undefined_2d472862bd29a478=function(e){return e===void 0};o.__wbg___wbindgen_jsval_eq_6b13ab83478b1c50=function(e,n){return e===n};o.__wbg___wbindgen_number_get_a20bf9b85341449d=function(e,n){let t=n,r=typeof t=="number"?t:void 0;l().setFloat64(e+8*1,K(r)?0:r,!0),l().setInt32(e+4*0,!K(r),!0)};o.__wbg___wbindgen_string_get_e4f06c90489ad01b=function(e,n){let t=n,r=typeof t=="string"?t:void 0;var a=K(r)?0:y(r,s.__wbindgen_malloc,s.__wbindgen_realloc),c=d;l().setInt32(e+4*1,c,!0),l().setInt32(e+4*0,a,!0)};o.__wbg___wbindgen_throw_b855445ff6a94295=function(e,n){throw new Error(N(e,n))};o.__wbg_from_a4ad7cbddd0d7135=function(e){return Array.from(e)};o.__wbg_get_7bed016f185add81=function(e,n){return e[n>>>0]};o.__wbg_get_efcb449f58ec27c2=function(){return J(function(e,n){return Reflect.get(e,n)},arguments)};o.__wbg_hschema_unwrap=function(e){return b.__unwrap(e)};o.__wbg_isArray_96e0af9891d0945d=function(e){return Array.isArray(e)};o.__wbg_length_cdd215e10d9dd507=function(e){return e.length};o.__wbg_new_1acc0b6eea89d040=function(){return new Object};o.__wbg_new_e17d9f43105b08be=function(){return new Array};o.__wbg_push_df81a39d04db858c=function(e,n){return e.push(n)};o.__wbg_set_3fda3bac07393de4=function(e,n,t){e[n]=t};o.__wbg_set_c213c871859d6500=function(e,n,t){e[n>>>0]=t};o.__wbg_set_c2abbebe8b9ebee1=function(){return J(function(e,n,t){return Reflect.set(e,n,t)},arguments)};o.__wbindgen_cast_2241b6af4c4b2941=function(e,n){return N(e,n)};o.__wbindgen_cast_d6cd19b81560fd6e=function(e){return e};o.__wbindgen_init_externref_table=function(){let e=s.__wbindgen_externrefs,n=e.grow(4);e.set(0,void 0),e.set(n+0,void 0),e.set(n+1,null),e.set(n+2,!0),e.set(n+3,!1)};var ie=`${__dirname}/validations_bg.wasm`,_e=require("fs").readFileSync(ie),ue=new WebAssembly.Module(_e),s=o.__wasm=new WebAssembly.Instance(ue,D).exports;s.__wbindgen_start()});var me={};v(me,{AnySchemaType:()=>O,ArraySchema:()=>C,BaseSchema:()=>u,BooleanSchemaType:()=>I,InstanceOfSchema:()=>E,LiteralSchema:()=>B,NullSchemaType:()=>A,NumberSchemaType:()=>x,ObjectSchemaType:()=>w,OptionalSchema:()=>F,StringSchemaType:()=>S,UnionSchema:()=>R,h:()=>m});module.exports=$(me);var _=ae(Q()),u=class{wasmSchema;constructor(n){this.wasmSchema=n}get"~standard"(){return{version:1,vendor:"h-schema-rs",validate:n=>this.wasmSchema.validate(n)}}get jsonSchema(){return this.wasmSchema.get_json_schema()}get inferred(){return null}coerce(){return this.wasmSchema=this.wasmSchema.coerce(),this}optional(){return new F(this.wasmSchema.optional(),this)}array(){return new C(_.HSchema.array(this.wasmSchema),this)}enum(n){let t=n.map(r=>m.literal(r));return m.options(...t)}instanceOf(n){return new E(this.wasmSchema,n)}},S=class e extends u{constructor(n=_.HSchema.string()){super(n)}minLength(n){return new e(this.wasmSchema.min_length(n))}maxLength(n){return new e(this.wasmSchema.max_length(n))}uuid(){return new e(this.wasmSchema.uuid())}email(){return new e(this.wasmSchema.email())}regex(n){return new e(this.wasmSchema.regex(n.source))}phone(){return new e(this.wasmSchema.phone())}domain(n=!0){return new e(this.wasmSchema.domain(n))}},x=class e extends u{constructor(n=_.HSchema.number()){super(n)}min(n){return new e(this.wasmSchema.min(n))}max(n){return new e(this.wasmSchema.max(n))}},I=class extends u{constructor(){super(_.HSchema.boolean())}},A=class extends u{constructor(){super(_.HSchema.null_type())}},O=class extends u{constructor(){super(_.HSchema.any())}},B=class extends u{value;constructor(n){super(_.HSchema.literal(n)),this.value=n}},F=class extends u{innerSchema;constructor(n,t){super(n),this.innerSchema=t}},C=class extends u{innerSchema;constructor(n,t){super(n),this.innerSchema=t}},w=class extends u{definition;constructor(n){let t=_.HSchema.object();for(let r in n){let a=n[r],c;a instanceof u?c=a.wasmSchema:typeof a=="string"?a==="string"?c=_.HSchema.string():a==="number"?c=_.HSchema.number():a==="boolean"?c=_.HSchema.boolean():c=_.HSchema.any():c=_.HSchema.any(),t.add_prop(r,c)}super(t),this.definition=n}},R=class extends u{schemas;constructor(n){let t=n.map(r=>r.wasmSchema);super(_.HSchema.union(t)),this.schemas=n}},E=class extends u{classConstructor;constructor(n,t){super(_.HSchema.instance_of(t,t.name)),this.classConstructor=t}};function V(e){return e instanceof u?e:e==="string"?new S:e==="number"?new x:e==="boolean"?new I:typeof e=="object"&&e!==null?new w(e):new O}var m={string:()=>new S,number:()=>new x,boolean:()=>new I,null:()=>new A,any:()=>new O,literal:e=>new B(e),object:e=>new w(e||{}),array:e=>V(e).array(),optional:e=>V(e).optional(),options:(...e)=>{let n=e.map(t=>V(t));return new R(n)},enum:e=>{if(!e||e.length===0)throw new Error("h.enum() requires non-empty array");let n=e.map(t=>m.literal(t));return m.options(...n)},instanceOf:e=>new w({}).instanceOf(e),uuid:()=>m.string().uuid(),email:()=>m.string().email(),regex:e=>m.string().regex(e),phone:()=>m.string().phone(),domain:(e=!0)=>m.string().domain(e),toStandard:e=>V(e)};0&&(module.exports={AnySchemaType,ArraySchema,BaseSchema,BooleanSchemaType,InstanceOfSchema,LiteralSchema,NullSchemaType,NumberSchemaType,ObjectSchemaType,OptionalSchema,StringSchemaType,UnionSchema,h});
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hedystia/validations",
3
- "version": "1.6.10",
3
+ "version": "1.7.1",
4
4
  "devDependencies": {
5
5
  "@standard-schema/spec": "^1.0.0",
6
6
  "@types/bun": "latest",
@@ -17,8 +17,8 @@
17
17
  "private": false,
18
18
  "license": "MIT",
19
19
  "scripts": {
20
- "build": "tsup",
21
- "dev": "bun --watch --no-clear-screen run src/index.ts"
20
+ "build": "cargo clean && wasm-pack build --target nodejs && tsup && cp pkg/validations_bg.wasm dist/",
21
+ "dev": "bun --watch --no-clear-screen run index.ts"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",