@hedystia/validations 1.2.5 → 1.2.7
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 +22 -5
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/readme.md +3 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,11 +4,28 @@ type SchemaPrimitive = "string" | "number" | "boolean" | "any";
|
|
|
4
4
|
interface SchemaLike {
|
|
5
5
|
[key: string]: SchemaPrimitive | SchemaLike | BaseSchema<any, any>;
|
|
6
6
|
}
|
|
7
|
-
type
|
|
8
|
-
[K in keyof
|
|
7
|
+
type Simplify<T> = T extends any ? {
|
|
8
|
+
[K in keyof T]: T[K];
|
|
9
|
+
} : never;
|
|
10
|
+
type RequiredKeys<S> = {
|
|
11
|
+
[K in keyof S]: S[K] extends OptionalSchema<any, any> ? never : K;
|
|
12
|
+
}[keyof S];
|
|
13
|
+
type OptionalKeys<S> = {
|
|
14
|
+
[K in keyof S]: S[K] extends OptionalSchema<any, any> ? K : never;
|
|
15
|
+
}[keyof S];
|
|
16
|
+
type SchemaPrimitiveMap = {
|
|
17
|
+
string: string;
|
|
18
|
+
number: number;
|
|
19
|
+
boolean: boolean;
|
|
20
|
+
any: unknown;
|
|
21
|
+
};
|
|
22
|
+
type SchemaType<S> = S extends BaseSchema<any, infer O> ? O : S extends keyof SchemaPrimitiveMap ? SchemaPrimitiveMap[S] : S extends Record<string, any> ? InferSchema<S> : unknown;
|
|
23
|
+
type InferObject<S extends SchemaDefinition> = Simplify<{
|
|
24
|
+
[K in RequiredKeys<S>]: SchemaType<S[K]>;
|
|
9
25
|
} & {
|
|
10
|
-
[K in
|
|
11
|
-
}
|
|
26
|
+
[K in OptionalKeys<S>]?: SchemaType<S[K]>;
|
|
27
|
+
}>;
|
|
28
|
+
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
29
|
[key: string]: any;
|
|
13
30
|
} ? {
|
|
14
31
|
[K in keyof S as undefined extends InferSchema<S[K]> ? K : never]?: InferSchema<S[K]>;
|
|
@@ -116,7 +133,7 @@ declare const h: {
|
|
|
116
133
|
null: () => NullSchemaType;
|
|
117
134
|
any: () => AnySchemaType;
|
|
118
135
|
literal: <T extends string | number | boolean>(value: T) => LiteralSchema<T>;
|
|
119
|
-
object: <S extends SchemaDefinition>(schemaDef?: S) => ObjectSchemaType<
|
|
136
|
+
object: <S extends SchemaDefinition>(schemaDef?: S) => ObjectSchemaType<InferObject<S>>;
|
|
120
137
|
array: <S extends AnySchema>(schema: S) => ArraySchema<unknown, InferSchema<S>[]>;
|
|
121
138
|
enum: <T extends readonly [any, ...any[]]>(values: T) => EnumSchema<unknown, T[number]>;
|
|
122
139
|
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})},
|
|
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})},K=(n,e,a,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of j(e))!P.call(n,t)&&t!==a&&w(n,t,{get:()=>e[t],enumerable:!(r=T(e,t))||r.enumerable});return n};var E=n=>K(w({},"__esModule",{value:!0}),n);var R={};V(R,{h:()=>u});module.exports=E(R);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 A(n,e){return typeof e=="string"&&n==="string"||typeof e=="number"&&n==="number"&&!Number.isNaN(e)||typeof e=="boolean"&&n==="boolean"}var S=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)}},l=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 t=await r["~standard"].validate(e);if(!("issues"in t))return{value:t.value};a.push(...t.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(t,s)=>{let o=await this.innerSchema["~standard"].validate(t);return"issues"in o?{index:s,issues:o.issues?.map(m=>({...m,path:m.path?[s,...m.path]:[s]}))}:{index:s,value:o.value}})),r=a.filter(t=>"issues"in t);return r.length>0?{issues:r.flatMap(t=>t.issues)}:{value:a.map(t=>"value"in t?t.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 t in e){let s=e[t];s instanceof d||r.push(t),typeof s=="string"?a[t]={type:s}:s instanceof i?a[t]=s.jsonSchema:typeof s=="object"&&s!==null&&(a[t]={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={},t=[];for(let s in this.definition){let o=this.definition[s],m=o instanceof d;if(!(s in a)&&!m){t.push({message:`Missing required property: ${s}`,path:[s]});continue}if(s in a){if(typeof o=="string"&&o in["string","number","boolean"]){let c=o;A(c,a[s])?r[s]=a[s]:t.push({message:`Invalid type for property ${s}: expected ${c}`,path:[s]})}else if(o instanceof i){let c=await o["~standard"].validate(a[s]);"issues"in c?c.issues&&t.push(...c.issues.map(f=>({...f,path:f.path?[s,...f.path]:[s]}))):r[s]=c.value}}}return t.length>0?{issues:t}:{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 S;else if(n==="number")e=new l;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 S,number:()=>new l,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
package/readme.md
CHANGED
|
@@ -21,8 +21,9 @@
|
|
|
21
21
|
|
|
22
22
|
## 🌟 Superpowers
|
|
23
23
|
|
|
24
|
+
- 🌐 **Multi-runtime support** - Bun (default), Deno, Node.js, Vercel, Cloudflare Workers, Fastly Compute, Lambda, etc.
|
|
24
25
|
- 🔒 **End-to-end type safety** - From params to response, full TypeScript integration
|
|
25
|
-
- ⚡ **Bun-native performance** - Built for Bun runtime with
|
|
26
|
+
- ⚡ **Bun-native performance** - Built for Bun runtime with native validation
|
|
26
27
|
- 🧩 **Client integration** - Auto-generated type-safe HTTP client
|
|
27
28
|
- 🛡️ **Validation built-in** - Zod integration for runtime safety
|
|
28
29
|
- 🔌 **Extensible architecture** - Middleware, hooks and macros system
|
|
@@ -131,6 +132,7 @@ app.use("/swagger", swaggerPlugin.plugin).listen(3000);
|
|
|
131
132
|
- ✅ Router Groups & Middleware
|
|
132
133
|
- ✅ Type-safe Client Generation
|
|
133
134
|
- ✅ WebSocket Support
|
|
135
|
+
- ✅ Adapter System to work with other frameworks
|
|
134
136
|
|
|
135
137
|
### Advanced Capabilities
|
|
136
138
|
- ✅ Standard Schema Compatibility
|