UNPKG

@fajarnugraha37/validator

Version:

A typed façade over [AJV](https://ajv.js.org/) that brings schema builders, caching, and loader pipelines to the `nope` workspace

2 lines (1 loc) 4.45 kB
"use strict";var a=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var n=(t,e)=>a(t,"name",{value:e,configurable:!0});var S=(t,e)=>{for(var s in e)a(t,s,{get:e[s],enumerable:!0})},y=(t,e,s,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let h of l(e))!d.call(t,h)&&h!==s&&a(t,h,{get:()=>e[h],enumerable:!(i=p(e,h))||i.enumerable});return t};var f=t=>y(a({},"__esModule",{value:!0}),t);var g={};S(g,{SchemaBuilder:()=>m});module.exports=f(g);var o=n(t=>{let e=globalThis.structuredClone;return typeof e=="function"?e(t):JSON.parse(JSON.stringify(t))},"clone"),r=n(t=>t instanceof m?t.build():o(t),"normalizeInput"),c=n(t=>!t||typeof t!="object"||Array.isArray(t)?{}:t,"ensureObject"),u=n(t=>Array.from(new Set(t)),"unique"),m=class t{static{n(this,"SchemaBuilder")}schema;constructor(e={}){this.schema=o(e)}static create(e={}){return new t(e)}static object(){return new t({type:"object",properties:{},required:[]})}static array(){return new t({type:"array"})}static string(){return new t({type:"string"})}static number(){return new t({type:"number"})}static integer(){return new t({type:"integer"})}static boolean(){return new t({type:"boolean"})}static nil(){return new t({type:"null"})}clone(){return new t(this.build())}extend(e){return Object.assign(this.schema,r(e)),this}title(e){return this.schema.title=e,this}description(e){return this.schema.description=e,this}default(e){return this.schema.default=e,this}examples(...e){return this.schema.examples=e,this}example(e){let s=Array.isArray(this.schema.examples)?this.schema.examples:[];return s.push(e),this.schema.examples=s,this}const(e){return this.schema.const=e,this}enum(e){return this.schema.enum=e,this}format(e){return this.schema.format=e,this}$id(e){return this.schema.$id=e,this}$schema(e){return this.schema.$schema=e,this}ref(e){return this.schema.$ref=e,this}readOnly(e=!0){return this.schema.readOnly=e,this}writeOnly(e=!0){return this.schema.writeOnly=e,this}nullable(e=!0){let s=this.schema.type;return e?Array.isArray(s)?this.schema.type=u([...s,"null"]):s&&s!=="null"?this.schema.type=u([s,"null"]):this.schema.type="null":Array.isArray(s)?this.schema.type=s.filter(i=>i!=="null"):s==="null"&&delete this.schema.type,this}type(e){return this.schema.type=e,this}property(e,s,i={}){let h=c(this.schema.properties);return h[e]=r(s),this.schema.properties=h,i.required&&this.required(e),this}properties(e,s={}){for(let[i,h]of Object.entries(e))this.property(i,h);return s.required&&this.required(...s.required),this}required(...e){let s=Array.isArray(this.schema.required)?this.schema.required:[];return this.schema.required=u([...s,...e]),this}additionalProperties(e){return this.schema.additionalProperties=typeof e=="boolean"?e:r(e),this}patternProperty(e,s){let i=c(this.schema.patternProperties);return i[e]=r(s),this.schema.patternProperties=i,this}propertyNames(e){return this.schema.propertyNames=r(e),this}minProperties(e){return this.schema.minProperties=e,this}maxProperties(e){return this.schema.maxProperties=e,this}items(e){return this.schema.items=r(e),this}prefixItems(...e){return this.schema.prefixItems=e.map(r),this}contains(e){return this.schema.contains=r(e),this}minItems(e){return this.schema.minItems=e,this}maxItems(e){return this.schema.maxItems=e,this}uniqueItems(e=!0){return this.schema.uniqueItems=e,this}minLength(e){return this.schema.minLength=e,this}maxLength(e){return this.schema.maxLength=e,this}pattern(e){return this.schema.pattern=e,this}minimum(e){return this.schema.minimum=e,this}maximum(e){return this.schema.maximum=e,this}exclusiveMinimum(e){return this.schema.exclusiveMinimum=e,this}exclusiveMaximum(e){return this.schema.exclusiveMaximum=e,this}multipleOf(e){return this.schema.multipleOf=e,this}allOf(...e){return this.schema.allOf=e.map(r),this}anyOf(...e){return this.schema.anyOf=e.map(r),this}oneOf(...e){return this.schema.oneOf=e.map(r),this}not(e){return this.schema.not=r(e),this}ifSchema(e){return this.schema.if=r(e),this}thenSchema(e){return this.schema.then=r(e),this}elseSchema(e){return this.schema.else=r(e),this}definition(e,s){let i=c(this.schema.$defs);return i[e]=r(s),this.schema.$defs=i,this}definitions(e){for(let[s,i]of Object.entries(e))this.definition(s,i);return this}meta(e,s){return this.schema[e]=s,this}build(){return o(this.schema)}toJSON(){return this.build()}};0&&(module.exports={SchemaBuilder});