@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) • 3.98 kB
JavaScript
var o=Object.defineProperty;var h=(s,e)=>o(s,"name",{value:e,configurable:!0});var c=h(s=>{let e=globalThis.structuredClone;return typeof e=="function"?e(s):JSON.parse(JSON.stringify(s))},"clone"),i=h(s=>s instanceof u?s.build():c(s),"normalizeInput"),a=h(s=>!s||typeof s!="object"||Array.isArray(s)?{}:s,"ensureObject"),m=h(s=>Array.from(new Set(s)),"unique"),u=class s{static{h(this,"SchemaBuilder")}schema;constructor(e={}){this.schema=c(e)}static create(e={}){return new s(e)}static object(){return new s({type:"object",properties:{},required:[]})}static array(){return new s({type:"array"})}static string(){return new s({type:"string"})}static number(){return new s({type:"number"})}static integer(){return new s({type:"integer"})}static boolean(){return new s({type:"boolean"})}static nil(){return new s({type:"null"})}clone(){return new s(this.build())}extend(e){return Object.assign(this.schema,i(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 t=Array.isArray(this.schema.examples)?this.schema.examples:[];return t.push(e),this.schema.examples=t,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 t=this.schema.type;return e?Array.isArray(t)?this.schema.type=m([...t,"null"]):t&&t!=="null"?this.schema.type=m([t,"null"]):this.schema.type="null":Array.isArray(t)?this.schema.type=t.filter(r=>r!=="null"):t==="null"&&delete this.schema.type,this}type(e){return this.schema.type=e,this}property(e,t,r={}){let n=a(this.schema.properties);return n[e]=i(t),this.schema.properties=n,r.required&&this.required(e),this}properties(e,t={}){for(let[r,n]of Object.entries(e))this.property(r,n);return t.required&&this.required(...t.required),this}required(...e){let t=Array.isArray(this.schema.required)?this.schema.required:[];return this.schema.required=m([...t,...e]),this}additionalProperties(e){return this.schema.additionalProperties=typeof e=="boolean"?e:i(e),this}patternProperty(e,t){let r=a(this.schema.patternProperties);return r[e]=i(t),this.schema.patternProperties=r,this}propertyNames(e){return this.schema.propertyNames=i(e),this}minProperties(e){return this.schema.minProperties=e,this}maxProperties(e){return this.schema.maxProperties=e,this}items(e){return this.schema.items=i(e),this}prefixItems(...e){return this.schema.prefixItems=e.map(i),this}contains(e){return this.schema.contains=i(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(i),this}anyOf(...e){return this.schema.anyOf=e.map(i),this}oneOf(...e){return this.schema.oneOf=e.map(i),this}not(e){return this.schema.not=i(e),this}ifSchema(e){return this.schema.if=i(e),this}thenSchema(e){return this.schema.then=i(e),this}elseSchema(e){return this.schema.else=i(e),this}definition(e,t){let r=a(this.schema.$defs);return r[e]=i(t),this.schema.$defs=r,this}definitions(e){for(let[t,r]of Object.entries(e))this.definition(t,r);return this}meta(e,t){return this.schema[e]=t,this}build(){return c(this.schema)}toJSON(){return this.build()}};export{u as SchemaBuilder};