@hedystia/validations 1.9.6 → 1.9.8
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 +9 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -105,12 +105,14 @@ 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
|
+
null(): UnionSchema<I, O | null>;
|
|
108
109
|
enum<V extends O & (string | number | boolean), Values extends readonly [V, ...V[]]>(values: Values): UnionSchema<I, Values[number]>;
|
|
109
110
|
array(): ArraySchema<I, O[]>;
|
|
110
111
|
instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
|
|
111
112
|
}
|
|
112
113
|
declare class StringSchemaType extends BaseSchema<unknown, string> {
|
|
113
114
|
readonly type: SchemaPrimitive;
|
|
115
|
+
private _validateDate;
|
|
114
116
|
private _validateUUID;
|
|
115
117
|
private _validateRegex;
|
|
116
118
|
private _validateEmail;
|
|
@@ -123,12 +125,14 @@ declare class StringSchemaType extends BaseSchema<unknown, string> {
|
|
|
123
125
|
primitive(): SchemaPrimitive;
|
|
124
126
|
minLength(n: number): StringSchemaType;
|
|
125
127
|
maxLength(n: number): StringSchemaType;
|
|
128
|
+
date(): StringSchemaType;
|
|
126
129
|
uuid(): StringSchemaType;
|
|
127
130
|
regex(regex: RegExp): StringSchemaType;
|
|
128
131
|
email(): StringSchemaType;
|
|
129
132
|
phone(): StringSchemaType;
|
|
130
133
|
domain(requireHttpOrHttps?: boolean): StringSchemaType;
|
|
131
134
|
readonly "~standard": StandardSchemaV1.Props<unknown, string>;
|
|
135
|
+
private _isValidDate;
|
|
132
136
|
private _isValidUUID;
|
|
133
137
|
private _isValidRegex;
|
|
134
138
|
private _isValidEmail;
|
|
@@ -265,6 +269,11 @@ declare const h: {
|
|
|
265
269
|
* @returns {InstanceOfSchema<unknown, InstanceType<C>>} Instance of schema type
|
|
266
270
|
*/
|
|
267
271
|
instanceOf: <C extends new (...args: any[]) => any>(constructor: C) => InstanceOfSchema<unknown, InstanceType<C>>;
|
|
272
|
+
/**
|
|
273
|
+
* Create date schema type
|
|
274
|
+
* @returns {StringSchemaType} Date schema type
|
|
275
|
+
*/
|
|
276
|
+
date: () => StringSchemaType;
|
|
268
277
|
/**
|
|
269
278
|
* Create UUID schema type
|
|
270
279
|
* @returns {StringSchemaType} UUID schema type
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
|
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 D={};V(D,{AnySchemaType:()=>w,ArraySchema:()=>x,BaseSchema:()=>i,BooleanSchemaType:()=>l,InstanceOfSchema:()=>I,LiteralSchema:()=>y,NullSchemaType:()=>f,NumberSchemaType:()=>p,ObjectSchemaType:()=>g,OptionalSchema:()=>u,StringSchemaType:()=>S,UnionSchema:()=>h,h:()=>m});module.exports=P(D);var i=class{jsonSchema={};get inferred(){return null}schema=this;_coerce=!1;coerce(){return this._coerce=!0,this}optional(){return new u(this)}null(){return new h(this,new f)}enum(e){let n=e.map(r=>new y(r));return new h(...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 S=class t extends i{type="string";_validateDate=!1;_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}date(){let e=new t;return Object.assign(e,this),e._validateDate=!0,e.jsonSchema={...this.jsonSchema,format:"date"},e}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"}]}:this._validateDate&&!this._isValidDate(e)?{issues:[{message:"Invalid date format"}]}:{value:e}),types:{input:{},output:{}}};_isValidDate(e){let n=new Date(e);return!Number.isNaN(n.getTime())}_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)}},p=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:{}}}},l=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:{}}}},w=class extends i{type="any";"~standard"={version:1,vendor:"h-schema",validate:e=>({value:e}),types:{input:{},output:{}}}},y=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:e=>e==null?{value:void 0}:this.innerSchema["~standard"].validate(e),types:{input:{},output:{}}}},f=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:{}}}},h=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:e=>{let n=[];for(let r of this.schemas){let a=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:e=>{if(!Array.isArray(e))return{issues:[{message:`Expected array, received ${typeof e}`}]};let n=e.map((a,s)=>{let o=this.innerSchema["~standard"].validate(a);return"issues"in o?{index:s,issues:o.issues?.map(d=>({...d,path:d.path?[s,...d.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:e=>e instanceof this.classConstructor?this.innerSchema["~standard"].validate(e):{issues:[{message:`Expected instance of ${this.classConstructor.name}`}]},types:{input:{},output:{}}}},g=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: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],d=o instanceof u;if(!(s in n)&&!d){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=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 b(t){let e;if(t instanceof i)e=t;else if(typeof t=="string")if(t==="string")e=new S;else if(t==="number")e=new p;else if(t==="boolean")e=new l;else throw new Error("Invalid schema type provided to toStandard");else if(typeof t=="object"&&t!==null&&!Array.isArray(t))e=new g(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 S,number:()=>new p,boolean:()=>new l,null:()=>new f,any:()=>new w,literal:t=>new y(t),object:t=>new g(t||{}),array:t=>b(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=>b(t).optional(),options:(...t)=>{let e=t.map(n=>b(n).schema);return new h(...e)},instanceOf:t=>m.object({}).instanceOf(t),date:()=>m.string().date(),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:b};0&&(module.exports={AnySchemaType,ArraySchema,BaseSchema,BooleanSchemaType,InstanceOfSchema,LiteralSchema,NullSchemaType,NumberSchemaType,ObjectSchemaType,OptionalSchema,StringSchemaType,UnionSchema,h});
|