Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-hook-form-jsonschema

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-hook-form-jsonschema - npm Package Compare versions

Comparing version 0.2.0-beta.6 to 0.2.0-beta.7

6

CHANGELOG.md

@@ -10,2 +10,8 @@ # Changelog

## [0.2.0-beta.7] - 2020-02-14
### Changed
- **BREAKING**: `onSubmit` now passes an object with `data`, `event` and `methods` as members to the callback.
## [0.2.0-beta.6] - 2020-02-13

@@ -12,0 +18,0 @@

2

output/components/index.d.ts

@@ -1,2 +0,2 @@

export { FormContextProps, JSONFormContextValues } from './types';
export { FormContextProps, JSONFormContextValues, OnSubmitParameters, OnSubmitType, } from './types';
export { FormContext, useFormContext } from './FormContext';

@@ -10,3 +10,8 @@ import React from 'react';

}
export declare type OnSubmitType = (data: JSONSchemaType, event: React.BaseSyntheticEvent | undefined) => void | Promise<void>;
export declare type OnSubmitParameters = {
data: JSONSchemaType;
event: React.BaseSyntheticEvent | undefined;
methods: JSONFormContextValues;
};
export declare type OnSubmitType = (props: OnSubmitParameters) => void | Promise<void>;
export declare type FormContextProps = {

@@ -13,0 +18,0 @@ validationMode?: Mode;

@@ -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.pattern}:t.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:exports.ErrorTypes.pattern},(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",R=(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)})},V=()=>({}),F=e=>Object.assign(Object.assign({},R(e,"hidden")),{isRequired:!1,getLabelProps:V}),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 R(e,t)},N=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),$=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=N(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=N(e.path,t,p),r},getItems:()=>p})},M=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=M(e.path),t},getSelectProps:()=>{const s={};return s.name=e.path,s.ref=t(r),s.required=e.isRequired,s.id=M(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=>R(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($(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={};c.onSubmit=u.handleSubmit((t,r)=>{e.onSubmit&&e.onSubmit(d(e.schema,t),r)}),e.noNativeValidate&&(c.noValidate=e.noNativeValidate);const l=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]),h=r.useMemo(()=>m(e.schema,l,[]),[e.schema,l]);return s.createElement(g.Provider,{value:Object.assign(Object.assign({},u),{schema:h,idMap:l,customValidators:e.customValidators})},s.createElement("form",Object.assign({},c),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=>$(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},{}),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.pattern}:t.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:exports.ErrorTypes.pattern},(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));
export { Controller } from 'react-hook-form';
export * from 'react-hook-form/dist/types';
export { FormContext, FormContextProps } from './components';
export { FormContext, FormContextProps, JSONFormContextValues, } from './components';
export * from './hooks';
export * from './JSONSchema';
export { ErrorMessage, ErrorMessageValues, ErrorTypes, } from './hooks/validators/types';

@@ -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={};m.onSubmit=u.handleSubmit((e,t)=>{r.onSubmit&&r.onSubmit(h(r.schema,e),t)}),r.noNativeValidate&&(m.noValidate=r.noNativeValidate);const l=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]),d=n(()=>p(r.schema,l,[]),[r.schema,l]);return t.createElement(f.Provider,{value:Object.assign(Object.assign({},u),{schema:d,idMap:l,customValidators:r.customValidators})},t.createElement("form",Object.assign({},m),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.pattern}:t.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:O.pattern},(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),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.pattern}:t.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:O.pattern},(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};
{
"name": "react-hook-form-jsonschema",
"version": "0.2.0-beta.6",
"version": "0.2.0-beta.7",
"description": "Wrapper arround react-hook-form to create forms from a JSON schema.",

@@ -5,0 +5,0 @@ "main": "output/index.cjs.js",

@@ -115,3 +115,6 @@ # react-hook-form-jsonschema

- `submitFocusError`: Boolean, when `true` focus on the first field with error after submit validates, if there is any. Defaults to `true`.
- `onSubmit`: Callback function that the form values are passed to when submit is triggered without errors.
- `onSubmit`: If provided `react-hook-form-jsonschema` will call this function as the submit action, it passes an object with the following members:
- `data`: The data that was provided as inputs to the form, correctly formatted as an instance of the JSON Schema provided.
- `event`: A react event
- `methods`: Provides access to the methods of [`react-hook-form`](https://react-hook-form.com/api) `useForm`, from this you can extract, for example, the `triggerValidation` method to revalidate the form if an error occured while submitting.
- `noNativeValidate`: Boolean, when `true` disables the default browser validation (notice that `react-hook-form-jsonschema` does NOT yet implement validation for URIs and email addresses).

@@ -118,0 +121,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc