UNPKG

@valibot/to-json-schema

Version:

The official JSON schema converter for Valibot

2 lines (1 loc) 6.04 kB
"use strict";var l=Object.create;var o=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var y=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty;var w=(e,n)=>{for(var r in n)o(e,r,{get:n[r],enumerable:!0})},f=(e,n,r,s)=>{if(n&&typeof n=="object"||typeof n=="function")for(let u of c(n))!k.call(e,u)&&u!==r&&o(e,u,{get:()=>n[u],enumerable:!(s=I(n,u))||s.enumerable});return e};var E=(e,n,r)=>(r=e!=null?l(y(e)):{},f(n||!e||!e.__esModule?o(r,"default",{value:e,enumerable:!0}):r,e)),M=e=>f(o({},"__esModule",{value:!0}),e);var h={};w(h,{toJsonSchema:()=>b});module.exports=M(h);var d=E(require("valibot"),1);function i(e,n){switch(n?.errorMode){case"ignore":break;case"warn":{console.warn(e);break}default:throw new Error(e)}}function g(e,n,r){switch(n.type){case"base64":{e.contentEncoding="base64";break}case"bic":case"cuid2":case"decimal":case"digits":case"emoji":case"hexadecimal":case"hex_color":case"nanoid":case"octal":case"ulid":{e.pattern=n.requirement.source;break}case"description":{e.description=n.description;break}case"email":{e.format="email";break}case"empty":{e.type==="array"?e.maxItems=0:(e.type!=="string"&&i(`The "${n.type}" action is not supported on type "${e.type}".`,r),e.maxLength=0);break}case"integer":{e.type="integer";break}case"ipv4":{e.format="ipv4";break}case"ipv6":{e.format="ipv6";break}case"iso_date":{e.format="date";break}case"iso_date_time":case"iso_timestamp":{e.format="date-time";break}case"iso_time":{e.format="time";break}case"length":{e.type==="array"?(e.minItems=n.requirement,e.maxItems=n.requirement):(e.type!=="string"&&i(`The "${n.type}" action is not supported on type "${e.type}".`,r),e.minLength=n.requirement,e.maxLength=n.requirement);break}case"max_length":{e.type==="array"?e.maxItems=n.requirement:(e.type!=="string"&&i(`The "${n.type}" action is not supported on type "${e.type}".`,r),e.maxLength=n.requirement);break}case"max_value":{e.type!=="number"&&i(`The "max_value" action is not supported on type "${e.type}".`,r),e.maximum=n.requirement;break}case"min_length":{e.type==="array"?e.minItems=n.requirement:(e.type!=="string"&&i(`The "${n.type}" action is not supported on type "${e.type}".`,r),e.minLength=n.requirement);break}case"min_value":{e.type!=="number"&&i(`The "min_value" action is not supported on type "${e.type}".`,r),e.minimum=n.requirement;break}case"multiple_of":{e.multipleOf=n.requirement;break}case"non_empty":{e.type==="array"?e.minItems=1:(e.type!=="string"&&i(`The "${n.type}" action is not supported on type "${e.type}".`,r),e.minLength=1);break}case"regex":{n.requirement.flags&&i("RegExp flags are not supported by JSON Schema.",r),e.pattern=n.requirement.source;break}case"title":{e.title=n.title;break}case"url":{e.format="uri";break}case"uuid":{e.format="uuid";break}case"value":{e.const=n.requirement;break}default:i(`The "${n.type}" action cannot be converted to JSON Schema.`,r)}return e}var O=0;function a(e,n,r,s){let u=s.referenceMap.get(n);if(u&&u in s.definitions)return e.$ref=`#/$defs/${u}`,e;if("pipe"in n){for(let t=0;t<n.pipe.length;t++){let p=n.pipe[t];if(p.kind==="schema"){t>0&&i('A "pipe" with multiple schemas cannot be converted to JSON Schema.',r);let v=a({},p,r,s);if(v.$ref){let m=v.$ref.split("/")[2];Object.assign(e,s.definitions[m])}else Object.assign(e,v)}else e=g(e,p,r)}return e}switch(n.type){case"boolean":{e.type="boolean";break}case"null":{e.type="null";break}case"number":{e.type="number";break}case"string":{e.type="string";break}case"array":{e.type="array",e.items=a({},n.item,r,s);break}case"tuple":case"tuple_with_rest":case"loose_tuple":case"strict_tuple":{e.type="array",e.items=[];for(let t of n.items)e.items.push(a({},t,r,s));n.type==="tuple_with_rest"?e.additionalItems=a({},n.rest,r,s):e.additionalItems=n.type==="loose_tuple";break}case"object":case"object_with_rest":case"loose_object":case"strict_object":{e.type="object",e.properties={},e.required=[];for(let t in n.entries){let p=n.entries[t];e.properties[t]=a({},p,r,s),p.type!=="nullish"&&p.type!=="optional"&&e.required.push(t)}n.type==="object_with_rest"?e.additionalProperties=a({},n.rest,r,s):n.type==="strict_object"&&(e.additionalProperties=!1);break}case"record":{"pipe"in n.key&&i('The "record" schema with a schema for the key that contains a "pipe" cannot be converted to JSON Schema.',r),n.key.type!=="string"&&i(`The "record" schema with the "${n.key.type}" schema for the key cannot be converted to JSON Schema.`,r),e.type="object",e.additionalProperties=a({},n.value,r,s);break}case"any":case"unknown":break;case"nullable":case"nullish":{e.anyOf=[a({},n.wrapped,r,s),{type:"null"}],n.default!==void 0&&(e.default=d.getDefault(n));break}case"exact_optional":case"optional":case"undefinedable":{e=a(e,n.wrapped,r,s),n.default!==void 0&&(e.default=d.getDefault(n));break}case"literal":{typeof n.literal!="boolean"&&typeof n.literal!="number"&&typeof n.literal!="string"&&i('The value of the "literal" schema is not JSON compatible.',r),e.const=n.literal;break}case"enum":{e.enum=n.options;break}case"picklist":{n.options.some(t=>typeof t!="number"&&typeof t!="string")&&i('An option of the "picklist" schema is not JSON compatible.',r),e.enum=n.options;break}case"union":case"variant":{e.anyOf=n.options.map(t=>a({},t,r,s));break}case"intersect":{e.allOf=n.options.map(t=>a({},t,r,s));break}case"lazy":{let t=s.getterMap.get(n.getter);t||(t=n.getter(void 0),s.getterMap.set(n.getter,t));let p=s.referenceMap.get(t);p||(p=`${O++}`,s.referenceMap.set(t,p),s.definitions[p]=a({},t,r,s)),e.$ref=`#/$defs/${p}`;break}default:i(`The "${n.type}" schema cannot be converted to JSON Schema.`,r)}return e}function b(e,n){let r={definitions:{},referenceMap:new Map,getterMap:new Map};if(n?.definitions){for(let u in n.definitions)r.referenceMap.set(n.definitions[u],u);for(let u in n.definitions)r.definitions[u]=a({},n.definitions[u],n,r)}let s=a({$schema:"http://json-schema.org/draft-07/schema#"},e,n,r);return r.referenceMap.size&&(s.$defs=r.definitions),s}0&&(module.exports={toJsonSchema});