express-zod-api
Version:
A Typescript framework to help you get an API server up and running with I/O schema validation and custom middlewares in minutes.
1 lines • 1.74 kB
JavaScript
import{F as e,U as t}from"./routing-walker-DjzH37jF.js";import*as n from"ramda";const r=e=>e.type===`object`,i=n.mergeDeepWith((e,t)=>{if(Array.isArray(e)&&Array.isArray(t))return n.concat(e,t);if(e===t)return t;throw Error(`Can not flatten properties`,{cause:{a:e,b:t}})}),a=new Set([`type`,`properties`,`required`,`examples`,`description`,`additionalProperties`]),o=e=>{for(let t of Object.keys(e))if(!a.has(t))return!1;return!0},s=n.pair(!0),c=(e,t,r)=>!(`allOf`in e)||!e.allOf?[]:e.allOf.map(e=>{if(t===`throw`&&!(e.type===`object`&&o(e)))throw Error(`Can not merge`);return n.pair(r,e)}),l=e=>{let t=[];return e.anyOf&&t.push(...n.map(s,e.anyOf)),e.oneOf&&t.push(...n.map(s,e.oneOf)),t},u=(e,n,r,i)=>{if(!t(e.propertyNames))return;let a=[];typeof e.propertyNames.const==`string`&&a.push(e.propertyNames.const),e.propertyNames.enum&&a.push(...e.propertyNames.enum.filter(e=>typeof e==`string`));let o={...Object(e.additionalProperties)};for(let e of a)n.properties[e]??=o;i||r.push(...a)},d=(r,i,a)=>{i.examples?.length&&(a?r.examples=n.concat(r.examples||[],i.examples):r.examples=e(r.examples?.filter(t)||[],i.examples.filter(t),([e,t])=>n.mergeDeepRight(e,t)))},f=(e,t=`coerce`)=>{let a=[n.pair(!1,e)],o={type:`object`,properties:{}},s=[];for(let[e,f]of a)f.description&&(o.description??=f.description),a.push(...c(f,t,e)),a.push(...l(f)),d(o,f,e),r(f)&&(a.push([e,{examples:p(f)}]),f.properties&&(o.properties=(t===`throw`?i:n.mergeDeepRight)(o.properties,f.properties),!e&&f.required&&s.push(...f.required)),u(f,o,s,e));return s.length&&(o.required=[...new Set(s)]),o},p=r=>Object.entries(r.properties||{}).reduce((r,[i,a])=>{let{examples:o=[]}=t(a)?a:{};return e(r,o.map(n.objOf(i)),([e,t])=>({...e,...t}))},[]);export{f as t};