@hedystia/validations 1.2.3 → 1.2.4

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.
@@ -0,0 +1,130 @@
1
+ import { StandardSchemaV1 } from '@standard-schema/spec';
2
+
3
+ type SchemaPrimitive = "string" | "number" | "boolean" | "any";
4
+ interface SchemaLike {
5
+ [key: string]: SchemaPrimitive | SchemaLike | BaseSchema<any, any>;
6
+ }
7
+ type InferSchema<S> = S extends BaseSchema<any, infer O> ? O extends object ? {
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 {
12
+ [key: string]: any;
13
+ } ? {
14
+ [K in keyof S as undefined extends InferSchema<S[K]> ? K : never]?: InferSchema<S[K]>;
15
+ } & {
16
+ [K in keyof S as undefined extends InferSchema<S[K]> ? never : K]: InferSchema<S[K]>;
17
+ } : unknown;
18
+ type SchemaDefinition = SchemaLike;
19
+ interface Schema<I, O> extends StandardSchemaV1<I, O> {
20
+ optional(): OptionalSchema<I, O | undefined>;
21
+ enum<Values extends readonly [O, ...O[]]>(values: Values): EnumSchema<I, Values[number]>;
22
+ array(): ArraySchema<I, O[]>;
23
+ instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
24
+ jsonSchema: any;
25
+ readonly inferred: O;
26
+ schema: Schema<I, O>;
27
+ }
28
+ declare abstract class BaseSchema<I, O> implements Schema<I, O> {
29
+ abstract readonly "~standard": StandardSchemaV1.Props<I, O>;
30
+ jsonSchema: any;
31
+ get inferred(): O;
32
+ schema: Schema<I, O>;
33
+ protected _coerce: boolean;
34
+ coerce(): this;
35
+ optional(): OptionalSchema<I, O | undefined>;
36
+ enum<Values extends readonly [O, ...O[]]>(values: Values): EnumSchema<I, Values[number]>;
37
+ array(): ArraySchema<I, O[]>;
38
+ instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
39
+ }
40
+ declare class StringSchemaType extends BaseSchema<unknown, string> {
41
+ readonly type: SchemaPrimitive;
42
+ private _validateEmail;
43
+ private _validatePhone;
44
+ constructor();
45
+ primitive(): SchemaPrimitive;
46
+ email(): StringSchemaType;
47
+ phone(): StringSchemaType;
48
+ readonly "~standard": StandardSchemaV1.Props<unknown, string>;
49
+ private _isValidEmail;
50
+ private _isValidPhone;
51
+ }
52
+ declare class NumberSchemaType extends BaseSchema<unknown, number> {
53
+ readonly type: SchemaPrimitive;
54
+ constructor();
55
+ primitive(): SchemaPrimitive;
56
+ readonly "~standard": StandardSchemaV1.Props<unknown, number>;
57
+ }
58
+ declare class BooleanSchemaType extends BaseSchema<unknown, boolean> {
59
+ readonly type: SchemaPrimitive;
60
+ constructor();
61
+ primitive(): SchemaPrimitive;
62
+ readonly "~standard": StandardSchemaV1.Props<unknown, boolean>;
63
+ }
64
+ declare class AnySchemaType extends BaseSchema<unknown, any> {
65
+ readonly type: SchemaPrimitive;
66
+ readonly "~standard": StandardSchemaV1.Props<unknown, any>;
67
+ }
68
+ declare class LiteralSchema<T extends string | number | boolean> extends BaseSchema<unknown, T> {
69
+ private readonly value;
70
+ constructor(value: T);
71
+ readonly "~standard": StandardSchemaV1.Props<unknown, T>;
72
+ }
73
+ declare class OptionalSchema<I, O> extends BaseSchema<I, O | undefined> {
74
+ private readonly innerSchema;
75
+ constructor(schema: Schema<I, O>);
76
+ readonly "~standard": StandardSchemaV1.Props<I, O | undefined>;
77
+ }
78
+ declare class NullSchemaType extends BaseSchema<unknown, null> {
79
+ readonly type: "null";
80
+ constructor();
81
+ readonly "~standard": StandardSchemaV1.Props<unknown, null>;
82
+ }
83
+ declare class UnionSchema<I, O> extends BaseSchema<I, O> {
84
+ private readonly schemas;
85
+ constructor(...schemas: Schema<I, any>[]);
86
+ readonly "~standard": StandardSchemaV1.Props<I, O>;
87
+ }
88
+ declare class EnumSchema<I, O> extends BaseSchema<I, O> {
89
+ private readonly innerSchema;
90
+ private readonly values;
91
+ constructor(schema: Schema<I, any>, values: readonly O[]);
92
+ readonly "~standard": StandardSchemaV1.Props<I, O>;
93
+ }
94
+ declare class ArraySchema<I, O extends any[]> extends BaseSchema<I, O> {
95
+ private readonly innerSchema;
96
+ constructor(schema: Schema<I, O[number]>);
97
+ readonly "~standard": StandardSchemaV1.Props<I, O>;
98
+ }
99
+ declare class InstanceOfSchema<I, O> extends BaseSchema<I, O> {
100
+ private readonly innerSchema;
101
+ private readonly classConstructor;
102
+ constructor(schema: Schema<I, any>, classConstructor: new (...args: any[]) => any);
103
+ readonly "~standard": StandardSchemaV1.Props<I, O>;
104
+ }
105
+ declare class ObjectSchemaType<T extends Record<string, unknown>> extends BaseSchema<unknown, T> {
106
+ readonly definition: SchemaDefinition;
107
+ constructor(definition: SchemaDefinition);
108
+ readonly "~standard": StandardSchemaV1.Props<unknown, T>;
109
+ }
110
+ type AnySchema = SchemaPrimitive | BaseSchema<any, any> | SchemaDefinition;
111
+ declare function toStandard<T>(schema: AnySchema): Schema<unknown, T>;
112
+ declare const h: {
113
+ string: () => StringSchemaType;
114
+ number: () => NumberSchemaType;
115
+ boolean: () => BooleanSchemaType;
116
+ null: () => NullSchemaType;
117
+ any: () => AnySchemaType;
118
+ literal: <T extends string | number | boolean>(value: T) => LiteralSchema<T>;
119
+ object: <S extends SchemaDefinition>(schemaDef?: S) => ObjectSchemaType<InferSchema<S>>;
120
+ array: <S extends AnySchema>(schema: S) => ArraySchema<unknown, InferSchema<S>[]>;
121
+ enum: <T extends readonly [any, ...any[]]>(values: T) => EnumSchema<unknown, T[number]>;
122
+ optional: <S extends AnySchema>(schema: S) => OptionalSchema<unknown, InferSchema<S> | undefined>;
123
+ options: <S extends AnySchema[]>(...schemas: S) => UnionSchema<unknown, InferSchema<S[number]>>;
124
+ instanceOf: <C extends new (...args: any[]) => any>(constructor: C) => InstanceOfSchema<unknown, InstanceType<C>>;
125
+ email: () => StringSchemaType;
126
+ phone: () => StringSchemaType;
127
+ toStandard: typeof toStandard;
128
+ };
129
+
130
+ export { h };
package/dist/index.js ADDED
@@ -0,0 +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 k(this,e)}array(){return new g(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:{}}}},O=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:{}}}},k=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:{}}}},g=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 O(...e)},instanceOf:n=>u.object({}).instanceOf(n),email:()=>u.string().email(),phone:()=>u.string().phone(),toStandard:h};0&&(module.exports={h});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hedystia/validations",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "devDependencies": {
5
5
  "@standard-schema/spec": "^1.0.0",
6
6
  "@types/bun": "latest",