react-hook-form-jsonschema
Advanced tools
Comparing version 0.2.0-beta.8 to 0.2.0-beta.9
@@ -10,2 +10,8 @@ # Changelog | ||
## [0.2.0-beta.9] - 2020-02-18 | ||
### Fix | ||
- `notInEnum` to return expected value in `expected` field of the `ErrorMessage`. | ||
## [0.2.0-beta.8] - 2020-02-17 | ||
@@ -12,0 +18,0 @@ |
@@ -40,3 +40,3 @@ import React from 'react'; | ||
getLabelProps(): React.ComponentProps<'label'>; | ||
getItems(): Array<string>; | ||
getItems(): string[]; | ||
getItemInputProps(index: number): React.ComponentProps<'input'>; | ||
@@ -49,3 +49,3 @@ getItemLabelProps(index: number): React.ComponentProps<'label'>; | ||
export interface UseCheckboxReturnType extends BasicInputReturnType { | ||
getItems(): Array<string>; | ||
getItems(): string[]; | ||
getItemInputProps(index: number): React.ComponentProps<'input'>; | ||
@@ -62,3 +62,3 @@ getItemLabelProps(index: number): React.ComponentProps<'label'>; | ||
getItemOptionProps(index: number): React.ComponentProps<'option'>; | ||
getItems(): Array<string>; | ||
getItems(): string[]; | ||
getSelectProps(): React.ComponentProps<'select'>; | ||
@@ -87,3 +87,3 @@ } | ||
export declare type InputReturnTypes = UseRawInputReturnType | UseTextAreaReturnType | UseSelectReturnType | UseRadioReturnType | UseCheckboxReturnType; | ||
export declare type UseObjectReturnType = Array<InputReturnTypes>; | ||
export declare type UseObjectReturnType = InputReturnTypes[]; | ||
export declare type UISchemaType = { | ||
@@ -90,0 +90,0 @@ type: UITypes; |
@@ -1,2 +0,2 @@ | ||
import { JSONSchemaPathInfo } from '../../../JSONSchema'; | ||
import { JSONSchemaPathInfo, JSONSchemaType, JSONSchemaBaseInstanceTypes } from '../../../JSONSchema'; | ||
export declare enum ErrorTypes { | ||
@@ -15,3 +15,3 @@ required = "__form_error_required__", | ||
} | ||
export declare type ErrorMessageValues = boolean | number | string | undefined; | ||
export declare type ErrorMessageValues = JSONSchemaType['enum'] | JSONSchemaBaseInstanceTypes | undefined; | ||
export declare type ErrorMessage = { | ||
@@ -18,0 +18,0 @@ message: ErrorTypes | string; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react-hook-form"),r=require("react"),s=(e=r)&&"object"==typeof e&&"default"in e?e.default:e;const n=(e,t)=>e+"/"+t,a=(e,t)=>{const r=t.split("/");return r[0]===e&&r.shift(),""===r[r.length-1]&&r.pop(),r},o=e=>a("$",e),i=/^[a-z][a-z0-9+.-]*:/i,p=e=>i.test(e),u=/^#(\/(([^#/~])|(~[01]))*)*/i,c=(e,t,r)=>{if(p(e)){const r=new URL(e);if(r.hash)return c(r.hash,t,t[`${r.origin}${r.pathname}`])}else if(n=e,u.test(n)&&r)return(s=e,a("#",s)).reduce((e,t)=>{if(e)return e[t]},r);var s,n;return t[e]},m=(e,t,r)=>{let s={};if(e.$ref){const n=e.$ref;if(r.indexOf(n)>-1)return s;r.push(n),s=Object.assign({},c(n,t))}else s=Object.assign({},e);return Object.keys(s).reduce((e,s)=>("object"!=typeof e[s]||null===e[s]||Array.isArray(e[s])||r.indexOf(e[s].$ref)>-1||(e[s]=m(e[s],t,r.slice())),e),s)},l={integer:e=>parseInt(e),number:e=>parseFloat(e),boolean:e=>"true"===e},d=(e,t)=>Object.keys(t).sort().reduce((r,s)=>{var n;const a=o(s);if(!a||!t[s])return r;let i=r,p=e;for(let e=0;e<a.length&&null!=p;e++){if(p.type&&"object"===p.type&&(p=p.properties,p=p[a[e]]),e===a.length-1){if(!p)break;i[a[e]]=p.type&&l[p.type]?l[p.type](t[s]):null!=(n=t[s])?n:{}}else null==i[a[e]]&&(i[a[e]]={});i=i[a[e]]}return r},{}),h=(e,t,r)=>{const{schema:s}=r,a=o(e).reduce((e,t)=>{const{JSONSchema:r,currentData:s}=e;if(!r||"object"!==r.type||!r.properties)return Object.assign(Object.assign({},e),{JSONSchema:void 0,invalidPointer:!0});const a=!!s,o=s?s[t]:s,i=e.isRequired,p=r.required&&r.required.indexOf(t)>-1;return{JSONSchema:r.properties[t],currentData:o,fatherExists:a,fatherIsRequired:i,invalidPointer:!1,isRequired:p,objectName:t,pointer:n(n(e.pointer,"properties"),t)}},{JSONSchema:s,currentData:t,fatherExists:!0,fatherIsRequired:!0,invalidPointer:!1,isRequired:!0,objectName:"",pointer:"#"});return{JSONSchema:a.JSONSchema,invalidPointer:a.invalidPointer,isRequired:a.fatherIsRequired&&a.isRequired||!a.fatherIsRequired&&a.isRequired&&a.fatherExists,objectName:a.objectName,path:e,pointer:a.pointer}},g=r.createContext(null);function x(){return r.useContext(g)}var y,f,b;(y=exports.InputTypes||(exports.InputTypes={})).generic="generic",y.radio="radio",y.select="select",y.input="input",y.textArea="textArea",y.checkbox="checkbox",(f=exports.UITypes||(exports.UITypes={})).default="default",f.radio="radio",f.select="select",f.input="input",f.hidden="hidden",f.password="password",f.textArea="textArea",f.checkbox="checkbox",(b=exports.ErrorTypes||(exports.ErrorTypes={})).required="__form_error_required__",b.maxLength="__form_error_maxLenght__",b.minLength="__form_error_minLenght__",b.maxValue="__form_error_maxValue__",b.minValue="__form_error_minValue__",b.pattern="__form_error_pattern__",b.notInteger="__form_error_notInteger__",b.notFloat="__form_error_notFloat__",b.multipleOf="__form_error_multipleOf__",b.notInEnum="__form_error_notInEnum",b.undefinedError="__form_error_undefinedError__";const v=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},O=e=>{const t=void 0!==e.multipleOf?"integer"===e.type?parseInt(e.multipleOf):parseFloat(e.multipleOf):"integer"===e.type?1:"any";let r=void 0;if(e.multipleOf){const t=e.multipleOf.toString().split(".")[1];r=t?t.length:0}return[t,r]},_=e=>{const[t]=O(e);let r=void 0!==e.exclusiveMinimum?e.exclusiveMinimum:void 0!==e.minimum?e.minimum:void 0;return void 0!==r&&void 0!==e.exclusiveMinimum&&(r+=t&&"any"!=t?t:1e-4),r},j=e=>{const[t]=O(e);let r=void 0!==e.exclusiveMaximum?parseFloat(e.exclusiveMaximum):void 0!==e.maximum?parseFloat(e.maximum):void 0;return void 0!==r&&void 0!==e.exclusiveMaximum&&(r-=t&&"any"!=t?t:1e-4),r};function I(e,t){return Object.keys(e).reduce((r,s)=>(r[s]=r=>e[s](r,t),r),{})}const T=(e,t)=>{const{JSONSchema:r,isRequired:s}=e,n=Object.keys(t).length>0||r.enum,a=Object.assign({},n?{validate:Object.assign(Object.assign({},I(t,e)),r.enum?{enumValidator:e=>{if(!r.enum||!e)return!0;for(const t of r.enum)if(t==e)return!0;return exports.ErrorTypes.notInEnum}}:void 0)}:void 0);switch(s&&(a.required=exports.ErrorTypes.required),r.type){case"integer":case"number":return((e,t)=>{const r=_(e),s=j(e);return t.validate=Object.assign(Object.assign({},t.validate),{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||exports.ErrorTypes.multipleOf)}),"integer"===e.type?t.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:exports.ErrorTypes.notInteger}:t.pattern={value:/^([+-]?[0-9]+([.][0-9]+))?$/,message:exports.ErrorTypes.notFloat},(r||0===r)&&(t.min={value:r,message:exports.ErrorTypes.minValue}),(s||0===s)&&(t.max={value:s,message:exports.ErrorTypes.maxValue}),t})(r,a);case"string":return i=a,(o=r).minLength&&(i.minLength={value:o.minLength,message:exports.ErrorTypes.minLength}),o.maxLength&&(i.maxLength={value:o.maxLength,message:exports.ErrorTypes.maxLength}),o.pattern&&(i.pattern={value:new RegExp(o.pattern),message:exports.ErrorTypes.pattern}),i;case"boolean":return a;default:return{}}var o,i},E=(e,t)=>h(e,t,x()),k=(e,t,r)=>{var s;const{JSONSchema:n,isRequired:a,objectName:o}=t;let i,p,u;if("number"===n.type||"integer"===n.type){const e=O(n);u=e[0],i=_(n),p=j(n)}return{name:o,path:r,isRequired:a,formContext:e,type:exports.InputTypes.generic,validator:T(t,(s=e.customValidators,null!=s?s:{})),getError:()=>((e,t,r,s,n,a,o,i)=>{if("array"===t.type){const e=s.getValues({nest:!0})[n];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:exports.ErrorTypes.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:exports.ErrorTypes.maxLength,expected:t.maxItems}}}if(!e)return;const p={message:"string"==typeof e.message?e.message:exports.ErrorTypes.undefinedError,expected:void 0};switch(e.message){case exports.ErrorTypes.required:p.message=exports.ErrorTypes.required,p.expected=r;break;case exports.ErrorTypes.maxLength:p.message=exports.ErrorTypes.maxLength,p.expected=t.maxLength;break;case exports.ErrorTypes.minLength:p.message=exports.ErrorTypes.minLength,p.expected=t.minLength;break;case exports.ErrorTypes.maxValue:p.message=exports.ErrorTypes.maxValue,p.expected=a;break;case exports.ErrorTypes.minValue:p.message=exports.ErrorTypes.minValue,p.expected=o;break;case exports.ErrorTypes.multipleOf:p.message=exports.ErrorTypes.multipleOf,p.expected=i;break;case exports.ErrorTypes.pattern:p.message=exports.ErrorTypes.pattern,p.expected=t.pattern}return p})(e.errors[r]?e.errors[r]:void 0,n,a,e,r,i,p,u),getObject:()=>n,getCurrentValue:()=>e.getValues()[r]}},L=e=>{const t=x(),r=d(t.schema,t.getValues()),s=E(e,r);return k(t,s,e)},q=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),S=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,a,o,i,p=[];if("array"===s.type){if(s.items.enum)p=s.items.enum;else if("string"===s.items.type)p=s.enum?s.enum:[];else if("number"===s.items.type||"integer"===s.items.type){const e=O(s);if(o=e[0],i=e[1],n=_(s),a=j(s),void 0!==n&&void 0!==a&&"any"!=o)for(let e=n;e<=a;e+=o)p.push(v(e,i||0))}s.uniqueItems&&(p=[...new Set(p)])}else"boolean"===s.type&&(p=["true"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.checkbox,isSingle:"boolean"===s.type,getItemInputProps:n=>{const a={key:""};return a.name="array"===s.type?`${e.path}[${n}]`:e.path,a.ref=t(r),a.type="checkbox",a.id=q(e.path,n,p),a.value=p[n],a},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,p),r.htmlFor=q(e.path,t,p),r},getItems:()=>p})},P=(e,t)=>e+"-"+t+"-input",V=(e,t)=>{const{register:r}=e.formContext,{validator:s}=e,n=e.getObject();let a,o,i,p;const u={key:""};if("string"===n.type)u.pattern=n.pattern,u.minLength=n.minLength,u.maxLength=n.maxLength;else if("number"===n.type||"integer"===n.type){const e=O(n);i=e[0],p=e[1],a=_(n),o=j(n),u.min=`${a}`,u.max=`${o}`,u.step="any"===i?"any":v(i,p||0)}return Object.assign(Object.assign({},e),{type:exports.InputTypes.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=P(e.path,t),r},getInputProps:()=>(u.name=e.path,u.ref=r(s),u.type=t,u.required=e.isRequired,u.id=P(e.path,t),u)})},R=()=>({}),F=e=>Object.assign(Object.assign({},V(e,"hidden")),{isRequired:!1,getLabelProps:R}),w=e=>{let t;switch(e.getObject().format){case"date-time":t="datetime-local";break;case"email":t="email";break;case"hostname":case"uri":t="url";break;default:t="text"}return V(e,t)},M=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),N=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,a,o,i,p=[];if("string"===s.type)p=s.enum?s.enum:[];else if("number"===s.type||"integer"===s.type){const e=O(s);if(o=e[0],i=e[1],n=_(s),a=j(s),void 0!==n&&void 0!==a&&"any"!=o)for(let e=n;e<=a;e+=o)p.push(v(e,i||0))}else"boolean"===s.type&&(p=["true","false"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.radio,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=void 0!==s.title?s.title:e.path,t},getItemInputProps:s=>{const n={key:""};return n.name=e.path,n.ref=t(r),n.type="radio",n.required=e.isRequired,n.id=M(e.path,s,p),n.value=p[s],n},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,p),r.htmlFor=M(e.path,t,p),r},getItems:()=>p})},$=e=>e+"-select",C=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,a,o,i,p=[""];if("string"===s.type)p=p.concat(s.enum?s.enum:[]);else if("number"===s.type||"integer"===s.type){const e=O(s);if(o=e[0],i=e[1],n=_(s),a=j(s),void 0!==n&&void 0!==a&&"any"!=o)for(let e=n;e<=a;e+=o)p.push(v(e,i||0))}else"boolean"===s.type&&(p=["true","false"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.select,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=$(e.path),t},getSelectProps:()=>{const s={};return s.name=e.path,s.ref=t(r),s.required=e.isRequired,s.id=$(e.path),s},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,p),r.value=p[t],r},getItems:()=>p})},U=e=>V(e,"password"),A=e=>e+"-textarea-input",J=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject(),n={};return"string"===s.type&&(n.minLength=s.minLength,n.maxLength=s.maxLength),Object.assign(Object.assign({},e),{type:exports.InputTypes.textArea,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=A(e.path),t},getTextAreaProps:()=>(n.name=e.path,n.ref=t(r),n.required=e.isRequired,n.id=A(e.path),n)})};function D(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(C(e)):r.push(w(e));break;case"integer":case"number":r.push(w(e));break;case"array":case"boolean":r.push(S(e))}return r}function z(e,t,r,s,a){let o=[];const{JSONSchema:i}=t,p=k(e,t,r);if("object"===i.type){return Object.keys(i.properties).reduce(function(e,t,r,s){return(a,o)=>{const i=t&&t.properties?t.properties[o]:void 0,p=n(e,o),u=h(p,s,r),c=z(r,u,p,i,s);return a.concat(c)}}(r,s,e,a),[])}if(!s)return o.concat(D(p));switch(s.type){case exports.UITypes.default:o=o.concat(D(p));break;case exports.UITypes.checkbox:o.push(S(p));break;case exports.UITypes.hidden:o.push(F(p));break;case exports.UITypes.input:o.push(w(p));break;case exports.UITypes.password:o.push(U(p));break;case exports.UITypes.radio:o.push(N(p));break;case exports.UITypes.select:o.push(C(p));break;case exports.UITypes.textArea:o.push(J(p))}return o}Object.defineProperty(exports,"Controller",{enumerable:!0,get:function(){return t.Controller}}),exports.FormContext=e=>{const{validationMode:a="onSubmit",revalidateMode:o="onChange",submitFocusError:i=!0}=e,u=t.useForm({mode:a,reValidateMode:o,submitFocusError:i}),c=r.useMemo(()=>(e=>{const t=(e,r,s)=>Object.keys(r).reduce((a,o)=>{if("object"==typeof r[o]&&null!==r[o]&&!Array.isArray(r[o]))return Object.assign(Object.assign({},t(n(e,o),r[o],s)),a);const i=r[o];if("$id"===o&&i&&(a[i]=r,!p(i)))try{a[new URL(i,s).href]=r}catch(e){if(!(e instanceof TypeError))throw e}return a},{[e]:r});let r=void 0;if(e.$id&&p(e.$id))try{r=new URL(e.$id)}catch(e){if(r=void 0,!(e instanceof TypeError))throw e}return r?Object.assign({[r.href]:e},t("#",e,r)):t("#",e,r)})(e.schema),[e.schema]),l=r.useMemo(()=>m(e.schema,c,[]),[e.schema,c]),h=r.useMemo(()=>Object.assign(Object.assign({},u),{schema:l,idMap:c,customValidators:e.customValidators}),[u,l,c,e.customValidators]),x={};return x.onSubmit=u.handleSubmit(async(t,r)=>{if(e.onSubmit)return e.onSubmit({data:d(e.schema,t),event:r,methods:h})}),e.noNativeValidate&&(x.noValidate=e.noNativeValidate),s.createElement(g.Provider,{value:h},s.createElement("form",Object.assign({},x),e.children))},exports.concatFormPath=n,exports.useAnnotatedSchemaFromPath=E,exports.useCheckbox=e=>S(L(e)),exports.useHidden=e=>F(L(e)),exports.useInput=e=>w(L(e)),exports.useObject=e=>{const t=x(),r=d(t.schema,t.getValues());return z(t,E(e.path,r),e.path,e.UISchema,r)},exports.useObjectFromForm=e=>d(x().schema,e),exports.usePassword=e=>U(L(e)),exports.useRadio=e=>N(L(e)),exports.useSelect=e=>C(L(e)),exports.useTextArea=e=>J(L(e)); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react-hook-form"),r=require("react"),s=(e=r)&&"object"==typeof e&&"default"in e?e.default:e;const n=(e,t)=>e+"/"+t,a=(e,t)=>{const r=t.split("/");return r[0]===e&&r.shift(),""===r[r.length-1]&&r.pop(),r},o=e=>a("$",e),i=/^[a-z][a-z0-9+.-]*:/i,p=e=>i.test(e),u=/^#(\/(([^#/~])|(~[01]))*)*/i,c=(e,t,r)=>{if(p(e)){const r=new URL(e);if(r.hash)return c(r.hash,t,t[`${r.origin}${r.pathname}`])}else if(n=e,u.test(n)&&r)return(s=e,a("#",s)).reduce((e,t)=>{if(e)return e[t]},r);var s,n;return t[e]},m=(e,t,r)=>{let s={};if(e.$ref){const n=e.$ref;if(r.indexOf(n)>-1)return s;r.push(n),s=Object.assign({},c(n,t))}else s=Object.assign({},e);return Object.keys(s).reduce((e,s)=>("object"!=typeof e[s]||null===e[s]||Array.isArray(e[s])||r.indexOf(e[s].$ref)>-1||(e[s]=m(e[s],t,r.slice())),e),s)},l={integer:e=>parseInt(e),number:e=>parseFloat(e),boolean:e=>"true"===e},d=(e,t)=>Object.keys(t).sort().reduce((r,s)=>{var n;const a=o(s);if(!a||!t[s])return r;let i=r,p=e;for(let e=0;e<a.length&&null!=p;e++){if(p.type&&"object"===p.type&&(p=p.properties,p=p[a[e]]),e===a.length-1){if(!p)break;i[a[e]]=p.type&&l[p.type]?l[p.type](t[s]):null!=(n=t[s])?n:{}}else null==i[a[e]]&&(i[a[e]]={});i=i[a[e]]}return r},{}),g=(e,t,r)=>{const{schema:s}=r,a=o(e).reduce((e,t)=>{const{JSONSchema:r,currentData:s}=e;if(!r||"object"!==r.type||!r.properties)return Object.assign(Object.assign({},e),{JSONSchema:void 0,invalidPointer:!0});const a=!!s,o=s?s[t]:s,i=e.isRequired,p=r.required&&r.required.indexOf(t)>-1;return{JSONSchema:r.properties[t],currentData:o,fatherExists:a,fatherIsRequired:i,invalidPointer:!1,isRequired:p,objectName:t,pointer:n(n(e.pointer,"properties"),t)}},{JSONSchema:s,currentData:t,fatherExists:!0,fatherIsRequired:!0,invalidPointer:!1,isRequired:!0,objectName:"",pointer:"#"});return{JSONSchema:a.JSONSchema,invalidPointer:a.invalidPointer,isRequired:a.fatherIsRequired&&a.isRequired||!a.fatherIsRequired&&a.isRequired&&a.fatherExists,objectName:a.objectName,path:e,pointer:a.pointer}},h=r.createContext(null);function x(){return r.useContext(h)}var y,f,b;(y=exports.InputTypes||(exports.InputTypes={})).generic="generic",y.radio="radio",y.select="select",y.input="input",y.textArea="textArea",y.checkbox="checkbox",(f=exports.UITypes||(exports.UITypes={})).default="default",f.radio="radio",f.select="select",f.input="input",f.hidden="hidden",f.password="password",f.textArea="textArea",f.checkbox="checkbox",(b=exports.ErrorTypes||(exports.ErrorTypes={})).required="__form_error_required__",b.maxLength="__form_error_maxLenght__",b.minLength="__form_error_minLenght__",b.maxValue="__form_error_maxValue__",b.minValue="__form_error_minValue__",b.pattern="__form_error_pattern__",b.notInteger="__form_error_notInteger__",b.notFloat="__form_error_notFloat__",b.multipleOf="__form_error_multipleOf__",b.notInEnum="__form_error_notInEnum",b.undefinedError="__form_error_undefinedError__";const v=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},O=e=>{const t=void 0!==e.multipleOf?"integer"===e.type?parseInt(e.multipleOf):parseFloat(e.multipleOf):"integer"===e.type?1:"any";let r=void 0;if(e.multipleOf){const t=e.multipleOf.toString().split(".")[1];r=t?t.length:0}return[t,r]},_=e=>{const[t]=O(e);let r=void 0!==e.exclusiveMinimum?e.exclusiveMinimum:void 0!==e.minimum?e.minimum:void 0;return void 0!==r&&void 0!==e.exclusiveMinimum&&(r+=t&&"any"!=t?t:1e-4),r},j=e=>{const[t]=O(e);let r=void 0!==e.exclusiveMaximum?parseFloat(e.exclusiveMaximum):void 0!==e.maximum?parseFloat(e.maximum):void 0;return void 0!==r&&void 0!==e.exclusiveMaximum&&(r-=t&&"any"!=t?t:1e-4),r};function I(e,t){return Object.keys(e).reduce((r,s)=>(r[s]=r=>e[s](r,t),r),{})}const T=(e,t)=>{const{JSONSchema:r,isRequired:s}=e,n=Object.keys(t).length>0||r.enum,a=Object.assign({},n?{validate:Object.assign(Object.assign({},I(t,e)),r.enum?{enumValidator:e=>{if(!r.enum||!e)return!0;for(const t of r.enum)if(t==e)return!0;return exports.ErrorTypes.notInEnum}}:void 0)}:void 0);switch(s&&(a.required=exports.ErrorTypes.required),r.type){case"integer":case"number":return((e,t)=>{const r=_(e),s=j(e);return t.validate=Object.assign(Object.assign({},t.validate),{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||exports.ErrorTypes.multipleOf)}),"integer"===e.type?t.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:exports.ErrorTypes.notInteger}:t.pattern={value:/^([+-]?[0-9]+([.][0-9]+))?$/,message:exports.ErrorTypes.notFloat},(r||0===r)&&(t.min={value:r,message:exports.ErrorTypes.minValue}),(s||0===s)&&(t.max={value:s,message:exports.ErrorTypes.maxValue}),t})(r,a);case"string":return i=a,(o=r).minLength&&(i.minLength={value:o.minLength,message:exports.ErrorTypes.minLength}),o.maxLength&&(i.maxLength={value:o.maxLength,message:exports.ErrorTypes.maxLength}),o.pattern&&(i.pattern={value:new RegExp(o.pattern),message:exports.ErrorTypes.pattern}),i;case"boolean":return a;default:return{}}var o,i},E=(e,t)=>g(e,t,x()),k=(e,t,r)=>{var s;const{JSONSchema:n,isRequired:a,objectName:o}=t;let i,p,u;if("number"===n.type||"integer"===n.type){const e=O(n);u=e[0],i=_(n),p=j(n)}return{name:o,path:r,isRequired:a,formContext:e,type:exports.InputTypes.generic,validator:T(t,(s=e.customValidators,null!=s?s:{})),getError:()=>((e,t,r,s,n,a,o,i)=>{if("array"===t.type){const e=s.getValues({nest:!0})[n];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:exports.ErrorTypes.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:exports.ErrorTypes.maxLength,expected:t.maxItems}}}if(!e)return;const p={message:"string"==typeof e.message?e.message:exports.ErrorTypes.undefinedError,expected:void 0};switch(e.message){case exports.ErrorTypes.required:p.message=exports.ErrorTypes.required,p.expected=r;break;case exports.ErrorTypes.maxLength:p.message=exports.ErrorTypes.maxLength,p.expected=t.maxLength;break;case exports.ErrorTypes.minLength:p.message=exports.ErrorTypes.minLength,p.expected=t.minLength;break;case exports.ErrorTypes.maxValue:p.message=exports.ErrorTypes.maxValue,p.expected=o;break;case exports.ErrorTypes.minValue:p.message=exports.ErrorTypes.minValue,p.expected=a;break;case exports.ErrorTypes.multipleOf:p.message=exports.ErrorTypes.multipleOf,p.expected=i;break;case exports.ErrorTypes.pattern:p.message=exports.ErrorTypes.pattern,p.expected=t.pattern;break;case exports.ErrorTypes.notInEnum:p.message=exports.ErrorTypes.notInEnum,p.expected=t.enum}return p})(e.errors[r]?e.errors[r]:void 0,n,a,e,r,i,p,u),getObject:()=>n,getCurrentValue:()=>e.getValues()[r]}},L=e=>{const t=x(),r=d(t.schema,t.getValues()),s=E(e,r);return k(t,s,e)},q=e=>e?e.toString():"",S=e=>e.enum?e.enum.map(q):[],P=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),V=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,a,o,i,p=[];if("array"===s.type){if(s.items.enum)p=S(s.items);else if("string"===s.items.type)p=S(s);else if("number"===s.items.type||"integer"===s.items.type){const e=O(s);if(o=e[0],i=e[1],n=_(s),a=j(s),void 0!==n&&void 0!==a&&"any"!=o)for(let e=n;e<=a;e+=o)p.push(v(e,i||0))}s.uniqueItems&&(p=[...new Set(p)])}else"boolean"===s.type&&(p=["true"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.checkbox,isSingle:"boolean"===s.type,getItemInputProps:n=>{const a={key:""};return a.name="array"===s.type?`${e.path}[${n}]`:e.path,a.ref=t(r),a.type="checkbox",a.id=P(e.path,n,p),a.value=p[n],a},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,p),r.htmlFor=P(e.path,t,p),r},getItems:()=>p})},R=(e,t)=>e+"-"+t+"-input",F=(e,t)=>{const{register:r}=e.formContext,{validator:s}=e,n=e.getObject();let a,o,i,p;const u={key:""};if("string"===n.type)u.pattern=n.pattern,u.minLength=n.minLength,u.maxLength=n.maxLength;else if("number"===n.type||"integer"===n.type){const e=O(n);i=e[0],p=e[1],a=_(n),o=j(n),u.min=`${a}`,u.max=`${o}`,u.step="any"===i?"any":v(i,p||0)}return Object.assign(Object.assign({},e),{type:exports.InputTypes.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=R(e.path,t),r},getInputProps:()=>(u.name=e.path,u.ref=r(s),u.type=t,u.required=e.isRequired,u.id=R(e.path,t),u)})},w=()=>({}),M=e=>Object.assign(Object.assign({},F(e,"hidden")),{isRequired:!1,getLabelProps:w}),N=e=>{const t=e.getObject();let r="text";if("string"===t.type)switch(t.format){case"date-time":r="datetime-local";break;case"email":r="email";break;case"hostname":case"uri":r="url"}else"integer"!==t.type&&"number"!==t.type||(r="number");return F(e,r)},$=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),C=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,a,o,i,p=[];if("string"===s.type)p=S(s);else if("number"===s.type||"integer"===s.type){const e=O(s);if(o=e[0],i=e[1],n=_(s),a=j(s),void 0!==n&&void 0!==a&&"any"!=o)for(let e=n;e<=a;e+=o)p.push(v(e,i||0))}else"boolean"===s.type&&(p=["true","false"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.radio,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=void 0!==s.title?s.title:e.path,t},getItemInputProps:s=>{const n={key:""};return n.name=e.path,n.ref=t(r),n.type="radio",n.required=e.isRequired,n.id=$(e.path,s,p),n.value=p[s],n},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,p),r.htmlFor=$(e.path,t,p),r},getItems:()=>p})},U=e=>e+"-select",A=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,a,o,i,p=[""];if("string"===s.type)p=p.concat(S(s));else if("number"===s.type||"integer"===s.type){const e=O(s);if(o=e[0],i=e[1],n=_(s),a=j(s),void 0!==n&&void 0!==a&&"any"!=o)for(let e=n;e<=a;e+=o)p.push(v(e,i||0))}else"boolean"===s.type&&(p=["true","false"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.select,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=U(e.path),t},getSelectProps:()=>{const s={};return s.name=e.path,s.ref=t(r),s.required=e.isRequired,s.id=U(e.path),s},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,p),r.value=p[t],r},getItems:()=>p})},J=e=>F(e,"password"),D=e=>e+"-textarea-input",z=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject(),n={};return"string"===s.type&&(n.minLength=s.minLength,n.maxLength=s.maxLength),Object.assign(Object.assign({},e),{type:exports.InputTypes.textArea,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=D(e.path),t},getTextAreaProps:()=>(n.name=e.path,n.ref=t(r),n.required=e.isRequired,n.id=D(e.path),n)})};function H(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(A(e)):r.push(N(e));break;case"integer":case"number":r.push(N(e));break;case"array":case"boolean":r.push(V(e))}return r}function B(e,t,r,s,a){let o=[];const{JSONSchema:i}=t,p=k(e,t,r);if("object"===i.type){return Object.keys(i.properties).reduce(function(e,t,r,s){return(a,o)=>{const i=t&&t.properties?t.properties[o]:void 0,p=n(e,o),u=g(p,s,r),c=B(r,u,p,i,s);return a.concat(c)}}(r,s,e,a),[])}if(!s)return o.concat(H(p));switch(s.type){case exports.UITypes.default:o=o.concat(H(p));break;case exports.UITypes.checkbox:o.push(V(p));break;case exports.UITypes.hidden:o.push(M(p));break;case exports.UITypes.input:o.push(N(p));break;case exports.UITypes.password:o.push(J(p));break;case exports.UITypes.radio:o.push(C(p));break;case exports.UITypes.select:o.push(A(p));break;case exports.UITypes.textArea:o.push(z(p))}return o}Object.defineProperty(exports,"Controller",{enumerable:!0,get:function(){return t.Controller}}),exports.FormContext=e=>{const{validationMode:a="onSubmit",revalidateMode:o="onChange",submitFocusError:i=!0}=e,u=t.useForm({mode:a,reValidateMode:o,submitFocusError:i}),c=r.useMemo(()=>(e=>{const t=(e,r,s)=>Object.keys(r).reduce((a,o)=>{if("object"==typeof r[o]&&null!==r[o]&&!Array.isArray(r[o]))return Object.assign(Object.assign({},t(n(e,o),r[o],s)),a);const i=r[o];if("$id"===o&&i&&(a[i]=r,!p(i)))try{a[new URL(i,s).href]=r}catch(e){if(!(e instanceof TypeError))throw e}return a},{[e]:r});let r=void 0;if(e.$id&&p(e.$id))try{r=new URL(e.$id)}catch(e){if(r=void 0,!(e instanceof TypeError))throw e}return r?Object.assign({[r.href]:e},t("#",e,r)):t("#",e,r)})(e.schema),[e.schema]),l=r.useMemo(()=>m(e.schema,c,[]),[e.schema,c]),g=r.useMemo(()=>Object.assign(Object.assign({},u),{schema:l,idMap:c,customValidators:e.customValidators}),[u,l,c,e.customValidators]),x={};return x.onSubmit=u.handleSubmit(async(t,r)=>{if(e.onSubmit)return e.onSubmit({data:d(e.schema,t),event:r,methods:g})}),e.noNativeValidate&&(x.noValidate=e.noNativeValidate),s.createElement(h.Provider,{value:g},s.createElement("form",Object.assign({},x),e.children))},exports.concatFormPath=n,exports.useAnnotatedSchemaFromPath=E,exports.useCheckbox=e=>V(L(e)),exports.useHidden=e=>M(L(e)),exports.useInput=e=>N(L(e)),exports.useObject=e=>{const t=x(),r=d(t.schema,t.getValues());return B(t,E(e.path,r),e.path,e.UISchema,r)},exports.useObjectFromForm=e=>d(x().schema,e),exports.usePassword=e=>J(L(e)),exports.useRadio=e=>C(L(e)),exports.useSelect=e=>A(L(e)),exports.useTextArea=e=>z(L(e)); |
@@ -1,1 +0,1 @@ | ||
import{useForm as e}from"react-hook-form";export{Controller}from"react-hook-form";import t,{createContext as r,useMemo as n,useContext as a}from"react";const i=(e,t)=>e+"/"+t,s=(e,t)=>{const r=t.split("/");return r[0]===e&&r.shift(),""===r[r.length-1]&&r.pop(),r},o=e=>s("$",e),u=/^[a-z][a-z0-9+.-]*:/i,c=e=>u.test(e),m=/^#(\/(([^#/~])|(~[01]))*)*/i,l=(e,t,r)=>{if(c(e)){const r=new URL(e);if(r.hash)return l(r.hash,t,t[`${r.origin}${r.pathname}`])}else if(a=e,m.test(a)&&r)return(n=e,s("#",n)).reduce((e,t)=>{if(e)return e[t]},r);var n,a;return t[e]},p=(e,t,r)=>{let n={};if(e.$ref){const a=e.$ref;if(r.indexOf(a)>-1)return n;r.push(a),n=Object.assign({},l(a,t))}else n=Object.assign({},e);return Object.keys(n).reduce((e,n)=>("object"!=typeof e[n]||null===e[n]||Array.isArray(e[n])||r.indexOf(e[n].$ref)>-1||(e[n]=p(e[n],t,r.slice())),e),n)},d={integer:e=>parseInt(e),number:e=>parseFloat(e),boolean:e=>"true"===e},h=(e,t)=>Object.keys(t).sort().reduce((r,n)=>{var a;const i=o(n);if(!i||!t[n])return r;let s=r,u=e;for(let e=0;e<i.length&&null!=u;e++){if(u.type&&"object"===u.type&&(u=u.properties,u=u[i[e]]),e===i.length-1){if(!u)break;s[i[e]]=u.type&&d[u.type]?d[u.type](t[n]):null!=(a=t[n])?a:{}}else null==s[i[e]]&&(s[i[e]]={});s=s[i[e]]}return r},{}),g=(e,t,r)=>{const{schema:n}=r,a=o(e).reduce((e,t)=>{const{JSONSchema:r,currentData:n}=e;if(!r||"object"!==r.type||!r.properties)return Object.assign(Object.assign({},e),{JSONSchema:void 0,invalidPointer:!0});const a=!!n,s=n?n[t]:n,o=e.isRequired,u=r.required&&r.required.indexOf(t)>-1;return{JSONSchema:r.properties[t],currentData:s,fatherExists:a,fatherIsRequired:o,invalidPointer:!1,isRequired:u,objectName:t,pointer:i(i(e.pointer,"properties"),t)}},{JSONSchema:n,currentData:t,fatherExists:!0,fatherIsRequired:!0,invalidPointer:!1,isRequired:!0,objectName:"",pointer:"#"});return{JSONSchema:a.JSONSchema,invalidPointer:a.invalidPointer,isRequired:a.fatherIsRequired&&a.isRequired||!a.fatherIsRequired&&a.isRequired&&a.fatherExists,objectName:a.objectName,path:e,pointer:a.pointer}},f=r(null);function b(){return a(f)}const x=r=>{const{validationMode:a="onSubmit",revalidateMode:s="onChange",submitFocusError:o=!0}=r,u=e({mode:a,reValidateMode:s,submitFocusError:o}),m=n(()=>(e=>{const t=(e,r,n)=>Object.keys(r).reduce((a,s)=>{if("object"==typeof r[s]&&null!==r[s]&&!Array.isArray(r[s]))return Object.assign(Object.assign({},t(i(e,s),r[s],n)),a);const o=r[s];if("$id"===s&&o&&(a[o]=r,!c(o)))try{a[new URL(o,n).href]=r}catch(e){if(!(e instanceof TypeError))throw e}return a},{[e]:r});let r=void 0;if(e.$id&&c(e.$id))try{r=new URL(e.$id)}catch(e){if(r=void 0,!(e instanceof TypeError))throw e}return r?Object.assign({[r.href]:e},t("#",e,r)):t("#",e,r)})(r.schema),[r.schema]),l=n(()=>p(r.schema,m,[]),[r.schema,m]),d=n(()=>Object.assign(Object.assign({},u),{schema:l,idMap:m,customValidators:r.customValidators}),[u,l,m,r.customValidators]),g={};return g.onSubmit=u.handleSubmit(async(e,t)=>{if(r.onSubmit)return r.onSubmit({data:h(r.schema,e),event:t,methods:d})}),r.noNativeValidate&&(g.noValidate=r.noNativeValidate),t.createElement(f.Provider,{value:d},t.createElement("form",Object.assign({},g),r.children))};var y,v,O;!function(e){e.generic="generic",e.radio="radio",e.select="select",e.input="input",e.textArea="textArea",e.checkbox="checkbox"}(y||(y={})),function(e){e.default="default",e.radio="radio",e.select="select",e.input="input",e.hidden="hidden",e.password="password",e.textArea="textArea",e.checkbox="checkbox"}(v||(v={})),function(e){e.required="__form_error_required__",e.maxLength="__form_error_maxLenght__",e.minLength="__form_error_minLenght__",e.maxValue="__form_error_maxValue__",e.minValue="__form_error_minValue__",e.pattern="__form_error_pattern__",e.notInteger="__form_error_notInteger__",e.notFloat="__form_error_notFloat__",e.multipleOf="__form_error_multipleOf__",e.notInEnum="__form_error_notInEnum",e.undefinedError="__form_error_undefinedError__"}(O||(O={}));const _=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},j=e=>{const t=void 0!==e.multipleOf?"integer"===e.type?parseInt(e.multipleOf):parseFloat(e.multipleOf):"integer"===e.type?1:"any";let r=void 0;if(e.multipleOf){const t=e.multipleOf.toString().split(".")[1];r=t?t.length:0}return[t,r]},k=e=>{const[t]=j(e);let r=void 0!==e.exclusiveMinimum?e.exclusiveMinimum:void 0!==e.minimum?e.minimum:void 0;return void 0!==r&&void 0!==e.exclusiveMinimum&&(r+=t&&"any"!=t?t:1e-4),r},L=e=>{const[t]=j(e);let r=void 0!==e.exclusiveMaximum?parseFloat(e.exclusiveMaximum):void 0!==e.maximum?parseFloat(e.maximum):void 0;return void 0!==r&&void 0!==e.exclusiveMaximum&&(r-=t&&"any"!=t?t:1e-4),r};function I(e,t){return Object.keys(e).reduce((r,n)=>(r[n]=r=>e[n](r,t),r),{})}const q=(e,t)=>{const{JSONSchema:r,isRequired:n}=e,a=Object.keys(t).length>0||r.enum,i=Object.assign({},a?{validate:Object.assign(Object.assign({},I(t,e)),r.enum?{enumValidator:e=>{if(!r.enum||!e)return!0;for(const t of r.enum)if(t==e)return!0;return O.notInEnum}}:void 0)}:void 0);switch(n&&(i.required=O.required),r.type){case"integer":case"number":return((e,t)=>{const r=k(e),n=L(e);return t.validate=Object.assign(Object.assign({},t.validate),{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||O.multipleOf)}),"integer"===e.type?t.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:O.notInteger}:t.pattern={value:/^([+-]?[0-9]+([.][0-9]+))?$/,message:O.notFloat},(r||0===r)&&(t.min={value:r,message:O.minValue}),(n||0===n)&&(t.max={value:n,message:O.maxValue}),t})(r,i);case"string":return o=i,(s=r).minLength&&(o.minLength={value:s.minLength,message:O.minLength}),s.maxLength&&(o.maxLength={value:s.maxLength,message:O.maxLength}),s.pattern&&(o.pattern={value:new RegExp(s.pattern),message:O.pattern}),o;case"boolean":return i;default:return{}}var s,o},S=(e,t)=>g(e,t,b()),V=e=>h(b().schema,e),R=(e,t,r)=>{var n;const{JSONSchema:a,isRequired:i,objectName:s}=t;let o,u,c;if("number"===a.type||"integer"===a.type){const e=j(a);c=e[0],o=k(a),u=L(a)}return{name:s,path:r,isRequired:i,formContext:e,type:y.generic,validator:q(t,(n=e.customValidators,null!=n?n:{})),getError:()=>((e,t,r,n,a,i,s,o)=>{if("array"===t.type){const e=n.getValues({nest:!0})[a];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:O.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:O.maxLength,expected:t.maxItems}}}if(!e)return;const u={message:"string"==typeof e.message?e.message:O.undefinedError,expected:void 0};switch(e.message){case O.required:u.message=O.required,u.expected=r;break;case O.maxLength:u.message=O.maxLength,u.expected=t.maxLength;break;case O.minLength:u.message=O.minLength,u.expected=t.minLength;break;case O.maxValue:u.message=O.maxValue,u.expected=i;break;case O.minValue:u.message=O.minValue,u.expected=s;break;case O.multipleOf:u.message=O.multipleOf,u.expected=o;break;case O.pattern:u.message=O.pattern,u.expected=t.pattern}return u})(e.errors[r]?e.errors[r]:void 0,a,i,e,r,o,u,c),getObject:()=>a,getCurrentValue:()=>e.getValues()[r]}},P=e=>{const t=b(),r=h(t.schema,t.getValues()),n=S(e,r);return R(t,n,e)},w=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),E=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,u=[];if("array"===n.type){if(n.items.enum)u=n.items.enum;else if("string"===n.items.type)u=n.enum?n.enum:[];else if("number"===n.items.type||"integer"===n.items.type){const e=j(n);if(s=e[0],o=e[1],a=k(n),i=L(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)u.push(_(e,o||0))}n.uniqueItems&&(u=[...new Set(u)])}else"boolean"===n.type&&(u=["true"]);return Object.assign(Object.assign({},e),{type:y.checkbox,isSingle:"boolean"===n.type,getItemInputProps:a=>{const i={key:""};return i.name="array"===n.type?`${e.path}[${a}]`:e.path,i.ref=t(r),i.type="checkbox",i.id=w(e.path,a,u),i.value=u[a],i},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,u),r.htmlFor=w(e.path,t,u),r},getItems:()=>u})},N=e=>E(P(e)),$=(e,t)=>e+"-"+t+"-input",F=(e,t)=>{const{register:r}=e.formContext,{validator:n}=e,a=e.getObject();let i,s,o,u;const c={key:""};if("string"===a.type)c.pattern=a.pattern,c.minLength=a.minLength,c.maxLength=a.maxLength;else if("number"===a.type||"integer"===a.type){const e=j(a);o=e[0],u=e[1],i=k(a),s=L(a),c.min=`${i}`,c.max=`${s}`,c.step="any"===o?"any":_(o,u||0)}return Object.assign(Object.assign({},e),{type:y.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=$(e.path,t),r},getInputProps:()=>(c.name=e.path,c.ref=r(n),c.type=t,c.required=e.isRequired,c.id=$(e.path,t),c)})},M=()=>({}),A=e=>Object.assign(Object.assign({},F(e,"hidden")),{isRequired:!1,getLabelProps:M}),C=e=>A(P(e)),J=e=>{let t;switch(e.getObject().format){case"date-time":t="datetime-local";break;case"email":t="email";break;case"hostname":case"uri":t="url";break;default:t="text"}return F(e,t)},U=e=>J(P(e)),D=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),T=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,u=[];if("string"===n.type)u=n.enum?n.enum:[];else if("number"===n.type||"integer"===n.type){const e=j(n);if(s=e[0],o=e[1],a=k(n),i=L(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)u.push(_(e,o||0))}else"boolean"===n.type&&(u=["true","false"]);return Object.assign(Object.assign({},e),{type:y.radio,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=void 0!==n.title?n.title:e.path,t},getItemInputProps:n=>{const a={key:""};return a.name=e.path,a.ref=t(r),a.type="radio",a.required=e.isRequired,a.id=D(e.path,n,u),a.value=u[n],a},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,u),r.htmlFor=D(e.path,t,u),r},getItems:()=>u})},z=e=>T(P(e)),B=e=>e+"-select",G=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,u=[""];if("string"===n.type)u=u.concat(n.enum?n.enum:[]);else if("number"===n.type||"integer"===n.type){const e=j(n);if(s=e[0],o=e[1],a=k(n),i=L(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)u.push(_(e,o||0))}else"boolean"===n.type&&(u=["true","false"]);return Object.assign(Object.assign({},e),{type:y.select,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=B(e.path),t},getSelectProps:()=>{const n={};return n.name=e.path,n.ref=t(r),n.required=e.isRequired,n.id=B(e.path),n},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,u),r.value=u[t],r},getItems:()=>u})},H=e=>G(P(e)),K=e=>F(e,"password"),Q=e=>K(P(e)),W=e=>e+"-textarea-input",X=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject(),a={};return"string"===n.type&&(a.minLength=n.minLength,a.maxLength=n.maxLength),Object.assign(Object.assign({},e),{type:y.textArea,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=W(e.path),t},getTextAreaProps:()=>(a.name=e.path,a.ref=t(r),a.required=e.isRequired,a.id=W(e.path),a)})},Y=e=>X(P(e));function Z(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(G(e)):r.push(J(e));break;case"integer":case"number":r.push(J(e));break;case"array":case"boolean":r.push(E(e))}return r}function ee(e,t,r,n,a){let s=[];const{JSONSchema:o}=t,u=R(e,t,r);if("object"===o.type){return Object.keys(o.properties).reduce(function(e,t,r,n){return(a,s)=>{const o=t&&t.properties?t.properties[s]:void 0,u=i(e,s),c=g(u,n,r),m=ee(r,c,u,o,n);return a.concat(m)}}(r,n,e,a),[])}if(!n)return s.concat(Z(u));switch(n.type){case v.default:s=s.concat(Z(u));break;case v.checkbox:s.push(E(u));break;case v.hidden:s.push(A(u));break;case v.input:s.push(J(u));break;case v.password:s.push(K(u));break;case v.radio:s.push(T(u));break;case v.select:s.push(G(u));break;case v.textArea:s.push(X(u))}return s}const te=e=>{const t=b(),r=h(t.schema,t.getValues());return ee(t,S(e.path,r),e.path,e.UISchema,r)};export{O as ErrorTypes,x as FormContext,y as InputTypes,v as UITypes,i as concatFormPath,S as useAnnotatedSchemaFromPath,N as useCheckbox,C as useHidden,U as useInput,te as useObject,V as useObjectFromForm,Q as usePassword,z as useRadio,H as useSelect,Y as useTextArea}; | ||
import{useForm as e}from"react-hook-form";export{Controller}from"react-hook-form";import t,{createContext as r,useMemo as n,useContext as a}from"react";const i=(e,t)=>e+"/"+t,s=(e,t)=>{const r=t.split("/");return r[0]===e&&r.shift(),""===r[r.length-1]&&r.pop(),r},o=e=>s("$",e),c=/^[a-z][a-z0-9+.-]*:/i,u=e=>c.test(e),m=/^#(\/(([^#/~])|(~[01]))*)*/i,p=(e,t,r)=>{if(u(e)){const r=new URL(e);if(r.hash)return p(r.hash,t,t[`${r.origin}${r.pathname}`])}else if(a=e,m.test(a)&&r)return(n=e,s("#",n)).reduce((e,t)=>{if(e)return e[t]},r);var n,a;return t[e]},l=(e,t,r)=>{let n={};if(e.$ref){const a=e.$ref;if(r.indexOf(a)>-1)return n;r.push(a),n=Object.assign({},p(a,t))}else n=Object.assign({},e);return Object.keys(n).reduce((e,n)=>("object"!=typeof e[n]||null===e[n]||Array.isArray(e[n])||r.indexOf(e[n].$ref)>-1||(e[n]=l(e[n],t,r.slice())),e),n)},d={integer:e=>parseInt(e),number:e=>parseFloat(e),boolean:e=>"true"===e},g=(e,t)=>Object.keys(t).sort().reduce((r,n)=>{var a;const i=o(n);if(!i||!t[n])return r;let s=r,c=e;for(let e=0;e<i.length&&null!=c;e++){if(c.type&&"object"===c.type&&(c=c.properties,c=c[i[e]]),e===i.length-1){if(!c)break;s[i[e]]=c.type&&d[c.type]?d[c.type](t[n]):null!=(a=t[n])?a:{}}else null==s[i[e]]&&(s[i[e]]={});s=s[i[e]]}return r},{}),h=(e,t,r)=>{const{schema:n}=r,a=o(e).reduce((e,t)=>{const{JSONSchema:r,currentData:n}=e;if(!r||"object"!==r.type||!r.properties)return Object.assign(Object.assign({},e),{JSONSchema:void 0,invalidPointer:!0});const a=!!n,s=n?n[t]:n,o=e.isRequired,c=r.required&&r.required.indexOf(t)>-1;return{JSONSchema:r.properties[t],currentData:s,fatherExists:a,fatherIsRequired:o,invalidPointer:!1,isRequired:c,objectName:t,pointer:i(i(e.pointer,"properties"),t)}},{JSONSchema:n,currentData:t,fatherExists:!0,fatherIsRequired:!0,invalidPointer:!1,isRequired:!0,objectName:"",pointer:"#"});return{JSONSchema:a.JSONSchema,invalidPointer:a.invalidPointer,isRequired:a.fatherIsRequired&&a.isRequired||!a.fatherIsRequired&&a.isRequired&&a.fatherExists,objectName:a.objectName,path:e,pointer:a.pointer}},f=r(null);function b(){return a(f)}const y=r=>{const{validationMode:a="onSubmit",revalidateMode:s="onChange",submitFocusError:o=!0}=r,c=e({mode:a,reValidateMode:s,submitFocusError:o}),m=n(()=>(e=>{const t=(e,r,n)=>Object.keys(r).reduce((a,s)=>{if("object"==typeof r[s]&&null!==r[s]&&!Array.isArray(r[s]))return Object.assign(Object.assign({},t(i(e,s),r[s],n)),a);const o=r[s];if("$id"===s&&o&&(a[o]=r,!u(o)))try{a[new URL(o,n).href]=r}catch(e){if(!(e instanceof TypeError))throw e}return a},{[e]:r});let r=void 0;if(e.$id&&u(e.$id))try{r=new URL(e.$id)}catch(e){if(r=void 0,!(e instanceof TypeError))throw e}return r?Object.assign({[r.href]:e},t("#",e,r)):t("#",e,r)})(r.schema),[r.schema]),p=n(()=>l(r.schema,m,[]),[r.schema,m]),d=n(()=>Object.assign(Object.assign({},c),{schema:p,idMap:m,customValidators:r.customValidators}),[c,p,m,r.customValidators]),h={};return h.onSubmit=c.handleSubmit(async(e,t)=>{if(r.onSubmit)return r.onSubmit({data:g(r.schema,e),event:t,methods:d})}),r.noNativeValidate&&(h.noValidate=r.noNativeValidate),t.createElement(f.Provider,{value:d},t.createElement("form",Object.assign({},h),r.children))};var x,v,O;!function(e){e.generic="generic",e.radio="radio",e.select="select",e.input="input",e.textArea="textArea",e.checkbox="checkbox"}(x||(x={})),function(e){e.default="default",e.radio="radio",e.select="select",e.input="input",e.hidden="hidden",e.password="password",e.textArea="textArea",e.checkbox="checkbox"}(v||(v={})),function(e){e.required="__form_error_required__",e.maxLength="__form_error_maxLenght__",e.minLength="__form_error_minLenght__",e.maxValue="__form_error_maxValue__",e.minValue="__form_error_minValue__",e.pattern="__form_error_pattern__",e.notInteger="__form_error_notInteger__",e.notFloat="__form_error_notFloat__",e.multipleOf="__form_error_multipleOf__",e.notInEnum="__form_error_notInEnum",e.undefinedError="__form_error_undefinedError__"}(O||(O={}));const _=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},j=e=>{const t=void 0!==e.multipleOf?"integer"===e.type?parseInt(e.multipleOf):parseFloat(e.multipleOf):"integer"===e.type?1:"any";let r=void 0;if(e.multipleOf){const t=e.multipleOf.toString().split(".")[1];r=t?t.length:0}return[t,r]},k=e=>{const[t]=j(e);let r=void 0!==e.exclusiveMinimum?e.exclusiveMinimum:void 0!==e.minimum?e.minimum:void 0;return void 0!==r&&void 0!==e.exclusiveMinimum&&(r+=t&&"any"!=t?t:1e-4),r},L=e=>{const[t]=j(e);let r=void 0!==e.exclusiveMaximum?parseFloat(e.exclusiveMaximum):void 0!==e.maximum?parseFloat(e.maximum):void 0;return void 0!==r&&void 0!==e.exclusiveMaximum&&(r-=t&&"any"!=t?t:1e-4),r};function I(e,t){return Object.keys(e).reduce((r,n)=>(r[n]=r=>e[n](r,t),r),{})}const q=(e,t)=>{const{JSONSchema:r,isRequired:n}=e,a=Object.keys(t).length>0||r.enum,i=Object.assign({},a?{validate:Object.assign(Object.assign({},I(t,e)),r.enum?{enumValidator:e=>{if(!r.enum||!e)return!0;for(const t of r.enum)if(t==e)return!0;return O.notInEnum}}:void 0)}:void 0);switch(n&&(i.required=O.required),r.type){case"integer":case"number":return((e,t)=>{const r=k(e),n=L(e);return t.validate=Object.assign(Object.assign({},t.validate),{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||O.multipleOf)}),"integer"===e.type?t.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:O.notInteger}:t.pattern={value:/^([+-]?[0-9]+([.][0-9]+))?$/,message:O.notFloat},(r||0===r)&&(t.min={value:r,message:O.minValue}),(n||0===n)&&(t.max={value:n,message:O.maxValue}),t})(r,i);case"string":return o=i,(s=r).minLength&&(o.minLength={value:s.minLength,message:O.minLength}),s.maxLength&&(o.maxLength={value:s.maxLength,message:O.maxLength}),s.pattern&&(o.pattern={value:new RegExp(s.pattern),message:O.pattern}),o;case"boolean":return i;default:return{}}var s,o},S=(e,t)=>h(e,t,b()),V=e=>g(b().schema,e),R=(e,t,r)=>{var n;const{JSONSchema:a,isRequired:i,objectName:s}=t;let o,c,u;if("number"===a.type||"integer"===a.type){const e=j(a);u=e[0],o=k(a),c=L(a)}return{name:s,path:r,isRequired:i,formContext:e,type:x.generic,validator:q(t,(n=e.customValidators,null!=n?n:{})),getError:()=>((e,t,r,n,a,i,s,o)=>{if("array"===t.type){const e=n.getValues({nest:!0})[a];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:O.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:O.maxLength,expected:t.maxItems}}}if(!e)return;const c={message:"string"==typeof e.message?e.message:O.undefinedError,expected:void 0};switch(e.message){case O.required:c.message=O.required,c.expected=r;break;case O.maxLength:c.message=O.maxLength,c.expected=t.maxLength;break;case O.minLength:c.message=O.minLength,c.expected=t.minLength;break;case O.maxValue:c.message=O.maxValue,c.expected=s;break;case O.minValue:c.message=O.minValue,c.expected=i;break;case O.multipleOf:c.message=O.multipleOf,c.expected=o;break;case O.pattern:c.message=O.pattern,c.expected=t.pattern;break;case O.notInEnum:c.message=O.notInEnum,c.expected=t.enum}return c})(e.errors[r]?e.errors[r]:void 0,a,i,e,r,o,c,u),getObject:()=>a,getCurrentValue:()=>e.getValues()[r]}},E=e=>{const t=b(),r=g(t.schema,t.getValues()),n=S(e,r);return R(t,n,e)},P=e=>e?e.toString():"",w=e=>e.enum?e.enum.map(P):[],N=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),$=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,c=[];if("array"===n.type){if(n.items.enum)c=w(n.items);else if("string"===n.items.type)c=w(n);else if("number"===n.items.type||"integer"===n.items.type){const e=j(n);if(s=e[0],o=e[1],a=k(n),i=L(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)c.push(_(e,o||0))}n.uniqueItems&&(c=[...new Set(c)])}else"boolean"===n.type&&(c=["true"]);return Object.assign(Object.assign({},e),{type:x.checkbox,isSingle:"boolean"===n.type,getItemInputProps:a=>{const i={key:""};return i.name="array"===n.type?`${e.path}[${a}]`:e.path,i.ref=t(r),i.type="checkbox",i.id=N(e.path,a,c),i.value=c[a],i},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,c),r.htmlFor=N(e.path,t,c),r},getItems:()=>c})},F=e=>$(E(e)),M=(e,t)=>e+"-"+t+"-input",A=(e,t)=>{const{register:r}=e.formContext,{validator:n}=e,a=e.getObject();let i,s,o,c;const u={key:""};if("string"===a.type)u.pattern=a.pattern,u.minLength=a.minLength,u.maxLength=a.maxLength;else if("number"===a.type||"integer"===a.type){const e=j(a);o=e[0],c=e[1],i=k(a),s=L(a),u.min=`${i}`,u.max=`${s}`,u.step="any"===o?"any":_(o,c||0)}return Object.assign(Object.assign({},e),{type:x.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=M(e.path,t),r},getInputProps:()=>(u.name=e.path,u.ref=r(n),u.type=t,u.required=e.isRequired,u.id=M(e.path,t),u)})},C=()=>({}),J=e=>Object.assign(Object.assign({},A(e,"hidden")),{isRequired:!1,getLabelProps:C}),U=e=>J(E(e)),D=e=>{const t=e.getObject();let r="text";if("string"===t.type)switch(t.format){case"date-time":r="datetime-local";break;case"email":r="email";break;case"hostname":case"uri":r="url"}else"integer"!==t.type&&"number"!==t.type||(r="number");return A(e,r)},T=e=>D(E(e)),z=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),B=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,c=[];if("string"===n.type)c=w(n);else if("number"===n.type||"integer"===n.type){const e=j(n);if(s=e[0],o=e[1],a=k(n),i=L(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)c.push(_(e,o||0))}else"boolean"===n.type&&(c=["true","false"]);return Object.assign(Object.assign({},e),{type:x.radio,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=void 0!==n.title?n.title:e.path,t},getItemInputProps:n=>{const a={key:""};return a.name=e.path,a.ref=t(r),a.type="radio",a.required=e.isRequired,a.id=z(e.path,n,c),a.value=c[n],a},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,c),r.htmlFor=z(e.path,t,c),r},getItems:()=>c})},G=e=>B(E(e)),H=e=>e+"-select",K=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,c=[""];if("string"===n.type)c=c.concat(w(n));else if("number"===n.type||"integer"===n.type){const e=j(n);if(s=e[0],o=e[1],a=k(n),i=L(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)c.push(_(e,o||0))}else"boolean"===n.type&&(c=["true","false"]);return Object.assign(Object.assign({},e),{type:x.select,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=H(e.path),t},getSelectProps:()=>{const n={};return n.name=e.path,n.ref=t(r),n.required=e.isRequired,n.id=H(e.path),n},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,c),r.value=c[t],r},getItems:()=>c})},Q=e=>K(E(e)),W=e=>A(e,"password"),X=e=>W(E(e)),Y=e=>e+"-textarea-input",Z=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject(),a={};return"string"===n.type&&(a.minLength=n.minLength,a.maxLength=n.maxLength),Object.assign(Object.assign({},e),{type:x.textArea,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=Y(e.path),t},getTextAreaProps:()=>(a.name=e.path,a.ref=t(r),a.required=e.isRequired,a.id=Y(e.path),a)})},ee=e=>Z(E(e));function te(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(K(e)):r.push(D(e));break;case"integer":case"number":r.push(D(e));break;case"array":case"boolean":r.push($(e))}return r}function re(e,t,r,n,a){let s=[];const{JSONSchema:o}=t,c=R(e,t,r);if("object"===o.type){return Object.keys(o.properties).reduce(function(e,t,r,n){return(a,s)=>{const o=t&&t.properties?t.properties[s]:void 0,c=i(e,s),u=h(c,n,r),m=re(r,u,c,o,n);return a.concat(m)}}(r,n,e,a),[])}if(!n)return s.concat(te(c));switch(n.type){case v.default:s=s.concat(te(c));break;case v.checkbox:s.push($(c));break;case v.hidden:s.push(J(c));break;case v.input:s.push(D(c));break;case v.password:s.push(W(c));break;case v.radio:s.push(B(c));break;case v.select:s.push(K(c));break;case v.textArea:s.push(Z(c))}return s}const ne=e=>{const t=b(),r=g(t.schema,t.getValues());return re(t,S(e.path,r),e.path,e.UISchema,r)};export{O as ErrorTypes,y as FormContext,x as InputTypes,v as UITypes,i as concatFormPath,S as useAnnotatedSchemaFromPath,F as useCheckbox,U as useHidden,T as useInput,ne as useObject,V as useObjectFromForm,X as usePassword,G as useRadio,Q as useSelect,ee as useTextArea}; |
@@ -10,7 +10,7 @@ export declare type JSONSchemaType = ArrayJSONSchemaType | BasicJSONSchemaType | BooleanJSONSchemaType | NumberJSONSchemaType | ObjectJSONSchemaType | StringJSONSchemaType | NullJSONSchemaType; | ||
$ref?: string; | ||
anyOf?: Array<JSONSchemaType>; | ||
allOf?: Array<JSONSchemaType>; | ||
oneOf?: Array<JSONSchemaType>; | ||
not?: Array<JSONSchemaType>; | ||
enum?: Array<any>; | ||
anyOf?: JSONSchemaType[]; | ||
allOf?: JSONSchemaType[]; | ||
oneOf?: JSONSchemaType[]; | ||
not?: JSONSchemaType[]; | ||
enum?: JSONSchemaBaseInstanceTypes[]; | ||
const?: any; | ||
@@ -21,3 +21,3 @@ default?: any; | ||
} | ||
export declare type PropertyDependencies = Record<string, Array<string>>; | ||
export declare type PropertyDependencies = Record<string, string[]>; | ||
export declare type SchemaDependencies = JSONSchemaType; | ||
@@ -28,3 +28,3 @@ export interface ObjectJSONSchemaType extends BasicJSONSchemaType { | ||
additionalProperties?: boolean; | ||
required?: Array<string>; | ||
required?: string[]; | ||
propertyNames?: StringJSONSchemaType; | ||
@@ -55,3 +55,3 @@ minProperties?: number; | ||
type?: 'array'; | ||
items?: JSONSchemaType | Array<JSONSchemaType>; | ||
items?: JSONSchemaType | JSONSchemaType[]; | ||
additionalItems?: boolean | JSONSchemaType; | ||
@@ -69,2 +69,3 @@ contains?: JSONSchemaType; | ||
} | ||
export declare type JSONSchemaBaseInstanceTypes = boolean | string | number | null; | ||
export declare type JSONSchemaPathInfo = { | ||
@@ -71,0 +72,0 @@ JSONSchema: JSONSchemaType; |
{ | ||
"name": "react-hook-form-jsonschema", | ||
"version": "0.2.0-beta.8", | ||
"version": "0.2.0-beta.9", | ||
"description": "Wrapper arround react-hook-form to create forms from a JSON schema.", | ||
@@ -5,0 +5,0 @@ "main": "output/index.cjs.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73021
36
574