react-hook-form-jsonschema
Advanced tools
Comparing version 0.2.0-beta.4 to 0.2.0-beta.5
@@ -10,2 +10,12 @@ # Changelog | ||
## [0.2.0-beta.5] - 2020-02-11 | ||
### Added | ||
- `customValidators` to allow the user to define their own validation functions | ||
### Changed | ||
- **BREAKING**: Renamed `FormValuesWithSchema` type to `JSONFormContextValues` | ||
## [0.2.0-beta.4] - 2020-02-05 | ||
@@ -12,0 +22,0 @@ |
import React, { FC } from 'react'; | ||
import { FieldValues } from 'react-hook-form'; | ||
import { FormContextProps, FormValuesWithSchema } from './types'; | ||
export declare const InternalFormContext: React.Context<FormValuesWithSchema<Record<string, any>> | null>; | ||
export declare function useFormContext<T extends FieldValues>(): FormValuesWithSchema<T>; | ||
import { FormContextProps, JSONFormContextValues } from './types'; | ||
export declare const InternalFormContext: React.Context<JSONFormContextValues<Record<string, any>> | null>; | ||
export declare function useFormContext<T extends FieldValues = FieldValues>(): JSONFormContextValues<T>; | ||
export declare const FormContext: FC<FormContextProps>; |
@@ -1,2 +0,2 @@ | ||
export { FormContextProps } from './types'; | ||
export { FormContextProps, JSONFormContextValues } from './types'; | ||
export { FormContext, useFormContext } from './FormContext'; |
import React from 'react'; | ||
import { FormContextValues, Mode } from 'react-hook-form'; | ||
import { FormContextValues, Mode, FieldValues } from 'react-hook-form'; | ||
import { JSONSchemaType } from '../../JSONSchema'; | ||
export interface FormValuesWithSchema<T> extends FormContextValues<T> { | ||
import { CustomValidators } from '../../hooks/validators'; | ||
export interface JSONFormContextValues<FormValues extends FieldValues = FieldValues> extends FormContextValues<FormValues> { | ||
schema: JSONSchemaType; | ||
customValidators?: CustomValidators; | ||
} | ||
@@ -14,3 +16,4 @@ export declare type OnSubmitType = (data: JSONSchemaType, event: React.BaseSyntheticEvent | undefined) => void | Promise<void>; | ||
noNativeValidate?: boolean; | ||
customValidators?: CustomValidators; | ||
schema: JSONSchemaType; | ||
}; |
export { useCheckbox } from './useCheckbox'; | ||
export { useGenericInput } from './useGenericInput'; | ||
export { useHidden } from './useHidden'; | ||
@@ -4,0 +3,0 @@ export { useInput } from './useInput'; |
import React from 'react'; | ||
import { FormContextValues, ValidationOptions } from 'react-hook-form'; | ||
import { ValidationOptions, FieldValues } from 'react-hook-form'; | ||
import { ErrorMessage } from '../validators'; | ||
import { JSONSchemaType } from '../../JSONSchema'; | ||
import { JSONFormContextValues } from '../../components'; | ||
export declare enum InputTypes { | ||
@@ -26,3 +27,4 @@ generic = "generic", | ||
getObject(): JSONSchemaType; | ||
formContext: FormContextValues; | ||
getCurrentValue(): FieldValues; | ||
formContext: JSONFormContextValues; | ||
isRequired: boolean; | ||
@@ -29,0 +31,0 @@ name: string; |
import { UseCheckboxParameters, BasicInputReturnType, UseCheckboxReturnType } from './types'; | ||
export declare const getCheckboxCustomFields: (baseObject: BasicInputReturnType) => UseCheckboxReturnType; | ||
export declare const getCheckboxCustomFields: (baseInput: BasicInputReturnType) => UseCheckboxReturnType; | ||
export declare const useCheckbox: UseCheckboxParameters; |
@@ -1,5 +0,5 @@ | ||
import { FormContextValues } from 'react-hook-form'; | ||
import { GenericInputParameters, BasicInputReturnType } from './types'; | ||
import { JSONFormContextValues } from '../components'; | ||
import { JSONSchemaPathInfo } from '../JSONSchema'; | ||
export declare const getGenericInput: (formContext: FormContextValues<Record<string, any>>, pathInfo: JSONSchemaPathInfo, path: string) => BasicInputReturnType; | ||
export declare const getGenericInput: (formContext: JSONFormContextValues<Record<string, any>>, pathInfo: JSONSchemaPathInfo, path: string) => BasicInputReturnType; | ||
export declare const useGenericInput: GenericInputParameters; |
import { UseRadioParameters, BasicInputReturnType, UseRadioReturnType } from './types'; | ||
export declare const getRadioCustomFields: (baseObject: BasicInputReturnType) => UseRadioReturnType; | ||
export declare const getRadioCustomFields: (baseInput: BasicInputReturnType) => UseRadioReturnType; | ||
export declare const useRadio: UseRadioParameters; |
import { UseRawInputParameters, BasicInputReturnType, UseRawInputReturnType } from './types'; | ||
export declare const getRawInputCustomFields: (baseFields: BasicInputReturnType, inputType: string) => UseRawInputReturnType; | ||
export declare const getRawInputCustomFields: (baseInput: BasicInputReturnType, inputType: string) => UseRawInputReturnType; | ||
export declare const useRawInput: UseRawInputParameters; |
import { UseSelectParameters, BasicInputReturnType, UseSelectReturnType } from './types'; | ||
export declare const getSelectCustomFields: (baseObject: BasicInputReturnType) => UseSelectReturnType; | ||
export declare const getSelectCustomFields: (baseInput: BasicInputReturnType) => UseSelectReturnType; | ||
export declare const useSelect: UseSelectParameters; |
import { UseTextAreaParameters, BasicInputReturnType, UseTextAreaReturnType } from './types'; | ||
export declare const getTextAreaCustomFields: (baseObject: BasicInputReturnType) => UseTextAreaReturnType; | ||
export declare const getTextAreaCustomFields: (baseInput: BasicInputReturnType) => UseTextAreaReturnType; | ||
export declare const useTextArea: UseTextAreaParameters; |
import { JSONSchemaType } from '../../JSONSchema'; | ||
export declare const toFixed: (value: number, precision: number) => string; | ||
export declare const getNumberStep: (currentObject: JSONSchemaType) => [number | "any", number | undefined]; | ||
export declare const getNumberMinimum: (currentObject: JSONSchemaType) => number | undefined; | ||
export declare const getNumberMaximum: (currentObject: JSONSchemaType) => number | undefined; | ||
export declare const getNumberValidator: (currentObject: JSONSchemaType, required: boolean) => Partial<{ | ||
export declare const getNumberValidator: (currentObject: JSONSchemaType, baseValidator: Partial<{ | ||
required: string | boolean | { | ||
@@ -32,2 +28,28 @@ value: boolean; | ||
validate: import("react-hook-form").Validate | Record<string, import("react-hook-form").Validate>; | ||
}>) => Partial<{ | ||
required: string | boolean | { | ||
value: boolean; | ||
message: string; | ||
}; | ||
min: string | number | { | ||
value: string | number; | ||
message: string; | ||
}; | ||
max: string | number | { | ||
value: string | number; | ||
message: string; | ||
}; | ||
maxLength: string | number | { | ||
value: string | number; | ||
message: string; | ||
}; | ||
minLength: string | number | { | ||
value: string | number; | ||
message: string; | ||
}; | ||
pattern: RegExp | { | ||
value: RegExp; | ||
message: string; | ||
}; | ||
validate: import("react-hook-form").Validate | Record<string, import("react-hook-form").Validate>; | ||
}>; |
import { JSONSchemaType } from '../../JSONSchema'; | ||
export declare const getStringValidator: (currentObject: JSONSchemaType, required: boolean) => Partial<{ | ||
export declare const getStringValidator: (currentObject: JSONSchemaType, baseValidator: Partial<{ | ||
required: string | boolean | { | ||
@@ -28,2 +28,28 @@ value: boolean; | ||
validate: import("react-hook-form").Validate | Record<string, import("react-hook-form").Validate>; | ||
}>) => Partial<{ | ||
required: string | boolean | { | ||
value: boolean; | ||
message: string; | ||
}; | ||
min: string | number | { | ||
value: string | number; | ||
message: string; | ||
}; | ||
max: string | number | { | ||
value: string | number; | ||
message: string; | ||
}; | ||
maxLength: string | number | { | ||
value: string | number; | ||
message: string; | ||
}; | ||
minLength: string | number | { | ||
value: string | number; | ||
message: string; | ||
}; | ||
pattern: RegExp | { | ||
value: RegExp; | ||
message: string; | ||
}; | ||
validate: import("react-hook-form").Validate | Record<string, import("react-hook-form").Validate>; | ||
}>; |
@@ -1,5 +0,5 @@ | ||
export * from './getBooleanValidator'; | ||
export * from './getNumberValidator'; | ||
export * from './numberUtilities'; | ||
export * from './getStringValidator'; | ||
export * from './getGenericValidator'; | ||
export * from './getError'; | ||
export * from './types'; |
@@ -0,1 +1,2 @@ | ||
import { JSONSchemaType } from '../../../JSONSchema'; | ||
export declare enum ErrorTypes { | ||
@@ -8,3 +9,6 @@ required = "__form_error_required__", | ||
pattern = "__form_error_pattern__", | ||
notInteger = "__form_error_notInteger__", | ||
notFloat = "__form_error_notFloat__", | ||
multipleOf = "__form_error_multipleOf__", | ||
notInEnum = "__form_error_notInEnum", | ||
undefinedError = "__form_error_undefinedError__" | ||
@@ -14,4 +18,11 @@ } | ||
export declare type ErrorMessage = { | ||
message: ErrorTypes; | ||
message: ErrorTypes | string; | ||
expected: ErrorMessageValues; | ||
} | undefined; | ||
export interface CustomValidatorContext { | ||
currentObject: JSONSchemaType; | ||
path: string; | ||
} | ||
export declare type CustomValidatorReturnValue = string | true; | ||
export declare type CustomValidator = (value: string, context: CustomValidatorContext) => CustomValidatorReturnValue; | ||
export declare type CustomValidators = Record<string, CustomValidator>; |
@@ -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 o=e=>{const t=e.split("/");return"#"===t[0]&&t.shift(),""===t[t.length-1]&&t.pop(),t},n=(e,t)=>{const r=Object.keys(t).sort(),s={};for(const n of r){const r=o(n);if(!r&&t[n])continue;let a=s,i=e;for(let e=0;e<r.length&&void 0!==i;e++){if(i.type&&"object"===i.type&&(i=i.properties,i=i[r[e]]),e===r.length-1){if(!i)break;"integer"===i.type?a[r[e]]=parseInt(t[n]):"number"===i.type?a[r[e]]=parseFloat(t[n]):"boolean"===i.type?a[r[e]]="true"===t[n]:a[r[e]]=t[n]}else void 0===a[r[e]]&&(a[r[e]]={});a=a[r[e]]}}return s},a=r.createContext(null);function i(){return r.useContext(a)}var p,u,m;(p=exports.InputTypes||(exports.InputTypes={})).generic="generic",p.radio="radio",p.select="select",p.input="input",p.textArea="textArea",p.checkbox="checkbox",(u=exports.UITypes||(exports.UITypes={})).default="default",u.radio="radio",u.select="select",u.input="input",u.hidden="hidden",u.password="password",u.textArea="textArea",u.checkbox="checkbox",(m=exports.ErrorTypes||(exports.ErrorTypes={})).required="__form_error_required__",m.maxLength="__form_error_maxLenght__",m.minLength="__form_error_minLenght__",m.maxValue="__form_error_maxValue__",m.minValue="__form_error_minValue__",m.pattern="__form_error_pattern__",m.multipleOf="__form_error_multipleOf__",m.undefinedError="__form_error_undefinedError__";const c=e=>{const t={};return e&&(t.required=exports.ErrorTypes.required),t},l=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},d=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]},x=e=>{const[t]=d(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},g=e=>{const[t]=d(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},h=(e,t)=>{const r=x(e),s=g(e),o={validate:{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||exports.ErrorTypes.multipleOf)}};return t&&(o.required=exports.ErrorTypes.required),"integer"===e.type?o.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:exports.ErrorTypes.pattern}:o.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:exports.ErrorTypes.pattern},(r||0===r)&&(o.min={value:r,message:exports.ErrorTypes.minValue}),(s||0===s)&&(o.max={value:s,message:exports.ErrorTypes.maxValue}),o},y=(e,t)=>{const r={};return t&&(r.required=exports.ErrorTypes.required),e.minLength&&(r.minLength={value:e.minLength,message:exports.ErrorTypes.minLength}),e.maxLength&&(r.maxLength={value:e.maxLength,message:exports.ErrorTypes.maxLength}),e.pattern&&(r.pattern={value:new RegExp(e.pattern),message:exports.ErrorTypes.pattern}),r},b=(e,t)=>e+"/"+t,f=e=>{const t=o(e);let r=i().schema,s=!1,n="";for(let e=0;e<t.length;e++){if(!r||!r.type||"object"!==r.type){r={};break}s=!!(r.required&&r.required.indexOf(t[e])>-1),n=t[e],r=r.properties[t[e]]}return{JSONSchema:r,isRequired:s,objectName:n}},v=(e,t,r)=>{const{JSONSchema:s,isRequired:o,objectName:n}=t;let a,i,p;if("number"===s.type||"integer"===s.type){const e=d(s);p=e[0],a=x(s),i=g(s)}return{name:n,path:r,isRequired:o,formContext:e,type:exports.InputTypes.generic,validator:{},getError:()=>((e,t,r,s,o,n,a,i)=>{if("array"===t.type){const e=s.getValues({nest:!0})[o];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: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=n;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}return p})(e.errors[r]?e.errors[r]:void 0,s,o,e,r,a,i,p),getObject:()=>s}},O=e=>{const t=i(),r=f(e);return v(t,r,e)},T=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),_=e=>{const t=e.getObject();let r,s,o,n,a={},i=[];if("array"===t.type){if(t.items.enum)i=t.items.enum;else if("string"===t.items.type)i=t.enum?t.enum:[],a=y(t,e.isRequired);else if("number"===t.items.type||"integer"===t.items.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}t.uniqueItems&&(i=[...new Set(i)])}else"boolean"===t.type&&(a=c(e.isRequired),i=["true"]);return Object.assign(Object.assign({},e),{validator:a,type:exports.InputTypes.checkbox,isSingle:"boolean"===t.type,getItemInputProps:r=>{const s={key:""};return s.name="array"===t.type?`${e.path}[${r}]`:e.path,s.ref=e.formContext.register(a),s.type="checkbox",s.id=T(e.path,r,i),s.value=i[r],s},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,i),r.htmlFor=T(e.path,t,i),r},getItems:()=>i})},I=(e,t)=>e+"-"+t+"-input",q=(e,t)=>{const{register:r}=e.formContext,s=e.getObject();let o,n,a,i,p={};const u={key:""};if("string"===s.type)p=y(s,e.isRequired),u.pattern=s.pattern,u.minLength=s.minLength,u.maxLength=s.maxLength;else if("number"===s.type||"integer"===s.type){const t=d(s);a=t[0],i=t[1],o=x(s),n=g(s),p=h(s,e.isRequired),u.min=`${o}`,u.max=`${n}`,u.step="any"===a?"any":l(a,i||0)}return Object.assign(Object.assign({},e),{validator:p,type:exports.InputTypes.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=I(e.path,t),r},getInputProps:()=>(u.name=e.path,u.ref=r(p),u.type=t,u.required=e.isRequired,u.id=I(e.path,t),u)})},E=()=>({}),j=e=>Object.assign(Object.assign({},q(e,"hidden")),{isRequired:!1,getLabelProps:E}),k=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 q(e,t)},L=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),R=e=>{const t=e.getObject();let r,s,o,n,a={},i=[];if("string"===t.type)i=t.enum?t.enum:[],a=y(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}else"boolean"===t.type&&(a=c(e.isRequired),i=["true","false"]);return Object.assign(Object.assign({},e),{validator:a,type:exports.InputTypes.radio,getLabelProps:()=>{const r={};return r.id=e.path+"-label",r.htmlFor=void 0!==t.title?t.title:e.path,r},getItemInputProps:t=>{const r={key:""};return r.name=e.path,r.ref=e.formContext.register(a),r.type="radio",r.required=e.isRequired,r.id=L(e.path,t,i),r.value=i[t],r},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,i),r.htmlFor=L(e.path,t,i),r},getItems:()=>i})},S=e=>e+"-select",P=e=>{const t=e.getObject();let r,s,o,n,a={},i=[""];if("string"===t.type)i=i.concat(t.enum?t.enum:[]),a=y(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}else"boolean"===t.type&&(a=c(e.isRequired),i=["true","false"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.select,validator:a,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=S(e.path),t},getSelectProps:()=>{const t={};return t.name=e.path,t.ref=e.formContext.register(a),t.required=e.isRequired,t.id=S(e.path),t},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,i),r.value=i[t],r},getItems:()=>i})},F=e=>q(e,"password"),V=e=>e+"-textarea-input",C=e=>{const t=e.getObject();let r={};const s={};return"string"===t.type?(r=y(t,e.isRequired),s.minLength=t.minLength,s.maxLength=t.maxLength):"number"!==t.type&&"integer"!==t.type||(r=h(t,e.isRequired)),Object.assign(Object.assign({},e),{type:exports.InputTypes.textArea,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=V(e.path),t},getTextAreaProps:()=>(s.name=e.path,s.ref=e.formContext.register(r),s.required=e.isRequired,s.id=V(e.path),s)})};function w(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(P(e)):r.push(k(e));break;case"integer":case"number":r.push(k(e));break;case"array":case"boolean":r.push(_(e))}return r}function M(e,t,r,s){let o=[];const{JSONSchema:n}=t,a=v(e,t,r);if("object"===n.type){return Object.keys(n.properties).reduce(function(e,t,r,s){const o=t.required;return(n,a)=>{const i=!!o&&-1!==o.indexOf(a),p=r&&r.properties?r.properties[a]:void 0,u=b(e,a),m={JSONSchema:t.properties[a],isRequired:i,objectName:a},c=M(s,m,u,p);return n.concat(c)}}(r,n,s,e),[])}if(!s)return o.concat(w(a));switch(s.type){case exports.UITypes.default:o=o.concat(w(a));break;case exports.UITypes.checkbox:o.push(_(a));break;case exports.UITypes.hidden:o.push(j(a));break;case exports.UITypes.input:o.push(k(a));break;case exports.UITypes.password:o.push(F(a));break;case exports.UITypes.radio:o.push(R(a));break;case exports.UITypes.select:o.push(P(a));break;case exports.UITypes.textArea:o.push(C(a))}return o}Object.defineProperty(exports,"Controller",{enumerable:!0,get:function(){return t.Controller}}),exports.FormContext=e=>{const{validationMode:r="onSubmit",revalidateMode:o="onChange",submitFocusError:i=!0}=e,p=t.useForm({mode:r,reValidateMode:o,submitFocusError:i}),u={};return u.onSubmit=p.handleSubmit((t,r)=>{e.onSubmit&&e.onSubmit(n(e.schema,t),r)}),e.noNativeValidate&&(u.noValidate=e.noNativeValidate),s.createElement(a.Provider,{value:Object.assign(Object.assign({},p),{schema:e.schema})},s.createElement("form",Object.assign({},u),e.children))},exports.concatFormPath=b,exports.useCheckbox=e=>_(O(e)),exports.useGenericInput=O,exports.useHidden=e=>j(O(e)),exports.useInput=e=>k(O(e)),exports.useObject=e=>M(i(),f(e.path),e.path,e.UISchema),exports.useObjectFromForm=e=>n(i().schema,e),exports.useObjectFromPath=f,exports.usePassword=e=>F(O(e)),exports.useRadio=e=>R(O(e)),exports.useSelect=e=>P(O(e)),exports.useTextArea=e=>C(O(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=>{const t=e.split("/");return"#"===t[0]&&t.shift(),""===t[t.length-1]&&t.pop(),t},o=(e,t)=>{const r=Object.keys(t).sort(),s={};for(const o of r){const r=n(o);if(!r&&t[o])continue;let a=s,i=e;for(let e=0;e<r.length&&void 0!==i;e++){if(i.type&&"object"===i.type&&(i=i.properties,i=i[r[e]]),e===r.length-1){if(!i)break;"integer"===i.type?a[r[e]]=parseInt(t[o]):"number"===i.type?a[r[e]]=parseFloat(t[o]):"boolean"===i.type?a[r[e]]="true"===t[o]:a[r[e]]=t[o]}else void 0===a[r[e]]&&(a[r[e]]={});a=a[r[e]]}}return s},a=r.createContext(null);function i(){return r.useContext(a)}var p,u,m;(p=exports.InputTypes||(exports.InputTypes={})).generic="generic",p.radio="radio",p.select="select",p.input="input",p.textArea="textArea",p.checkbox="checkbox",(u=exports.UITypes||(exports.UITypes={})).default="default",u.radio="radio",u.select="select",u.input="input",u.hidden="hidden",u.password="password",u.textArea="textArea",u.checkbox="checkbox",(m=exports.ErrorTypes||(exports.ErrorTypes={})).required="__form_error_required__",m.maxLength="__form_error_maxLenght__",m.minLength="__form_error_minLenght__",m.maxValue="__form_error_maxValue__",m.minValue="__form_error_minValue__",m.pattern="__form_error_pattern__",m.notInteger="__form_error_notInteger__",m.notFloat="__form_error_notFloat__",m.multipleOf="__form_error_multipleOf__",m.notInEnum="__form_error_notInEnum",m.undefinedError="__form_error_undefinedError__";const c=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},l=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]},d=e=>{const[t]=l(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},g=e=>{const[t]=l(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 x(e,t){return Object.keys(e).reduce((r,s)=>(r[s]=r=>e[s](r,t),r),{})}const h=(e,t,r)=>{const{currentObject:s}=r,n=Object.keys(t).length>0||s.enum,o=Object.assign({},n?{validate:Object.assign(Object.assign({},x(t,r)),s.enum?{enumValidator:e=>{if(s.enum)for(const t of s.enum)if(t==e)return!0;return!1}}:void 0)}:void 0);switch(e&&(o.required=exports.ErrorTypes.required),s.type){case"integer":case"number":return((e,t)=>{const r=d(e),s=g(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})(s,o);case"string":return((e,t)=>(e.minLength&&(t.minLength={value:e.minLength,message:exports.ErrorTypes.minLength}),e.maxLength&&(t.maxLength={value:e.maxLength,message:exports.ErrorTypes.maxLength}),e.pattern&&(t.pattern={value:new RegExp(e.pattern),message:exports.ErrorTypes.pattern}),t))(s,o);case"boolean":return o;default:return{}}},y=(e,t)=>e+"/"+t,b=e=>{const t=n(e);let r=i().schema,s=!1,o="";for(let e=0;e<t.length;e++){if(!r||!r.type||"object"!==r.type){r={};break}s=!!(r.required&&r.required.indexOf(t[e])>-1),o=t[e],r=r.properties[t[e]]}return{JSONSchema:r,isRequired:s,objectName:o}},f=(e,t,r)=>{var s;const{JSONSchema:n,isRequired:o,objectName:a}=t;let i,p,u;if("number"===n.type||"integer"===n.type){const e=l(n);u=e[0],i=d(n),p=g(n)}return{name:a,path:r,isRequired:o,formContext:e,type:exports.InputTypes.generic,validator:h(o,(s=e.customValidators,null!=s?s:{}),{currentObject:n,path:r}),getError:()=>((e,t,r,s,n,o,a,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}return p})(e.errors[r]?e.errors[r]:void 0,n,o,e,r,i,p,u),getObject:()=>n,getCurrentValue:()=>e.getValues()[r]}},_=e=>{const t=i(),r=b(e);return f(t,r,e)},v=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),O=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,o,a,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=l(s);if(a=e[0],i=e[1],n=d(s),o=g(s),void 0!==n&&void 0!==o&&"any"!=a)for(let e=n;e<=o;e+=a)p.push(c(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 o={key:""};return o.name="array"===s.type?`${e.path}[${n}]`:e.path,o.ref=t(r),o.type="checkbox",o.id=v(e.path,n,p),o.value=p[n],o},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,p),r.htmlFor=v(e.path,t,p),r},getItems:()=>p})},T=(e,t)=>e+"-"+t+"-input",j=(e,t)=>{const{register:r}=e.formContext,{validator:s}=e,n=e.getObject();let o,a,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=l(n);i=e[0],p=e[1],o=d(n),a=g(n),u.min=`${o}`,u.max=`${a}`,u.step="any"===i?"any":c(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=T(e.path,t),r},getInputProps:()=>(u.name=e.path,u.ref=r(s),u.type=t,u.required=e.isRequired,u.id=T(e.path,t),u)})},I=()=>({}),E=e=>Object.assign(Object.assign({},j(e,"hidden")),{isRequired:!1,getLabelProps:I}),k=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 j(e,t)},L=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),q=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,o,a,i,p=[];if("string"===s.type)p=s.enum?s.enum:[];else if("number"===s.type||"integer"===s.type){const e=l(s);if(a=e[0],i=e[1],n=d(s),o=g(s),void 0!==n&&void 0!==o&&"any"!=a)for(let e=n;e<=o;e+=a)p.push(c(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=L(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=L(e.path,t,p),r},getItems:()=>p})},V=e=>e+"-select",F=e=>{const{register:t}=e.formContext,{validator:r}=e,s=e.getObject();let n,o,a,i,p=[""];if("string"===s.type)p=p.concat(s.enum?s.enum:[]);else if("number"===s.type||"integer"===s.type){const e=l(s);if(a=e[0],i=e[1],n=d(s),o=g(s),void 0!==n&&void 0!==o&&"any"!=a)for(let e=n;e<=o;e+=a)p.push(c(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=V(e.path),t},getSelectProps:()=>{const s={};return s.name=e.path,s.ref=t(r),s.required=e.isRequired,s.id=V(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})},S=e=>j(e,"password"),P=e=>e+"-textarea-input",C=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=P(e.path),t},getTextAreaProps:()=>(n.name=e.path,n.ref=t(r),n.required=e.isRequired,n.id=P(e.path),n)})};function w(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(F(e)):r.push(k(e));break;case"integer":case"number":r.push(k(e));break;case"array":case"boolean":r.push(O(e))}return r}function M(e,t,r,s){let n=[];const{JSONSchema:o}=t,a=f(e,t,r);if("object"===o.type){return Object.keys(o.properties).reduce(function(e,t,r,s){const n=t.required;return(o,a)=>{const i=!!n&&-1!==n.indexOf(a),p=r&&r.properties?r.properties[a]:void 0,u=y(e,a),m={JSONSchema:t.properties[a],isRequired:i,objectName:a},c=M(s,m,u,p);return o.concat(c)}}(r,o,s,e),[])}if(!s)return n.concat(w(a));switch(s.type){case exports.UITypes.default:n=n.concat(w(a));break;case exports.UITypes.checkbox:n.push(O(a));break;case exports.UITypes.hidden:n.push(E(a));break;case exports.UITypes.input:n.push(k(a));break;case exports.UITypes.password:n.push(S(a));break;case exports.UITypes.radio:n.push(q(a));break;case exports.UITypes.select:n.push(F(a));break;case exports.UITypes.textArea:n.push(C(a))}return n}Object.defineProperty(exports,"Controller",{enumerable:!0,get:function(){return t.Controller}}),exports.FormContext=e=>{const{validationMode:r="onSubmit",revalidateMode:n="onChange",submitFocusError:i=!0}=e,p=t.useForm({mode:r,reValidateMode:n,submitFocusError:i}),u={};return u.onSubmit=p.handleSubmit((t,r)=>{e.onSubmit&&e.onSubmit(o(e.schema,t),r)}),e.noNativeValidate&&(u.noValidate=e.noNativeValidate),s.createElement(a.Provider,{value:Object.assign(Object.assign({},p),{schema:e.schema,customValidators:e.customValidators})},s.createElement("form",Object.assign({},u),e.children))},exports.concatFormPath=y,exports.useCheckbox=e=>O(_(e)),exports.useHidden=e=>E(_(e)),exports.useInput=e=>k(_(e)),exports.useObject=e=>M(i(),b(e.path),e.path,e.UISchema),exports.useObjectFromForm=e=>o(i().schema,e),exports.useObjectFromPath=b,exports.usePassword=e=>S(_(e)),exports.useRadio=e=>q(_(e)),exports.useSelect=e=>F(_(e)),exports.useTextArea=e=>C(_(e)); |
@@ -1,1 +0,1 @@ | ||
import{useForm as e}from"react-hook-form";export{Controller}from"react-hook-form";import t,{createContext as r,useContext as n}from"react";const i=e=>{const t=e.split("/");return"#"===t[0]&&t.shift(),""===t[t.length-1]&&t.pop(),t},a=(e,t)=>{const r=Object.keys(t).sort(),n={};for(const a of r){const r=i(a);if(!r&&t[a])continue;let s=n,o=e;for(let e=0;e<r.length&&void 0!==o;e++){if(o.type&&"object"===o.type&&(o=o.properties,o=o[r[e]]),e===r.length-1){if(!o)break;"integer"===o.type?s[r[e]]=parseInt(t[a]):"number"===o.type?s[r[e]]=parseFloat(t[a]):"boolean"===o.type?s[r[e]]="true"===t[a]:s[r[e]]=t[a]}else void 0===s[r[e]]&&(s[r[e]]={});s=s[r[e]]}}return n},s=r(null);function o(){return n(s)}const u=r=>{const{validationMode:n="onSubmit",revalidateMode:i="onChange",submitFocusError:o=!0}=r,u=e({mode:n,reValidateMode:i,submitFocusError:o}),m={};return m.onSubmit=u.handleSubmit((e,t)=>{r.onSubmit&&r.onSubmit(a(r.schema,e),t)}),r.noNativeValidate&&(m.noValidate=r.noNativeValidate),t.createElement(s.Provider,{value:Object.assign(Object.assign({},u),{schema:r.schema})},t.createElement("form",Object.assign({},m),r.children))};var m,c,p;!function(e){e.generic="generic",e.radio="radio",e.select="select",e.input="input",e.textArea="textArea",e.checkbox="checkbox"}(m||(m={})),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"}(c||(c={})),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.multipleOf="__form_error_multipleOf__",e.undefinedError="__form_error_undefinedError__"}(p||(p={}));const l=e=>{const t={};return e&&(t.required=p.required),t},d=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},g=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]},h=e=>{const[t]=g(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},b=e=>{const[t]=g(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},f=(e,t)=>{const r=h(e),n=b(e),i={validate:{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||p.multipleOf)}};return t&&(i.required=p.required),"integer"===e.type?i.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:p.pattern}:i.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:p.pattern},(r||0===r)&&(i.min={value:r,message:p.minValue}),(n||0===n)&&(i.max={value:n,message:p.maxValue}),i},x=(e,t)=>{const r={};return t&&(r.required=p.required),e.minLength&&(r.minLength={value:e.minLength,message:p.minLength}),e.maxLength&&(r.maxLength={value:e.maxLength,message:p.maxLength}),e.pattern&&(r.pattern={value:new RegExp(e.pattern),message:p.pattern}),r},y=(e,t)=>e+"/"+t,v=e=>{const t=i(e);let r=o().schema,n=!1,a="";for(let e=0;e<t.length;e++){if(!r||!r.type||"object"!==r.type){r={};break}n=!!(r.required&&r.required.indexOf(t[e])>-1),a=t[e],r=r.properties[t[e]]}return{JSONSchema:r,isRequired:n,objectName:a}},_=e=>a(o().schema,e),O=(e,t,r)=>{const{JSONSchema:n,isRequired:i,objectName:a}=t;let s,o,u;if("number"===n.type||"integer"===n.type){const e=g(n);u=e[0],s=h(n),o=b(n)}return{name:a,path:r,isRequired:i,formContext:e,type:m.generic,validator:{},getError:()=>((e,t,r,n,i,a,s,o)=>{if("array"===t.type){const e=n.getValues({nest:!0})[i];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:p.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:p.maxLength,expected:t.maxItems}}}if(!e)return;const u={message:p.undefinedError,expected:void 0};switch(e.message){case p.required:u.message=p.required,u.expected=r;break;case p.maxLength:u.message=p.maxLength,u.expected=t.maxLength;break;case p.minLength:u.message=p.minLength,u.expected=t.minLength;break;case p.maxValue:u.message=p.maxValue,u.expected=a;break;case p.minValue:u.message=p.minValue,u.expected=s;break;case p.multipleOf:u.message=p.multipleOf,u.expected=o;break;case p.pattern:u.message=p.pattern,u.expected=t.pattern}return u})(e.errors[r]?e.errors[r]:void 0,n,i,e,r,s,o,u),getObject:()=>n}},q=e=>{const t=o(),r=v(e);return O(t,r,e)},k=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),L=e=>{const t=e.getObject();let r,n,i,a,s={},o=[];if("array"===t.type){if(t.items.enum)o=t.items.enum;else if("string"===t.items.type)o=t.enum?t.enum:[],s=x(t,e.isRequired);else if("number"===t.items.type||"integer"===t.items.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}t.uniqueItems&&(o=[...new Set(o)])}else"boolean"===t.type&&(s=l(e.isRequired),o=["true"]);return Object.assign(Object.assign({},e),{validator:s,type:m.checkbox,isSingle:"boolean"===t.type,getItemInputProps:r=>{const n={key:""};return n.name="array"===t.type?`${e.path}[${r}]`:e.path,n.ref=e.formContext.register(s),n.type="checkbox",n.id=k(e.path,r,o),n.value=o[r],n},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,o),r.htmlFor=k(e.path,t,o),r},getItems:()=>o})},j=e=>L(q(e)),I=(e,t)=>e+"-"+t+"-input",R=(e,t)=>{const{register:r}=e.formContext,n=e.getObject();let i,a,s,o,u={};const c={key:""};if("string"===n.type)u=x(n,e.isRequired),c.pattern=n.pattern,c.minLength=n.minLength,c.maxLength=n.maxLength;else if("number"===n.type||"integer"===n.type){const t=g(n);s=t[0],o=t[1],i=h(n),a=b(n),u=f(n,e.isRequired),c.min=`${i}`,c.max=`${a}`,c.step="any"===s?"any":d(s,o||0)}return Object.assign(Object.assign({},e),{validator:u,type:m.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=I(e.path,t),r},getInputProps:()=>(c.name=e.path,c.ref=r(u),c.type=t,c.required=e.isRequired,c.id=I(e.path,t),c)})},S=()=>({}),V=e=>Object.assign(Object.assign({},R(e,"hidden")),{isRequired:!1,getLabelProps:S}),P=e=>V(q(e)),F=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)},w=e=>F(q(e)),M=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),E=e=>{const t=e.getObject();let r,n,i,a,s={},o=[];if("string"===t.type)o=t.enum?t.enum:[],s=x(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}else"boolean"===t.type&&(s=l(e.isRequired),o=["true","false"]);return Object.assign(Object.assign({},e),{validator:s,type:m.radio,getLabelProps:()=>{const r={};return r.id=e.path+"-label",r.htmlFor=void 0!==t.title?t.title:e.path,r},getItemInputProps:t=>{const r={key:""};return r.name=e.path,r.ref=e.formContext.register(s),r.type="radio",r.required=e.isRequired,r.id=M(e.path,t,o),r.value=o[t],r},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,o),r.htmlFor=M(e.path,t,o),r},getItems:()=>o})},N=e=>E(q(e)),C=e=>e+"-select",A=e=>{const t=e.getObject();let r,n,i,a,s={},o=[""];if("string"===t.type)o=o.concat(t.enum?t.enum:[]),s=x(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}else"boolean"===t.type&&(s=l(e.isRequired),o=["true","false"]);return Object.assign(Object.assign({},e),{type:m.select,validator:s,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=C(e.path),t},getSelectProps:()=>{const t={};return t.name=e.path,t.ref=e.formContext.register(s),t.required=e.isRequired,t.id=C(e.path),t},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,o),r.value=o[t],r},getItems:()=>o})},$=e=>A(q(e)),J=e=>R(e,"password"),T=e=>J(q(e)),U=e=>e+"-textarea-input",z=e=>{const t=e.getObject();let r={};const n={};return"string"===t.type?(r=x(t,e.isRequired),n.minLength=t.minLength,n.maxLength=t.maxLength):"number"!==t.type&&"integer"!==t.type||(r=f(t,e.isRequired)),Object.assign(Object.assign({},e),{type:m.textArea,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=U(e.path),t},getTextAreaProps:()=>(n.name=e.path,n.ref=e.formContext.register(r),n.required=e.isRequired,n.id=U(e.path),n)})},B=e=>z(q(e));function D(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(A(e)):r.push(F(e));break;case"integer":case"number":r.push(F(e));break;case"array":case"boolean":r.push(L(e))}return r}function G(e,t,r,n){let i=[];const{JSONSchema:a}=t,s=O(e,t,r);if("object"===a.type){return Object.keys(a.properties).reduce(function(e,t,r,n){const i=t.required;return(a,s)=>{const o=!!i&&-1!==i.indexOf(s),u=r&&r.properties?r.properties[s]:void 0,m=y(e,s),c={JSONSchema:t.properties[s],isRequired:o,objectName:s},p=G(n,c,m,u);return a.concat(p)}}(r,a,n,e),[])}if(!n)return i.concat(D(s));switch(n.type){case c.default:i=i.concat(D(s));break;case c.checkbox:i.push(L(s));break;case c.hidden:i.push(V(s));break;case c.input:i.push(F(s));break;case c.password:i.push(J(s));break;case c.radio:i.push(E(s));break;case c.select:i.push(A(s));break;case c.textArea:i.push(z(s))}return i}const H=e=>G(o(),v(e.path),e.path,e.UISchema);export{p as ErrorTypes,u as FormContext,m as InputTypes,c as UITypes,y as concatFormPath,j as useCheckbox,q as useGenericInput,P as useHidden,w as useInput,H as useObject,_ as useObjectFromForm,v as useObjectFromPath,T as usePassword,N as useRadio,$ as useSelect,B as useTextArea}; | ||
import{useForm as e}from"react-hook-form";export{Controller}from"react-hook-form";import t,{createContext as r,useContext as n}from"react";const a=e=>{const t=e.split("/");return"#"===t[0]&&t.shift(),""===t[t.length-1]&&t.pop(),t},i=(e,t)=>{const r=Object.keys(t).sort(),n={};for(const i of r){const r=a(i);if(!r&&t[i])continue;let s=n,o=e;for(let e=0;e<r.length&&void 0!==o;e++){if(o.type&&"object"===o.type&&(o=o.properties,o=o[r[e]]),e===r.length-1){if(!o)break;"integer"===o.type?s[r[e]]=parseInt(t[i]):"number"===o.type?s[r[e]]=parseFloat(t[i]):"boolean"===o.type?s[r[e]]="true"===t[i]:s[r[e]]=t[i]}else void 0===s[r[e]]&&(s[r[e]]={});s=s[r[e]]}}return n},s=r(null);function o(){return n(s)}const m=r=>{const{validationMode:n="onSubmit",revalidateMode:a="onChange",submitFocusError:o=!0}=r,m=e({mode:n,reValidateMode:a,submitFocusError:o}),u={};return u.onSubmit=m.handleSubmit((e,t)=>{r.onSubmit&&r.onSubmit(i(r.schema,e),t)}),r.noNativeValidate&&(u.noValidate=r.noNativeValidate),t.createElement(s.Provider,{value:Object.assign(Object.assign({},m),{schema:r.schema,customValidators:r.customValidators})},t.createElement("form",Object.assign({},u),r.children))};var u,c,p;!function(e){e.generic="generic",e.radio="radio",e.select="select",e.input="input",e.textArea="textArea",e.checkbox="checkbox"}(u||(u={})),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"}(c||(c={})),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__"}(p||(p={}));const l=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},g=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]},d=e=>{const[t]=g(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},h=e=>{const[t]=g(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 b(e,t){return Object.keys(e).reduce((r,n)=>(r[n]=r=>e[n](r,t),r),{})}const f=(e,t,r)=>{const{currentObject:n}=r,a=Object.keys(t).length>0||n.enum,i=Object.assign({},a?{validate:Object.assign(Object.assign({},b(t,r)),n.enum?{enumValidator:e=>{if(n.enum)for(const t of n.enum)if(t==e)return!0;return!1}}:void 0)}:void 0);switch(e&&(i.required=p.required),n.type){case"integer":case"number":return((e,t)=>{const r=d(e),n=h(e);return t.validate=Object.assign(Object.assign({},t.validate),{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||p.multipleOf)}),"integer"===e.type?t.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:p.pattern}:t.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:p.pattern},(r||0===r)&&(t.min={value:r,message:p.minValue}),(n||0===n)&&(t.max={value:n,message:p.maxValue}),t})(n,i);case"string":return((e,t)=>(e.minLength&&(t.minLength={value:e.minLength,message:p.minLength}),e.maxLength&&(t.maxLength={value:e.maxLength,message:p.maxLength}),e.pattern&&(t.pattern={value:new RegExp(e.pattern),message:p.pattern}),t))(n,i);case"boolean":return i;default:return{}}},x=(e,t)=>e+"/"+t,y=e=>{const t=a(e);let r=o().schema,n=!1,i="";for(let e=0;e<t.length;e++){if(!r||!r.type||"object"!==r.type){r={};break}n=!!(r.required&&r.required.indexOf(t[e])>-1),i=t[e],r=r.properties[t[e]]}return{JSONSchema:r,isRequired:n,objectName:i}},_=e=>i(o().schema,e),v=(e,t,r)=>{var n;const{JSONSchema:a,isRequired:i,objectName:s}=t;let o,m,c;if("number"===a.type||"integer"===a.type){const e=g(a);c=e[0],o=d(a),m=h(a)}return{name:s,path:r,isRequired:i,formContext:e,type:u.generic,validator:f(i,(n=e.customValidators,null!=n?n:{}),{currentObject:a,path:r}),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:p.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:p.maxLength,expected:t.maxItems}}}if(!e)return;const m={message:"string"==typeof e.message?e.message:p.undefinedError,expected:void 0};switch(e.message){case p.required:m.message=p.required,m.expected=r;break;case p.maxLength:m.message=p.maxLength,m.expected=t.maxLength;break;case p.minLength:m.message=p.minLength,m.expected=t.minLength;break;case p.maxValue:m.message=p.maxValue,m.expected=i;break;case p.minValue:m.message=p.minValue,m.expected=s;break;case p.multipleOf:m.message=p.multipleOf,m.expected=o;break;case p.pattern:m.message=p.pattern,m.expected=t.pattern}return m})(e.errors[r]?e.errors[r]:void 0,a,i,e,r,o,m,c),getObject:()=>a,getCurrentValue:()=>e.getValues()[r]}},O=e=>{const t=o(),r=y(e);return v(t,r,e)},j=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),k=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,m=[];if("array"===n.type){if(n.items.enum)m=n.items.enum;else if("string"===n.items.type)m=n.enum?n.enum:[];else if("number"===n.items.type||"integer"===n.items.type){const e=g(n);if(s=e[0],o=e[1],a=d(n),i=h(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)m.push(l(e,o||0))}n.uniqueItems&&(m=[...new Set(m)])}else"boolean"===n.type&&(m=["true"]);return Object.assign(Object.assign({},e),{type:u.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=j(e.path,a,m),i.value=m[a],i},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,m),r.htmlFor=j(e.path,t,m),r},getItems:()=>m})},L=e=>k(O(e)),I=(e,t)=>e+"-"+t+"-input",q=(e,t)=>{const{register:r}=e.formContext,{validator:n}=e,a=e.getObject();let i,s,o,m;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=g(a);o=e[0],m=e[1],i=d(a),s=h(a),c.min=`${i}`,c.max=`${s}`,c.step="any"===o?"any":l(o,m||0)}return Object.assign(Object.assign({},e),{type:u.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=I(e.path,t),r},getInputProps:()=>(c.name=e.path,c.ref=r(n),c.type=t,c.required=e.isRequired,c.id=I(e.path,t),c)})},V=()=>({}),S=e=>Object.assign(Object.assign({},q(e,"hidden")),{isRequired:!1,getLabelProps:V}),F=e=>S(O(e)),P=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 q(e,t)},w=e=>P(O(e)),E=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),M=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,m=[];if("string"===n.type)m=n.enum?n.enum:[];else if("number"===n.type||"integer"===n.type){const e=g(n);if(s=e[0],o=e[1],a=d(n),i=h(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)m.push(l(e,o||0))}else"boolean"===n.type&&(m=["true","false"]);return Object.assign(Object.assign({},e),{type:u.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=E(e.path,n,m),a.value=m[n],a},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,m),r.htmlFor=E(e.path,t,m),r},getItems:()=>m})},R=e=>M(O(e)),C=e=>e+"-select",N=e=>{const{register:t}=e.formContext,{validator:r}=e,n=e.getObject();let a,i,s,o,m=[""];if("string"===n.type)m=m.concat(n.enum?n.enum:[]);else if("number"===n.type||"integer"===n.type){const e=g(n);if(s=e[0],o=e[1],a=d(n),i=h(n),void 0!==a&&void 0!==i&&"any"!=s)for(let e=a;e<=i;e+=s)m.push(l(e,o||0))}else"boolean"===n.type&&(m=["true","false"]);return Object.assign(Object.assign({},e),{type:u.select,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=C(e.path),t},getSelectProps:()=>{const n={};return n.name=e.path,n.ref=t(r),n.required=e.isRequired,n.id=C(e.path),n},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,m),r.value=m[t],r},getItems:()=>m})},A=e=>N(O(e)),$=e=>q(e,"password"),J=e=>$(O(e)),T=e=>e+"-textarea-input",U=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:u.textArea,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=T(e.path),t},getTextAreaProps:()=>(a.name=e.path,a.ref=t(r),a.required=e.isRequired,a.id=T(e.path),a)})},z=e=>U(O(e));function B(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(N(e)):r.push(P(e));break;case"integer":case"number":r.push(P(e));break;case"array":case"boolean":r.push(k(e))}return r}function D(e,t,r,n){let a=[];const{JSONSchema:i}=t,s=v(e,t,r);if("object"===i.type){return Object.keys(i.properties).reduce(function(e,t,r,n){const a=t.required;return(i,s)=>{const o=!!a&&-1!==a.indexOf(s),m=r&&r.properties?r.properties[s]:void 0,u=x(e,s),c={JSONSchema:t.properties[s],isRequired:o,objectName:s},p=D(n,c,u,m);return i.concat(p)}}(r,i,n,e),[])}if(!n)return a.concat(B(s));switch(n.type){case c.default:a=a.concat(B(s));break;case c.checkbox:a.push(k(s));break;case c.hidden:a.push(S(s));break;case c.input:a.push(P(s));break;case c.password:a.push($(s));break;case c.radio:a.push(M(s));break;case c.select:a.push(N(s));break;case c.textArea:a.push(U(s))}return a}const G=e=>D(o(),y(e.path),e.path,e.UISchema);export{p as ErrorTypes,m as FormContext,u as InputTypes,c as UITypes,x as concatFormPath,L as useCheckbox,F as useHidden,w as useInput,G as useObject,_ as useObjectFromForm,y as useObjectFromPath,J as usePassword,R as useRadio,A as useSelect,z as useTextArea}; |
{ | ||
"name": "react-hook-form-jsonschema", | ||
"version": "0.2.0-beta.4", | ||
"version": "0.2.0-beta.5", | ||
"description": "Wrapper arround react-hook-form to create forms from a JSON schema.", | ||
@@ -49,4 +49,4 @@ "main": "output/index.cjs.js", | ||
"hooks": { | ||
"pre-commit": "git stash push --keep-index -m 'husky-pre-commit' && run-s lint && git stash pop || true", | ||
"pre-push": "git stash push --keep-index -m 'husky-pre-commit' && run-s test && git stash pop || true" | ||
"pre-commit": "run-s lint", | ||
"pre-push": "set -o pipefail && git stash push --keep-index -m 'husky-pre-commit' && run-s test && git stash pop || true" | ||
} | ||
@@ -53,0 +53,0 @@ }, |
@@ -93,2 +93,10 @@ # react-hook-form-jsonschema | ||
- `customValidators`: An object where each member has to be a funtion with the following format: | ||
- `function(value: string, context: CustomValidatorContext) => CustomValidatorReturnValue` | ||
- `params`: | ||
- `value`: Is the current value in the form input. | ||
- `context`: Is an object with the following fields: | ||
- `currentObject`: Is the current subschema this field refers to. | ||
- `path`: Is the path to that subschema. | ||
- `return value`: Must be either a `string` that identifies the error or a `true` value indicating the validation was succesfull. | ||
- `validationMode`: String to indicate when to validate the input, default is `'onSubmit'`. | ||
@@ -95,0 +103,0 @@ - `'onBlur'`: Validate when an input field is blurred. |
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
64710
32
536
576