@felte/core
Advanced tools
Comparing version 1.0.0-next.15 to 1.0.0-next.16
@@ -302,12 +302,35 @@ 'use strict'; | ||
return objValue; | ||
if (!srcValue) | ||
return objValue; | ||
if (!objValue || !srcValue) | ||
return; | ||
if (!Array.isArray(objValue)) | ||
objValue = [objValue]; | ||
if (Array.isArray(objValue)) { | ||
if (!Array.isArray(srcValue)) | ||
return [...objValue, srcValue]; | ||
const newErrors = []; | ||
for (let i = 0; i < srcValue.length; i++) { | ||
let obj = objValue[i]; | ||
let src = srcValue[i]; | ||
if (!_isPlainObject(obj) && !_isPlainObject(src)) { | ||
if (!Array.isArray(obj)) | ||
obj = [obj]; | ||
if (!Array.isArray(src)) | ||
src = [src]; | ||
newErrors.push(...obj, ...src); | ||
} | ||
else { | ||
newErrors.push(mergeErrors([obj !== null && obj !== void 0 ? obj : {}, src !== null && src !== void 0 ? src : {}])); | ||
} | ||
} | ||
return newErrors.filter(Boolean); | ||
} | ||
if (!Array.isArray(srcValue)) | ||
srcValue = [srcValue]; | ||
return [...objValue, ...srcValue]; | ||
return [objValue, ...srcValue] | ||
.reduce((acc, value) => acc.concat(value), []) | ||
.filter(Boolean); | ||
} | ||
function mergeErrors(errors) { | ||
return _mergeWith(...errors, executeCustomizer); | ||
const merged = _mergeWith(...errors, executeCustomizer); | ||
return merged; | ||
} | ||
@@ -322,35 +345,6 @@ function runValidations(values, validationOrValidations) { | ||
} | ||
async function executeValidation(values, validations) { | ||
async function executeValidation(values, shape, validations) { | ||
const errors = await Promise.all(runValidations(values, validations)); | ||
const merged = mergeErrors(errors); | ||
return syncFieldArrays(values, merged); | ||
return mergeErrors([shape, ...errors]); | ||
} | ||
function fieldArrayCustomizer(data, error) { | ||
if (_isPlainObject(data)) | ||
return; | ||
if (!Array.isArray(data) || !Array.isArray(error)) | ||
return error; | ||
if (data.length === 0) | ||
return error; | ||
if (data.length === error.length) | ||
return; | ||
const newError = []; | ||
for (let i = 0; i < error.length; i++) { | ||
const value = error[i]; | ||
const index = i % data.length; | ||
if (isNaN(index) || !value) | ||
continue; | ||
if (!Array.isArray(newError[index])) | ||
newError[index] = []; | ||
newError[index].push(value); | ||
} | ||
return newError.map((e) => { | ||
if (e.every((o) => _isPlainObject(o))) | ||
return mergeErrors(e); | ||
return e; | ||
}); | ||
} | ||
function syncFieldArrays(shape, error) { | ||
return _mergeWith(deepSet(shape, null), error, fieldArrayCustomizer); | ||
} | ||
@@ -416,2 +410,3 @@ function executeTransforms(values, transforms) { | ||
let defaultData = {}; | ||
let defaultTouched = {}; | ||
for (const el of node.elements) { | ||
@@ -433,11 +428,11 @@ if (isFieldSetElement(el)) | ||
defaultData = _set(defaultData, elName, el.checked); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
} | ||
defaultData = _set(defaultData, elName, el.checked ? [el.value] : []); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
} | ||
if (Array.isArray(_get(defaultData, elName)) && el.checked) { | ||
defaultData = _update(defaultData, elName, (value) => { | ||
return [...value, el.value]; | ||
}); | ||
defaultData = _update(defaultData, elName, (value) => [...value, el.value]); | ||
} | ||
@@ -450,2 +445,3 @@ continue; | ||
defaultData = _set(defaultData, elName, el.checked ? el.value : undefined); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
@@ -455,2 +451,3 @@ } | ||
defaultData = _set(defaultData, elName, el.multiple ? Array.from(el.files || []) : (_a = el.files) === null || _a === void 0 ? void 0 : _a[0]); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
@@ -461,4 +458,5 @@ } | ||
defaultData = _set(defaultData, elName, inputValue); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
} | ||
return { defaultData }; | ||
return { defaultData, defaultTouched }; | ||
} | ||
@@ -559,2 +557,4 @@ function setControlValue(el, value) { | ||
var _a; | ||
let formNode; | ||
let initialValues = ((_a = config.initialValues) !== null && _a !== void 0 ? _a : {}); | ||
const { data, touched, errors, warnings, isDirty, isSubmitting } = stores; | ||
@@ -598,10 +598,12 @@ const setData = createSetHelper(data.update); | ||
function addField(path, value, index) { | ||
const errValue = _isPlainObject(value) ? deepSet(value, []) : []; | ||
const touchedValue = _isPlainObject(value) ? deepSet(value, false) : false; | ||
errors.update(($errors) => { | ||
return addAtIndex($errors, path, null, index); | ||
return addAtIndex($errors, path, errValue, index); | ||
}); | ||
warnings.update(($warnings) => { | ||
return addAtIndex($warnings, path, null, index); | ||
return addAtIndex($warnings, path, errValue, index); | ||
}); | ||
touched.update(($touched) => { | ||
return addAtIndex($touched, path, false, index); | ||
return addAtIndex($touched, path, touchedValue, index); | ||
}); | ||
@@ -616,2 +618,8 @@ data.update(($data) => { | ||
const initialValue = _get(initialValues, path); | ||
const errValue = _isPlainObject(initialValue) | ||
? deepSet(initialValue, []) | ||
: []; | ||
const touchedValue = _isPlainObject(initialValue) | ||
? deepSet(initialValue, false) | ||
: false; | ||
data.update(($data) => { | ||
@@ -624,9 +632,9 @@ const newData = _set($data, path, initialValue); | ||
touched.update(($touched) => { | ||
return _set($touched, path, false); | ||
return _set($touched, path, touchedValue); | ||
}); | ||
errors.update(($errors) => { | ||
return _set($errors, path, null); | ||
return _set($errors, path, errValue); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _set($warnings, path, null); | ||
return _set($warnings, path, errValue); | ||
}); | ||
@@ -640,15 +648,12 @@ } | ||
const currentData = get(data); | ||
const initialErrors = deepSet(currentData, null); | ||
const shape = deepSet(get(touched), []); | ||
setTouched((t) => { | ||
return deepSet(t, true); | ||
}); | ||
const partialErrors = await executeValidation(currentData, validate); | ||
const currentErrors = _merge(initialErrors, partialErrors); | ||
const currentWarnings = await executeValidation(currentData, warn); | ||
warnings.set(_merge(initialErrors, currentWarnings || {})); | ||
errors.set(currentErrors || initialErrors); | ||
const currentErrors = await executeValidation(currentData, shape, validate); | ||
const currentWarnings = await executeValidation(currentData, shape, warn); | ||
warnings.set(currentWarnings || shape); | ||
errors.set(currentErrors || shape); | ||
return currentErrors; | ||
} | ||
let formNode; | ||
let initialValues = ((_a = config.initialValues) !== null && _a !== void 0 ? _a : {}); | ||
function reset() { | ||
@@ -677,3 +682,3 @@ setFields(_cloneDeep(initialValues)); | ||
const privateHelpers = { | ||
_setFormNode: (node) => { | ||
_setFormNode(node) { | ||
formNode = node; | ||
@@ -774,7 +779,7 @@ }, | ||
const currentData = get(data); | ||
const partialErrors = await executeValidation(currentData, validate); | ||
const currentErrors = _merge(deepSet(currentData, null), partialErrors); | ||
const currentWarnings = await executeValidation(currentData, warn); | ||
const shape = deepSet(get(touched), []); | ||
const currentErrors = await executeValidation(currentData, shape, validate); | ||
const currentWarnings = await executeValidation(currentData, shape, warn); | ||
if (currentWarnings) | ||
warnings.set(_merge(deepSet(currentData, null), currentWarnings)); | ||
warnings.set(_merge(deepSet(currentData, []), currentWarnings)); | ||
touched.update((t) => { | ||
@@ -785,3 +790,3 @@ return deepSet(t, true); | ||
errors.set(currentErrors); | ||
const hasErrors = deepSome(currentErrors, (error) => !!error); | ||
const hasErrors = deepSome(currentErrors, (error) => Array.isArray(error) ? error.length >= 1 : !!error); | ||
if (hasErrors) { | ||
@@ -855,7 +860,7 @@ _getCurrentExtenders().forEach((extender) => { | ||
node.noValidate = !!config.validate; | ||
const { defaultData } = getFormDefaultValues(node); | ||
const { defaultData, defaultTouched } = getFormDefaultValues(node); | ||
_setFormNode(node); | ||
setInitialValues(_merge(_cloneDeep(defaultData), _getInitialValues())); | ||
setFields(_getInitialValues()); | ||
touched.set(deepSet(_getInitialValues(), false)); | ||
touched.set(defaultTouched); | ||
function setCheckboxValues(target) { | ||
@@ -955,13 +960,25 @@ const elPath = getPath(target); | ||
continue; | ||
const fieldArrayReg = /.*(\[[0-9]+\]|\.[0-9]+)\.[^.]+$/; | ||
let fieldName = getPath(control); | ||
const shape = get(touched); | ||
const isFieldArray = fieldArrayReg.test(fieldName); | ||
if (isFieldArray) { | ||
const arrayPath = fieldName.split('.').slice(0, -1).join('.'); | ||
const valueToRemove = _get(shape, arrayPath); | ||
if (_isPlainObject(valueToRemove) && | ||
Object.keys(valueToRemove).length <= 1) { | ||
fieldName = arrayPath; | ||
} | ||
} | ||
data.update(($data) => { | ||
return _unset($data, getPath(control)); | ||
return _unset($data, fieldName); | ||
}); | ||
touched.update(($touched) => { | ||
return _unset($touched, getPath(control)); | ||
return _unset($touched, fieldName); | ||
}); | ||
errors.update(($errors) => { | ||
return _unset($errors, getPath(control)); | ||
return _unset($errors, fieldName); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _unset($warnings, getPath(control)); | ||
return _unset($warnings, fieldName); | ||
}); | ||
@@ -987,4 +1004,3 @@ } | ||
_setCurrentExtenders(extender.map(callExtender('UPDATE'))); | ||
const { defaultData: newDefaultData } = getFormDefaultValues(node); | ||
const newDefaultTouched = deepSet(newDefaultData, false); | ||
const { defaultData: newDefaultData, defaultTouched: newDefaultTouched, } = getFormDefaultValues(node); | ||
data.update(($data) => _defaultsDeep($data, newDefaultData)); | ||
@@ -1072,9 +1088,41 @@ touched.update(($touched) => { | ||
const errArray = Array.isArray(errValue) ? errValue : []; | ||
return touchValue.map((value, index) => (value && errArray[index]) || null); | ||
return touchValue.map((value, index) => { | ||
const err = errArray[index]; | ||
if (Array.isArray(err) && err.length === 0) | ||
return null; | ||
return (value && err) || null; | ||
}); | ||
} | ||
return (touchValue && errValue) || null; | ||
if (Array.isArray(errValue) && errValue.length === 0) | ||
return null; | ||
if (Array.isArray(errValue)) | ||
return errValue; | ||
return touchValue && errValue ? [errValue] : null; | ||
} | ||
function warningFilterer(errValue, touchValue) { | ||
if (_isPlainObject(touchValue)) | ||
return; | ||
if (Array.isArray(touchValue)) { | ||
if (touchValue.some(_isPlainObject)) | ||
return; | ||
const errArray = Array.isArray(errValue) ? errValue : []; | ||
return touchValue.map((_, index) => { | ||
const err = errArray[index]; | ||
if (Array.isArray(err) && err.length === 0) | ||
return null; | ||
return err || null; | ||
}); | ||
} | ||
if (Array.isArray(errValue) && errValue.length === 0) | ||
return null; | ||
if (Array.isArray(errValue)) | ||
return errValue; | ||
return errValue ? [errValue] : null; | ||
} | ||
function filterErrors([errors, touched]) { | ||
return _mergeWith(errors, touched, errorFilterer); | ||
} | ||
function filterWarnings([errors, touched]) { | ||
return _mergeWith(errors, touched, warningFilterer); | ||
} | ||
function debounce(func, timeout = 300) { | ||
@@ -1091,6 +1139,6 @@ let timer; | ||
let activeController; | ||
return async function executeValidations($data, validations) { | ||
return async function executeValidations($data, shape, validations) { | ||
if (!validations || !$data) | ||
return; | ||
let current = {}; | ||
let current = shape !== null && shape !== void 0 ? shape : deepSet($data, []); | ||
const controller = createAbortController(); | ||
@@ -1106,3 +1154,3 @@ if (activeController) | ||
current = mergeErrors([current, result]); | ||
store.set(syncFieldArrays($data, current)); | ||
store.set(current); | ||
}); | ||
@@ -1147,4 +1195,5 @@ }; | ||
: {}; | ||
let storesShape = deepSet(initialValues, []); | ||
const data = storeFactory(initialValues); | ||
const initialErrors = deepSet(initialValues, null); | ||
const initialErrors = deepSet(initialValues, []); | ||
const immediateErrors = storeFactory(initialErrors); | ||
@@ -1156,3 +1205,3 @@ const debouncedErrors = storeFactory(_cloneDeep(initialErrors)); | ||
], mergeErrors, _cloneDeep(initialErrors)); | ||
const initialWarnings = deepSet(initialValues, null); | ||
const initialWarnings = deepSet(initialValues, []); | ||
const immediateWarnings = storeFactory(initialWarnings); | ||
@@ -1167,2 +1216,3 @@ const debouncedWarnings = storeFactory(_cloneDeep(initialWarnings)); | ||
const [filteredErrors, startFilteredErrors, stopFilteredErrors] = derived([errors, touched], filterErrors, _cloneDeep(initialErrors)); | ||
const [filteredWarnings, startFilteredWarnings, stopFilteredWarnings,] = derived([warnings, touched], filterWarnings, _cloneDeep(initialWarnings)); | ||
let firstCalled = false; | ||
@@ -1176,3 +1226,3 @@ const [isValid, startIsValid, stopIsValid] = derived(errors, ([$errors]) => { | ||
else { | ||
return !deepSome($errors, (error) => !!error); | ||
return !deepSome($errors, (error) => Array.isArray(error) ? error.length >= 1 : !!error); | ||
} | ||
@@ -1191,9 +1241,12 @@ }, !config.validate && !((_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate)); | ||
var _a, _b; | ||
validateErrors($data, config.validate); | ||
validateWarnings($data, config.warn); | ||
validateErrors($data, storesShape, config.validate); | ||
validateWarnings($data, storesShape, config.warn); | ||
debouncedErrors.set({}); | ||
validateDebouncedErrors($data, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate); | ||
validateDebouncedErrors($data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate); | ||
debouncedWarnings.set({}); | ||
validateDebouncedWarnings($data, (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.warn); | ||
validateDebouncedWarnings($data, storesShape, (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.warn); | ||
}); | ||
touched.subscribe(($touched) => { | ||
storesShape = deepSet($touched, []); | ||
}); | ||
startErrors(); | ||
@@ -1203,2 +1256,3 @@ startIsValid(); | ||
startFilteredErrors(); | ||
startFilteredWarnings(); | ||
function cleanup() { | ||
@@ -1209,2 +1263,3 @@ dataUnsubscriber(); | ||
stopWarnings(); | ||
stopFilteredWarnings(); | ||
stopIsValid(); | ||
@@ -1217,8 +1272,9 @@ } | ||
immediateErrors.update; | ||
warnings.set = immediateWarnings.set; | ||
warnings.update = immediateWarnings.update; | ||
filteredWarnings.set = immediateWarnings.set; | ||
filteredWarnings.update = | ||
immediateWarnings.update; | ||
return { | ||
data, | ||
errors: filteredErrors, | ||
warnings: warnings, | ||
warnings: filteredWarnings, | ||
touched, | ||
@@ -1401,3 +1457,2 @@ isValid, | ||
exports.shouldIgnore = shouldIgnore; | ||
exports.syncFieldArrays = syncFieldArrays; | ||
//# sourceMappingURL=index.js.map |
@@ -5,5 +5,5 @@ import { __rest } from './external/.pnpm/tslib@2.3.1/external/tslib/tslib.es6.js'; | ||
import { getAllValidators } from './get-validators.js'; | ||
import { deepSet } from './packages/common/dist/esm/utils/deepSet.js'; | ||
import { executeValidation } from './packages/common/dist/esm/utils/executeValidation.js'; | ||
import { _merge } from './packages/common/dist/esm/utils/merge.js'; | ||
import { deepSet } from './packages/common/dist/esm/utils/deepSet.js'; | ||
import { deepSome } from './packages/common/dist/esm/utils/deepSome.js'; | ||
@@ -18,2 +18,3 @@ import { isFormControl, isSelectElement, isInputElement, isElement } from './packages/common/dist/esm/utils/typeGuards.js'; | ||
import { _defaultsDeep } from './packages/common/dist/esm/utils/defaultsDeep.js'; | ||
import { _isPlainObject } from './packages/common/dist/esm/utils/isPlainObject.js'; | ||
import { _unset } from './packages/common/dist/esm/utils/unset.js'; | ||
@@ -78,7 +79,7 @@ | ||
const currentData = get(data); | ||
const partialErrors = await executeValidation(currentData, validate); | ||
const currentErrors = _merge(deepSet(currentData, null), partialErrors); | ||
const currentWarnings = await executeValidation(currentData, warn); | ||
const shape = deepSet(get(touched), []); | ||
const currentErrors = await executeValidation(currentData, shape, validate); | ||
const currentWarnings = await executeValidation(currentData, shape, warn); | ||
if (currentWarnings) | ||
warnings.set(_merge(deepSet(currentData, null), currentWarnings)); | ||
warnings.set(_merge(deepSet(currentData, []), currentWarnings)); | ||
touched.update((t) => { | ||
@@ -89,3 +90,3 @@ return deepSet(t, true); | ||
errors.set(currentErrors); | ||
const hasErrors = deepSome(currentErrors, (error) => !!error); | ||
const hasErrors = deepSome(currentErrors, (error) => Array.isArray(error) ? error.length >= 1 : !!error); | ||
if (hasErrors) { | ||
@@ -159,7 +160,7 @@ _getCurrentExtenders().forEach((extender) => { | ||
node.noValidate = !!config.validate; | ||
const { defaultData } = getFormDefaultValues(node); | ||
const { defaultData, defaultTouched } = getFormDefaultValues(node); | ||
_setFormNode(node); | ||
setInitialValues(_merge(_cloneDeep(defaultData), _getInitialValues())); | ||
setFields(_getInitialValues()); | ||
touched.set(deepSet(_getInitialValues(), false)); | ||
touched.set(defaultTouched); | ||
function setCheckboxValues(target) { | ||
@@ -259,13 +260,25 @@ const elPath = getPath(target); | ||
continue; | ||
const fieldArrayReg = /.*(\[[0-9]+\]|\.[0-9]+)\.[^.]+$/; | ||
let fieldName = getPath(control); | ||
const shape = get(touched); | ||
const isFieldArray = fieldArrayReg.test(fieldName); | ||
if (isFieldArray) { | ||
const arrayPath = fieldName.split('.').slice(0, -1).join('.'); | ||
const valueToRemove = _get(shape, arrayPath); | ||
if (_isPlainObject(valueToRemove) && | ||
Object.keys(valueToRemove).length <= 1) { | ||
fieldName = arrayPath; | ||
} | ||
} | ||
data.update(($data) => { | ||
return _unset($data, getPath(control)); | ||
return _unset($data, fieldName); | ||
}); | ||
touched.update(($touched) => { | ||
return _unset($touched, getPath(control)); | ||
return _unset($touched, fieldName); | ||
}); | ||
errors.update(($errors) => { | ||
return _unset($errors, getPath(control)); | ||
return _unset($errors, fieldName); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _unset($warnings, getPath(control)); | ||
return _unset($warnings, fieldName); | ||
}); | ||
@@ -291,4 +304,3 @@ } | ||
_setCurrentExtenders(extender.map(callExtender('UPDATE'))); | ||
const { defaultData: newDefaultData } = getFormDefaultValues(node); | ||
const newDefaultTouched = deepSet(newDefaultData, false); | ||
const { defaultData: newDefaultData, defaultTouched: newDefaultTouched, } = getFormDefaultValues(node); | ||
data.update(($data) => _defaultsDeep($data, newDefaultData)); | ||
@@ -295,0 +307,0 @@ touched.update(($touched) => { |
@@ -7,6 +7,6 @@ import { get } from './get.js'; | ||
import { setForm } from './packages/common/dist/esm/utils/domUtils.js'; | ||
import { _isPlainObject } from './packages/common/dist/esm/utils/isPlainObject.js'; | ||
import { deepSet } from './packages/common/dist/esm/utils/deepSet.js'; | ||
import { _update } from './packages/common/dist/esm/utils/update.js'; | ||
import { deepSet } from './packages/common/dist/esm/utils/deepSet.js'; | ||
import { executeValidation } from './packages/common/dist/esm/utils/executeValidation.js'; | ||
import { _merge } from './packages/common/dist/esm/utils/merge.js'; | ||
import { _cloneDeep } from './packages/common/dist/esm/utils/cloneDeep.js'; | ||
@@ -49,2 +49,4 @@ | ||
var _a; | ||
let formNode; | ||
let initialValues = ((_a = config.initialValues) !== null && _a !== void 0 ? _a : {}); | ||
const { data, touched, errors, warnings, isDirty, isSubmitting } = stores; | ||
@@ -88,10 +90,12 @@ const setData = createSetHelper(data.update); | ||
function addField(path, value, index) { | ||
const errValue = _isPlainObject(value) ? deepSet(value, []) : []; | ||
const touchedValue = _isPlainObject(value) ? deepSet(value, false) : false; | ||
errors.update(($errors) => { | ||
return addAtIndex($errors, path, null, index); | ||
return addAtIndex($errors, path, errValue, index); | ||
}); | ||
warnings.update(($warnings) => { | ||
return addAtIndex($warnings, path, null, index); | ||
return addAtIndex($warnings, path, errValue, index); | ||
}); | ||
touched.update(($touched) => { | ||
return addAtIndex($touched, path, false, index); | ||
return addAtIndex($touched, path, touchedValue, index); | ||
}); | ||
@@ -106,2 +110,8 @@ data.update(($data) => { | ||
const initialValue = _get(initialValues, path); | ||
const errValue = _isPlainObject(initialValue) | ||
? deepSet(initialValue, []) | ||
: []; | ||
const touchedValue = _isPlainObject(initialValue) | ||
? deepSet(initialValue, false) | ||
: false; | ||
data.update(($data) => { | ||
@@ -114,9 +124,9 @@ const newData = _set($data, path, initialValue); | ||
touched.update(($touched) => { | ||
return _set($touched, path, false); | ||
return _set($touched, path, touchedValue); | ||
}); | ||
errors.update(($errors) => { | ||
return _set($errors, path, null); | ||
return _set($errors, path, errValue); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _set($warnings, path, null); | ||
return _set($warnings, path, errValue); | ||
}); | ||
@@ -130,15 +140,12 @@ } | ||
const currentData = get(data); | ||
const initialErrors = deepSet(currentData, null); | ||
const shape = deepSet(get(touched), []); | ||
setTouched((t) => { | ||
return deepSet(t, true); | ||
}); | ||
const partialErrors = await executeValidation(currentData, validate); | ||
const currentErrors = _merge(initialErrors, partialErrors); | ||
const currentWarnings = await executeValidation(currentData, warn); | ||
warnings.set(_merge(initialErrors, currentWarnings || {})); | ||
errors.set(currentErrors || initialErrors); | ||
const currentErrors = await executeValidation(currentData, shape, validate); | ||
const currentWarnings = await executeValidation(currentData, shape, warn); | ||
warnings.set(currentWarnings || shape); | ||
errors.set(currentErrors || shape); | ||
return currentErrors; | ||
} | ||
let formNode; | ||
let initialValues = ((_a = config.initialValues) !== null && _a !== void 0 ? _a : {}); | ||
function reset() { | ||
@@ -167,3 +174,3 @@ setFields(_cloneDeep(initialValues)); | ||
const privateHelpers = { | ||
_setFormNode: (node) => { | ||
_setFormNode(node) { | ||
formNode = node; | ||
@@ -170,0 +177,0 @@ }, |
@@ -22,5 +22,5 @@ export { get as getValueFromStore } from './get.js'; | ||
export { getValue } from './packages/common/dist/esm/utils/getValue.js'; | ||
export { executeValidation, mergeErrors, runValidations, syncFieldArrays } from './packages/common/dist/esm/utils/executeValidation.js'; | ||
export { executeValidation, mergeErrors, runValidations } from './packages/common/dist/esm/utils/executeValidation.js'; | ||
export { executeTransforms } from './packages/common/dist/esm/utils/executeTransforms.js'; | ||
export { addAttrsFromFieldset, getFormControls, getFormDefaultValues, getInputTextOrNumber, setControlValue, setForm } from './packages/common/dist/esm/utils/domUtils.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -58,2 +58,3 @@ import { isFormControl, isFieldSetElement, isInputElement } from './typeGuards.js'; | ||
let defaultData = {}; | ||
let defaultTouched = {}; | ||
for (const el of node.elements) { | ||
@@ -75,11 +76,11 @@ if (isFieldSetElement(el)) | ||
defaultData = _set(defaultData, elName, el.checked); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
} | ||
defaultData = _set(defaultData, elName, el.checked ? [el.value] : []); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
} | ||
if (Array.isArray(_get(defaultData, elName)) && el.checked) { | ||
defaultData = _update(defaultData, elName, (value) => { | ||
return [...value, el.value]; | ||
}); | ||
defaultData = _update(defaultData, elName, (value) => [...value, el.value]); | ||
} | ||
@@ -92,2 +93,3 @@ continue; | ||
defaultData = _set(defaultData, elName, el.checked ? el.value : undefined); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
@@ -97,2 +99,3 @@ } | ||
defaultData = _set(defaultData, elName, el.multiple ? Array.from(el.files || []) : (_a = el.files) === null || _a === void 0 ? void 0 : _a[0]); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
@@ -103,4 +106,5 @@ } | ||
defaultData = _set(defaultData, elName, inputValue); | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
} | ||
return { defaultData }; | ||
return { defaultData, defaultTouched }; | ||
} | ||
@@ -107,0 +111,0 @@ function setControlValue(el, value) { |
import { _mergeWith } from './mergeWith.js'; | ||
import { _isPlainObject } from './isPlainObject.js'; | ||
import { deepSet } from './deepSet.js'; | ||
@@ -12,12 +11,35 @@ function executeCustomizer(objValue, srcValue) { | ||
return objValue; | ||
if (!srcValue) | ||
return objValue; | ||
if (!objValue || !srcValue) | ||
return; | ||
if (!Array.isArray(objValue)) | ||
objValue = [objValue]; | ||
if (Array.isArray(objValue)) { | ||
if (!Array.isArray(srcValue)) | ||
return [...objValue, srcValue]; | ||
const newErrors = []; | ||
for (let i = 0; i < srcValue.length; i++) { | ||
let obj = objValue[i]; | ||
let src = srcValue[i]; | ||
if (!_isPlainObject(obj) && !_isPlainObject(src)) { | ||
if (!Array.isArray(obj)) | ||
obj = [obj]; | ||
if (!Array.isArray(src)) | ||
src = [src]; | ||
newErrors.push(...obj, ...src); | ||
} | ||
else { | ||
newErrors.push(mergeErrors([obj !== null && obj !== void 0 ? obj : {}, src !== null && src !== void 0 ? src : {}])); | ||
} | ||
} | ||
return newErrors.filter(Boolean); | ||
} | ||
if (!Array.isArray(srcValue)) | ||
srcValue = [srcValue]; | ||
return [...objValue, ...srcValue]; | ||
return [objValue, ...srcValue] | ||
.reduce((acc, value) => acc.concat(value), []) | ||
.filter(Boolean); | ||
} | ||
function mergeErrors(errors) { | ||
return _mergeWith(...errors, executeCustomizer); | ||
const merged = _mergeWith(...errors, executeCustomizer); | ||
return merged; | ||
} | ||
@@ -32,37 +54,8 @@ function runValidations(values, validationOrValidations) { | ||
} | ||
async function executeValidation(values, validations) { | ||
async function executeValidation(values, shape, validations) { | ||
const errors = await Promise.all(runValidations(values, validations)); | ||
const merged = mergeErrors(errors); | ||
return syncFieldArrays(values, merged); | ||
return mergeErrors([shape, ...errors]); | ||
} | ||
function fieldArrayCustomizer(data, error) { | ||
if (_isPlainObject(data)) | ||
return; | ||
if (!Array.isArray(data) || !Array.isArray(error)) | ||
return error; | ||
if (data.length === 0) | ||
return error; | ||
if (data.length === error.length) | ||
return; | ||
const newError = []; | ||
for (let i = 0; i < error.length; i++) { | ||
const value = error[i]; | ||
const index = i % data.length; | ||
if (isNaN(index) || !value) | ||
continue; | ||
if (!Array.isArray(newError[index])) | ||
newError[index] = []; | ||
newError[index].push(value); | ||
} | ||
return newError.map((e) => { | ||
if (e.every((o) => _isPlainObject(o))) | ||
return mergeErrors(e); | ||
return e; | ||
}); | ||
} | ||
function syncFieldArrays(shape, error) { | ||
return _mergeWith(deepSet(shape, null), error, fieldArrayCustomizer); | ||
} | ||
export { executeValidation, mergeErrors, runValidations, syncFieldArrays }; | ||
export { executeValidation, mergeErrors, runValidations }; | ||
//# sourceMappingURL=executeValidation.js.map |
import { executeTransforms } from './packages/common/dist/esm/utils/executeTransforms.js'; | ||
import { _cloneDeep } from './packages/common/dist/esm/utils/cloneDeep.js'; | ||
import { deepSet } from './packages/common/dist/esm/utils/deepSet.js'; | ||
import { mergeErrors, runValidations, syncFieldArrays } from './packages/common/dist/esm/utils/executeValidation.js'; | ||
import { mergeErrors, runValidations } from './packages/common/dist/esm/utils/executeValidation.js'; | ||
import { deepSome } from './packages/common/dist/esm/utils/deepSome.js'; | ||
@@ -25,9 +25,41 @@ import { _mergeWith } from './packages/common/dist/esm/utils/mergeWith.js'; | ||
const errArray = Array.isArray(errValue) ? errValue : []; | ||
return touchValue.map((value, index) => (value && errArray[index]) || null); | ||
return touchValue.map((value, index) => { | ||
const err = errArray[index]; | ||
if (Array.isArray(err) && err.length === 0) | ||
return null; | ||
return (value && err) || null; | ||
}); | ||
} | ||
return (touchValue && errValue) || null; | ||
if (Array.isArray(errValue) && errValue.length === 0) | ||
return null; | ||
if (Array.isArray(errValue)) | ||
return errValue; | ||
return touchValue && errValue ? [errValue] : null; | ||
} | ||
function warningFilterer(errValue, touchValue) { | ||
if (_isPlainObject(touchValue)) | ||
return; | ||
if (Array.isArray(touchValue)) { | ||
if (touchValue.some(_isPlainObject)) | ||
return; | ||
const errArray = Array.isArray(errValue) ? errValue : []; | ||
return touchValue.map((_, index) => { | ||
const err = errArray[index]; | ||
if (Array.isArray(err) && err.length === 0) | ||
return null; | ||
return err || null; | ||
}); | ||
} | ||
if (Array.isArray(errValue) && errValue.length === 0) | ||
return null; | ||
if (Array.isArray(errValue)) | ||
return errValue; | ||
return errValue ? [errValue] : null; | ||
} | ||
function filterErrors([errors, touched]) { | ||
return _mergeWith(errors, touched, errorFilterer); | ||
} | ||
function filterWarnings([errors, touched]) { | ||
return _mergeWith(errors, touched, warningFilterer); | ||
} | ||
function debounce(func, timeout = 300) { | ||
@@ -44,6 +76,6 @@ let timer; | ||
let activeController; | ||
return async function executeValidations($data, validations) { | ||
return async function executeValidations($data, shape, validations) { | ||
if (!validations || !$data) | ||
return; | ||
let current = {}; | ||
let current = shape !== null && shape !== void 0 ? shape : deepSet($data, []); | ||
const controller = createAbortController(); | ||
@@ -59,3 +91,3 @@ if (activeController) | ||
current = mergeErrors([current, result]); | ||
store.set(syncFieldArrays($data, current)); | ||
store.set(current); | ||
}); | ||
@@ -100,4 +132,5 @@ }; | ||
: {}; | ||
let storesShape = deepSet(initialValues, []); | ||
const data = storeFactory(initialValues); | ||
const initialErrors = deepSet(initialValues, null); | ||
const initialErrors = deepSet(initialValues, []); | ||
const immediateErrors = storeFactory(initialErrors); | ||
@@ -109,3 +142,3 @@ const debouncedErrors = storeFactory(_cloneDeep(initialErrors)); | ||
], mergeErrors, _cloneDeep(initialErrors)); | ||
const initialWarnings = deepSet(initialValues, null); | ||
const initialWarnings = deepSet(initialValues, []); | ||
const immediateWarnings = storeFactory(initialWarnings); | ||
@@ -120,2 +153,3 @@ const debouncedWarnings = storeFactory(_cloneDeep(initialWarnings)); | ||
const [filteredErrors, startFilteredErrors, stopFilteredErrors] = derived([errors, touched], filterErrors, _cloneDeep(initialErrors)); | ||
const [filteredWarnings, startFilteredWarnings, stopFilteredWarnings,] = derived([warnings, touched], filterWarnings, _cloneDeep(initialWarnings)); | ||
let firstCalled = false; | ||
@@ -129,3 +163,3 @@ const [isValid, startIsValid, stopIsValid] = derived(errors, ([$errors]) => { | ||
else { | ||
return !deepSome($errors, (error) => !!error); | ||
return !deepSome($errors, (error) => Array.isArray(error) ? error.length >= 1 : !!error); | ||
} | ||
@@ -144,9 +178,12 @@ }, !config.validate && !((_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate)); | ||
var _a, _b; | ||
validateErrors($data, config.validate); | ||
validateWarnings($data, config.warn); | ||
validateErrors($data, storesShape, config.validate); | ||
validateWarnings($data, storesShape, config.warn); | ||
debouncedErrors.set({}); | ||
validateDebouncedErrors($data, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate); | ||
validateDebouncedErrors($data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate); | ||
debouncedWarnings.set({}); | ||
validateDebouncedWarnings($data, (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.warn); | ||
validateDebouncedWarnings($data, storesShape, (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.warn); | ||
}); | ||
touched.subscribe(($touched) => { | ||
storesShape = deepSet($touched, []); | ||
}); | ||
startErrors(); | ||
@@ -156,2 +193,3 @@ startIsValid(); | ||
startFilteredErrors(); | ||
startFilteredWarnings(); | ||
function cleanup() { | ||
@@ -162,2 +200,3 @@ dataUnsubscriber(); | ||
stopWarnings(); | ||
stopFilteredWarnings(); | ||
stopIsValid(); | ||
@@ -170,8 +209,9 @@ } | ||
immediateErrors.update; | ||
warnings.set = immediateWarnings.set; | ||
warnings.update = immediateWarnings.update; | ||
filteredWarnings.set = immediateWarnings.set; | ||
filteredWarnings.update = | ||
immediateWarnings.update; | ||
return { | ||
data, | ||
errors: filteredErrors, | ||
warnings: warnings, | ||
warnings: filteredWarnings, | ||
touched, | ||
@@ -178,0 +218,0 @@ isValid, |
@@ -12,3 +12,3 @@ import { Extender, FormConfig, Obj, Stores, ValidationFunction, TransformFunction, Helpers } from '@felte/common'; | ||
private: { | ||
_setFormNode: (node: HTMLFormElement) => void; | ||
_setFormNode(node: HTMLFormElement): void; | ||
_getFormNode: () => HTMLFormElement | undefined; | ||
@@ -15,0 +15,0 @@ _getInitialValues: () => Data; |
@@ -1,2 +0,2 @@ | ||
import { StoreFactory, Obj, FormConfig, Errors, Touched, PartialWritable } from '@felte/common'; | ||
import { StoreFactory, Obj, FormConfig, Touched, PartialWritableErrors } from '@felte/common'; | ||
import { Writable, Readable } from 'svelte/store'; | ||
@@ -15,4 +15,4 @@ type Readables = Readable<any> | [Readable<any>, ...Array<Readable<any>>] | Array<Readable<any>>; | ||
data: Writable<Data> & StoreExt; | ||
errors: PartialWritable<Errors<Data>> & StoreExt; | ||
warnings: PartialWritable<Errors<Data>> & StoreExt; | ||
errors: PartialWritableErrors<Data> & StoreExt; | ||
warnings: PartialWritableErrors<Data> & StoreExt; | ||
touched: Writable<Touched<Data>> & StoreExt; | ||
@@ -19,0 +19,0 @@ isValid: Readable<boolean> & { |
{ | ||
"name": "@felte/core", | ||
"version": "1.0.0-next.15", | ||
"version": "1.0.0-next.16", | ||
"description": "Core package for FelteJS", | ||
@@ -27,3 +27,3 @@ "main": "dist/cjs/index.js", | ||
"dependencies": { | ||
"@felte/common": "1.0.0-next.12" | ||
"@felte/common": "1.0.0-next.13" | ||
}, | ||
@@ -30,0 +30,0 @@ "publishConfig": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
348822
3018
+ Added@felte/common@1.0.0-next.13(transitive)
- Removed@felte/common@1.0.0-next.12(transitive)
Updated@felte/common@1.0.0-next.13