@hedystia/validations 1.2.11 → 1.2.13
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 +73 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,59 @@
|
|
|
1
|
-
|
|
1
|
+
/** The Standard Schema interface. */
|
|
2
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
3
|
+
/** The Standard Schema properties. */
|
|
4
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
5
|
+
}
|
|
6
|
+
declare namespace StandardSchemaV1 {
|
|
7
|
+
/** The Standard Schema properties interface. */
|
|
8
|
+
export interface Props<Input = unknown, Output = Input> {
|
|
9
|
+
/** The version number of the standard. */
|
|
10
|
+
readonly version: 1;
|
|
11
|
+
/** The vendor name of the schema library. */
|
|
12
|
+
readonly vendor: string;
|
|
13
|
+
/** Validates unknown input values. */
|
|
14
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
15
|
+
/** Inferred types associated with the schema. */
|
|
16
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
17
|
+
}
|
|
18
|
+
/** The result interface of the validate function. */
|
|
19
|
+
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
20
|
+
/** The result interface if validation succeeds. */
|
|
21
|
+
export interface SuccessResult<Output> {
|
|
22
|
+
/** The typed output value. */
|
|
23
|
+
readonly value: Output;
|
|
24
|
+
/** The non-existent issues. */
|
|
25
|
+
readonly issues?: undefined;
|
|
26
|
+
}
|
|
27
|
+
/** The result interface if validation fails. */
|
|
28
|
+
export interface FailureResult {
|
|
29
|
+
/** The issues of failed validation. */
|
|
30
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
31
|
+
}
|
|
32
|
+
/** The issue interface of the failure output. */
|
|
33
|
+
export interface Issue {
|
|
34
|
+
/** The error message of the issue. */
|
|
35
|
+
readonly message: string;
|
|
36
|
+
/** The path of the issue, if any. */
|
|
37
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
38
|
+
}
|
|
39
|
+
/** The path segment interface of the issue. */
|
|
40
|
+
export interface PathSegment {
|
|
41
|
+
/** The key representing a path segment. */
|
|
42
|
+
readonly key: PropertyKey;
|
|
43
|
+
}
|
|
44
|
+
/** The Standard Schema types interface. */
|
|
45
|
+
export interface Types<Input = unknown, Output = Input> {
|
|
46
|
+
/** The input type of the schema. */
|
|
47
|
+
readonly input: Input;
|
|
48
|
+
/** The output type of the schema. */
|
|
49
|
+
readonly output: Output;
|
|
50
|
+
}
|
|
51
|
+
/** Infers the input type of a Standard Schema. */
|
|
52
|
+
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
53
|
+
/** Infers the output type of a Standard Schema. */
|
|
54
|
+
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
55
|
+
export { };
|
|
56
|
+
}
|
|
2
57
|
|
|
3
58
|
type SchemaPrimitive = "string" | "number" | "boolean" | "any";
|
|
4
59
|
interface SchemaLike {
|
|
@@ -56,6 +111,8 @@ declare abstract class BaseSchema<I, O> implements Schema<I, O> {
|
|
|
56
111
|
}
|
|
57
112
|
declare class StringSchemaType extends BaseSchema<unknown, string> {
|
|
58
113
|
readonly type: SchemaPrimitive;
|
|
114
|
+
private _validateUUID;
|
|
115
|
+
private _validateRegex;
|
|
59
116
|
private _validateEmail;
|
|
60
117
|
private _validatePhone;
|
|
61
118
|
private _minLength?;
|
|
@@ -64,9 +121,13 @@ declare class StringSchemaType extends BaseSchema<unknown, string> {
|
|
|
64
121
|
primitive(): SchemaPrimitive;
|
|
65
122
|
minLength(n: number): StringSchemaType;
|
|
66
123
|
maxLength(n: number): StringSchemaType;
|
|
124
|
+
uuid(): StringSchemaType;
|
|
125
|
+
regex(regex: RegExp): StringSchemaType;
|
|
67
126
|
email(): StringSchemaType;
|
|
68
127
|
phone(): StringSchemaType;
|
|
69
128
|
readonly "~standard": StandardSchemaV1.Props<unknown, string>;
|
|
129
|
+
private _isValidUUID;
|
|
130
|
+
private _isValidRegex;
|
|
70
131
|
private _isValidEmail;
|
|
71
132
|
private _isValidPhone;
|
|
72
133
|
}
|
|
@@ -206,6 +267,17 @@ declare const h: {
|
|
|
206
267
|
* @returns {InstanceOfSchema<unknown, InstanceType<C>>} Instance of schema type
|
|
207
268
|
*/
|
|
208
269
|
instanceOf: <C extends new (...args: any[]) => any>(constructor: C) => InstanceOfSchema<unknown, InstanceType<C>>;
|
|
270
|
+
/**
|
|
271
|
+
* Create UUID schema type
|
|
272
|
+
* @returns {StringSchemaType} UUID schema type
|
|
273
|
+
*/
|
|
274
|
+
uuid: () => StringSchemaType;
|
|
275
|
+
/**
|
|
276
|
+
* Create regex schema type
|
|
277
|
+
* @param {RegExp} regex - Regex
|
|
278
|
+
* @returns {StringSchemaType} Regex schema type
|
|
279
|
+
*/
|
|
280
|
+
regex: (regex: RegExp) => StringSchemaType;
|
|
209
281
|
/**
|
|
210
282
|
* Create email schema type
|
|
211
283
|
* @returns {StringSchemaType} Email schema type
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var g=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var _=(a,e)=>{for(var n in e)g(a,n,{get:e[n],enumerable:!0})},V=(a,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of T(e))!P.call(a,t)&&t!==n&&g(a,t,{get:()=>e[t],enumerable:!(r=j(e,t))||r.enumerable});return a};var K=a=>V(g({},"__esModule",{value:!0}),a);var E={};_(E,{h:()=>c});module.exports=K(E);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){return new x(this,e)}array(){return new O(this)}instanceOf(e){return new k(this,e)}};function R(a,e){return typeof e=="string"&&a==="string"||typeof e=="number"&&a==="number"&&!Number.isNaN(e)||typeof e=="boolean"&&a==="boolean"}var S=class a extends i{type="string";_validateUUID=!1;_validateRegex=!1;_validateEmail=!1;_validatePhone=!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}"~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"}]}:{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)}},p=class a extends i{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:{}}}},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:{}}}},b=class extends i{type="any";"~standard"={version:1,vendor:"h-schema",validate:e=>({value:e}),types:{input:{},output:{}}}},w=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:{}}}},v=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:{}}}},I=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 t=await r["~standard"].validate(e);if(!("issues"in t))return{value:t.value};n.push(...t.issues)}return{issues:n}},types:{input:{},output:{}}}},x=class extends i{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:{}}}},O=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(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}})),r=n.filter(t=>"issues"in t);return r.length>0?{issues:r.flatMap(t=>t.issues)}:{value:n.map(t=>"value"in t?t.value:null)}},types:{input:{},output:{}}}},k=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:{}}}},y=class extends i{definition;constructor(e){super(),this.definition=e;let n={},r=[];for(let t in e){let s=e[t];s instanceof u||r.push(t),typeof s=="string"?n[t]={type:s}:s instanceof i?n[t]=s.jsonSchema:typeof s=="object"&&s!==null&&(n[t]={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={},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 m=o;R(m,n[s])?r[s]=n[s]:t.push({message:`Invalid type for property ${s}: expected ${m}`,path:[s]})}else if(o instanceof i){let m=await o["~standard"].validate(n[s]);"issues"in m?m.issues&&t.push(...m.issues.map(f=>({...f,path:f.path?[s,...f.path]:[s]}))):r[s]=m.value}}}return t.length>0?{issues:t}:{value:r}},types:{input:{},output:{}}}};function d(a){let e;if(a instanceof i)e=a;else if(typeof a=="string")if(a==="string")e=new S;else if(a==="number")e=new p;else if(a==="boolean")e=new l;else throw new Error("Invalid schema type provided to toStandard");else if(typeof a=="object"&&a!==null&&!Array.isArray(a))e=new y(a);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 c={string:()=>new S,number:()=>new p,boolean:()=>new l,null:()=>new v,any:()=>new b,literal:a=>new w(a),object:a=>new y(a||{}),array:a=>d(a).array(),enum:a=>{let e=a[0],n;if(typeof e=="string")n=c.string();else if(typeof e=="number")n=c.number();else if(typeof e=="boolean")n=c.boolean();else throw new Error("Enum values must be primitives");return n.enum(a)},optional:a=>d(a).optional(),options:(...a)=>{let e=a.map(n=>d(n).schema);return new I(...e)},instanceOf:a=>c.object({}).instanceOf(a),uuid:()=>c.string().uuid(),regex:a=>c.string().regex(a),email:()=>c.string().email(),phone:()=>c.string().phone(),toStandard:d};0&&(module.exports={h});
|