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

@felte/core

Package Overview
Dependencies
Maintainers
1
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@felte/core - npm Package Compare versions

Comparing version 1.0.0-next.17 to 1.0.0-next.18

dist/esm/deep-set-touched.js

298

dist/cjs/index.js

@@ -89,5 +89,8 @@ 'use strict';

continue;
let rsValue = customizer(obj, source);
if (typeof rsValue !== 'undefined')
return rsValue;
const keys = Object.keys(source);
for (const key of keys) {
const rsValue = customizer(obj[key], source[key]);
rsValue = customizer(obj[key], source[key]);
if (typeof rsValue !== 'undefined') {

@@ -346,6 +349,2 @@ obj[key] = rsValue;

}
async function executeValidation(values, shape, validations) {
const errors = await Promise.all(runValidations(values, validations));
return mergeErrors([shape, ...errors]);
}

@@ -509,9 +508,13 @@ function executeTransforms(values, transforms) {

function getAllValidators(prop, config) {
var _a, _b, _c;
const validate = (_a = config[prop]) !== null && _a !== void 0 ? _a : [];
const validations = Array.isArray(validate) ? validate : [validate];
const debounced = (_c = (_b = config.debounced) === null || _b === void 0 ? void 0 : _b[prop]) !== null && _c !== void 0 ? _c : [];
const debValidations = Array.isArray(debounced) ? debounced : [debounced];
return [...validations, ...debValidations];
function deepSetTouched(obj, value) {
return _mapValues(obj, (prop) => {
if (_isPlainObject(prop))
return deepSetTouched(prop, value);
if (Array.isArray(prop)) {
if (prop.length === 0 || prop.every((p) => typeof p === 'string'))
return value;
return prop.map((p) => deepSetTouched(p, value));
}
return value;
});
}

@@ -552,3 +555,3 @@

}
function createHelpers({ stores, config, }) {
function createHelpers({ stores, config, validateErrors, validateWarnings, }) {
var _a;

@@ -595,4 +598,8 @@ let formNode;

function addField(path, value, index) {
const errValue = _isPlainObject(value) ? deepSet(value, []) : [];
const touchedValue = _isPlainObject(value) ? deepSet(value, false) : false;
const touchedValue = _isPlainObject(value)
? deepSetTouched(value, false)
: false;
const errValue = _isPlainObject(touchedValue)
? deepSet(touchedValue, [])
: [];
errors.update(($errors) => {

@@ -615,8 +622,8 @@ return addAtIndex($errors, path, errValue, index);

const initialValue = _get(initialValues, path);
const errValue = _isPlainObject(initialValue)
? deepSet(initialValue, [])
: [];
const touchedValue = _isPlainObject(initialValue)
? deepSet(initialValue, false)
? deepSetTouched(initialValue, false)
: false;
const errValue = _isPlainObject(touchedValue)
? deepSet(touchedValue, [])
: [];
data.update(($data) => {

@@ -641,13 +648,6 @@ const newData = _set($data, path, initialValue);

async function validate() {
const validate = getAllValidators('validate', config);
const warn = getAllValidators('warn', config);
const currentData = get(data);
const shape = deepSet(get(touched), []);
setTouched((t) => {
return deepSet(t, true);
});
const currentErrors = await executeValidation(currentData, shape, validate);
const currentWarnings = await executeValidation(currentData, shape, warn);
warnings.set(currentWarnings || shape);
errors.set(currentErrors || shape);
touched.set(deepSetTouched(currentData, true));
const currentErrors = await validateErrors(currentData);
await validateWarnings(currentData);
return currentErrors;

@@ -755,3 +755,3 @@ }

}
function createFormAction({ helpers, stores, config, extender, _setFormNode, _getFormNode, _getInitialValues, _setCurrentExtenders, _getCurrentExtenders, }) {
function createFormAction({ helpers, stores, config, extender, validateErrors, validateWarnings, _setFormNode, _getFormNode, _getInitialValues, _setCurrentExtenders, _getCurrentExtenders, }) {
const { setFields, setTouched, reset, setInitialValues } = helpers;

@@ -761,7 +761,5 @@ const { addValidator, addTransformer, validate, setIsDirty, setIsSubmitting } = helpers, contextHelpers = __rest(helpers, ["addValidator", "addTransformer", "validate", "setIsDirty", "setIsSubmitting"]);

function createSubmitHandler(altConfig) {
var _a, _b, _c, _d;
const validate = (_a = altConfig === null || altConfig === void 0 ? void 0 : altConfig.validate) !== null && _a !== void 0 ? _a : getAllValidators('validate', config);
const warn = (_b = altConfig === null || altConfig === void 0 ? void 0 : altConfig.warn) !== null && _b !== void 0 ? _b : getAllValidators('warn', config);
const onError = (_c = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onError) !== null && _c !== void 0 ? _c : config.onError;
const onSuccess = (_d = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSuccess) !== null && _d !== void 0 ? _d : config.onSuccess;
var _a, _b;
const onError = (_a = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onError) !== null && _a !== void 0 ? _a : config.onError;
const onSuccess = (_b = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSuccess) !== null && _b !== void 0 ? _b : config.onSuccess;
return async function handleSubmit(event) {

@@ -776,12 +774,8 @@ var _a, _b;

const currentData = get(data);
const shape = deepSet(get(touched), []);
const currentErrors = await executeValidation(currentData, shape, validate);
const currentWarnings = await executeValidation(currentData, shape, warn);
const currentErrors = await validateErrors(currentData, altConfig === null || altConfig === void 0 ? void 0 : altConfig.validate);
const currentWarnings = await validateWarnings(currentData, altConfig === null || altConfig === void 0 ? void 0 : altConfig.warn);
if (currentWarnings)
warnings.set(_merge(deepSet(currentData, []), currentWarnings));
touched.update((t) => {
return deepSet(t, true);
});
touched.set(deepSetTouched(currentData, true));
if (currentErrors) {
errors.set(currentErrors);
const hasErrors = deepSome(currentErrors, (error) => Array.isArray(error) ? error.length >= 1 : !!error);

@@ -893,3 +887,2 @@ if (hasErrors) {

function handleInput(e) {
var _a;
const target = e.target;

@@ -905,4 +898,2 @@ if (!target ||

return;
if ((_a = config.touchTriggerEvents) === null || _a === void 0 ? void 0 : _a.input)
setTouched(getPath(target), true);
isDirty.set(true);

@@ -915,3 +906,2 @@ const inputValue = getInputTextOrNumber(target);

function handleChange(e) {
var _a;
const target = e.target;

@@ -922,4 +912,3 @@ if (!target || !isFormControl(target) || shouldIgnore(target))

return;
if ((_a = config.touchTriggerEvents) === null || _a === void 0 ? void 0 : _a.change)
setTouched(getPath(target), true);
setTouched(getPath(target), true);
if (isSelectElement(target) ||

@@ -944,3 +933,2 @@ ['checkbox', 'radio', 'file'].includes(target.type)) {

function handleBlur(e) {
var _a;
const target = e.target;

@@ -951,4 +939,3 @@ if (!target || !isFormControl(target) || shouldIgnore(target))

return;
if ((_a = config.touchTriggerEvents) === null || _a === void 0 ? void 0 : _a.blur)
setTouched(getPath(target), true);
setTouched(getPath(target), true);
}

@@ -1071,4 +1058,4 @@ const mutationOptions = { childList: true, subtree: true };

function createAbortController() {
const signal = { aborted: false };
function createValidationController(priority) {
const signal = { aborted: false, priority };
return {

@@ -1081,5 +1068,9 @@ signal,

}
function errorFilterer(errValue, touchValue) {
if (_isPlainObject(touchValue))
function errorFilterer(touchValue, errValue) {
if (_isPlainObject(touchValue)) {
if (_isPlainObject(errValue) && Object.keys(errValue).length === 0) {
return deepSet(touchValue, null);
}
return;
}
if (Array.isArray(touchValue)) {

@@ -1102,5 +1093,9 @@ if (touchValue.some(_isPlainObject))

}
function warningFilterer(errValue, touchValue) {
if (_isPlainObject(touchValue))
function warningFilterer(touchValue, errValue) {
if (_isPlainObject(touchValue)) {
if (_isPlainObject(errValue) && Object.keys(errValue).length === 0) {
return deepSet(touchValue, null);
}
return;
}
if (Array.isArray(touchValue)) {

@@ -1124,6 +1119,6 @@ if (touchValue.some(_isPlainObject))

function filterErrors([errors, touched]) {
return _mergeWith(errors, touched, errorFilterer);
return _mergeWith(touched, errors, errorFilterer);
}
function filterWarnings([errors, touched]) {
return _mergeWith(errors, touched, warningFilterer);
return _mergeWith(touched, errors, warningFilterer);
}

@@ -1139,22 +1134,4 @@ function debounce(func, timeout = 300) {

}
function cancellableValidation(store) {
let activeController;
return async function executeValidations($data, shape, validations) {
if (!validations || !$data)
return;
let current = shape !== null && shape !== void 0 ? shape : deepSet($data, []);
const controller = createAbortController();
if (activeController)
activeController.abort();
activeController = controller;
const results = runValidations($data, validations);
results.forEach(async (promise) => {
const result = await promise;
if (controller.signal.aborted)
return;
current = mergeErrors([current, result]);
store.set(current);
});
};
}
// A `derived` store factory that can defer subscription and be constructed
// with any store factory.
function createDerivedFactory(storeFactory) {

@@ -1196,2 +1173,50 @@ return function derived(storeOrStores, deriver, initialValue) {

: {};
const initialTouched = deepSetTouched(initialValues, false);
const touched = storeFactory(initialTouched);
const validationCount = storeFactory(0);
const [isValidating, startIsValidating, stopIsValidating] = derived([touched, validationCount], ([$touched, $validationCount]) => {
const isTouched = deepSome($touched, (t) => !!t);
return isTouched && $validationCount >= 1;
}, false);
// It is important not to destructure stores created with the factory
// since some stores may be callable.
delete isValidating.set;
delete isValidating.update;
function cancellableValidation(store) {
let activeController;
return async function executeValidations($data, shape, validations, priority = false) {
if (!validations || !$data)
return;
let current = shape && Object.keys(shape).length > 0
? shape
: deepSet($data, []);
// Keeping a controller allows us to cancel previous asynchronous
// validations if they've become stale.
const controller = createValidationController(priority);
// By assigning `priority` we can prevent specific validations
// from being aborted. Used when submitting the form or
// calling the `validate` helper.
if (!(activeController === null || activeController === void 0 ? void 0 : activeController.signal.priority) || priority) {
activeController === null || activeController === void 0 ? void 0 : activeController.abort();
activeController = controller;
}
// If the current controller has priority and we're not trying to
// override it, completely prevent validations
if (activeController.signal.priority && !priority)
return;
validationCount.update((c) => c + 1);
const results = runValidations($data, validations);
results.forEach(async (promise) => {
const result = await promise;
if (controller.signal.aborted)
return;
current = mergeErrors([current, result]);
store.set(current);
});
await Promise.all(results);
activeController = undefined;
validationCount.update((c) => c - 1);
return current;
};
}
let storesShape = deepSet(initialValues, []);

@@ -1213,6 +1238,7 @@ const data = storeFactory(initialValues);

], mergeErrors, _cloneDeep(initialWarnings));
const initialTouched = deepSet(initialValues, false);
const touched = storeFactory(initialTouched);
const [filteredErrors, startFilteredErrors, stopFilteredErrors] = derived([errors, touched], filterErrors, _cloneDeep(initialErrors));
const [filteredWarnings, startFilteredWarnings, stopFilteredWarnings,] = derived([warnings, touched], filterWarnings, _cloneDeep(initialWarnings));
// This is necessary since, on the first run, validations
// have not run yet. We assume the form is not valid in the first calling
// if there's validation functions assigned in the configuration.
let firstCalled = false;

@@ -1235,4 +1261,24 @@ const [isValid, startIsValid, stopIsValid] = derived(errors, ([$errors]) => {

const validateWarnings = cancellableValidation(immediateWarnings);
const validateDebouncedErrors = debounce(cancellableValidation(debouncedErrors), (_c = (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.validateTimeout) !== null && _c !== void 0 ? _c : (_d = config.debounced) === null || _d === void 0 ? void 0 : _d.timeout);
const validateDebouncedWarnings = debounce(cancellableValidation(debouncedWarnings), (_f = (_e = config.debounced) === null || _e === void 0 ? void 0 : _e.warnTimeout) !== null && _f !== void 0 ? _f : (_g = config.debounced) === null || _g === void 0 ? void 0 : _g.timeout);
const validateDebouncedErrors = cancellableValidation(debouncedErrors);
const validateDebouncedWarnings = cancellableValidation(debouncedWarnings);
const _validateDebouncedErrors = debounce(validateDebouncedErrors, (_c = (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.validateTimeout) !== null && _c !== void 0 ? _c : (_d = config.debounced) === null || _d === void 0 ? void 0 : _d.timeout);
const _validateDebouncedWarnings = debounce(validateDebouncedWarnings, (_f = (_e = config.debounced) === null || _e === void 0 ? void 0 : _e.warnTimeout) !== null && _f !== void 0 ? _f : (_g = config.debounced) === null || _g === void 0 ? void 0 : _g.timeout);
async function executeErrorsValidation(data, altValidate) {
var _a;
const errors = validateErrors(data, storesShape, altValidate !== null && altValidate !== void 0 ? altValidate : config.validate, true);
if (altValidate)
return errors;
const debouncedErrors = validateDebouncedErrors(data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate, true);
return mergeErrors(await Promise.all([errors, debouncedErrors]));
}
async function executeWarningsValidation(data, altWarn) {
var _a;
const warnings = validateWarnings(data, storesShape, altWarn !== null && altWarn !== void 0 ? altWarn : config.warn, true);
if (altWarn)
return warnings;
const debouncedWarnings = validateDebouncedWarnings(data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.warn, true);
return mergeErrors(await Promise.all([warnings, debouncedWarnings]));
}
let errorsValue = initialErrors;
let warningsValue = initialWarnings;
function start() {

@@ -1243,10 +1289,14 @@ const dataUnsubscriber = data.subscribe(($data) => {

validateWarnings($data, storesShape, config.warn);
debouncedErrors.set({});
validateDebouncedErrors($data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate);
debouncedWarnings.set({});
validateDebouncedWarnings($data, storesShape, (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.warn);
_validateDebouncedErrors($data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate);
_validateDebouncedWarnings($data, storesShape, (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.warn);
});
touched.subscribe(($touched) => {
const unsubscribeTouched = touched.subscribe(($touched) => {
storesShape = deepSet($touched, []);
});
const unsubscribeErrors = errors.subscribe(($errors) => {
errorsValue = $errors;
});
const unsubscribeWarnings = warnings.subscribe(($warnings) => {
warningsValue = $warnings;
});
startErrors();

@@ -1257,2 +1307,3 @@ startIsValid();

startFilteredWarnings();
startIsValidating();
function cleanup() {

@@ -1265,11 +1316,27 @@ dataUnsubscriber();

stopIsValid();
stopIsValidating();
unsubscribeTouched();
unsubscribeErrors();
unsubscribeWarnings();
}
return cleanup;
}
filteredErrors.set = immediateErrors.set;
filteredErrors.update =
immediateErrors.update;
filteredWarnings.set = immediateWarnings.set;
filteredWarnings.update =
immediateWarnings.update;
function publicErrorsUpdater(updater) {
immediateErrors.set(updater(errorsValue));
debouncedErrors.set({});
}
function publicWarningsUpdater(updater) {
immediateWarnings.set(updater(warningsValue));
debouncedWarnings.set({});
}
function publicErrorsSetter(value) {
publicErrorsUpdater(() => value);
}
function publicWarningsSetter(value) {
publicWarningsUpdater(() => value);
}
filteredErrors.set = publicErrorsSetter;
filteredErrors.update = publicErrorsUpdater;
filteredWarnings.set = publicWarningsSetter;
filteredWarnings.update = publicWarningsUpdater;
return {

@@ -1283,2 +1350,5 @@ data,

isDirty,
isValidating,
validateErrors: executeErrorsValidation,
validateWarnings: executeWarningsValidation,
cleanup: config.preventStoreStart ? () => undefined : start(),

@@ -1290,6 +1360,5 @@ start,

function createForm(config, adapters) {
var _a, _b, _c;
var _a, _b;
(_a = config.extend) !== null && _a !== void 0 ? _a : (config.extend = []);
(_b = config.debounced) !== null && _b !== void 0 ? _b : (config.debounced = {});
(_c = config.touchTriggerEvents) !== null && _c !== void 0 ? _c : (config.touchTriggerEvents = { change: true, blur: true });
if (config.validate && !Array.isArray(config.validate))

@@ -1338,3 +1407,3 @@ config.validate = [config.validate];

let currentExtenders = [];
const { isSubmitting, data, errors, warnings, touched, isValid, isDirty, cleanup, start, } = createStores(adapters.storeFactory, config);
const { isSubmitting, isValidating, data, errors, warnings, touched, isValid, isDirty, cleanup, start, validateErrors, validateWarnings, } = createStores(adapters.storeFactory, config);
const originalUpdate = data.update;

@@ -1344,4 +1413,4 @@ const originalSet = data.set;

const transSet = (values) => originalSet(executeTransforms(values, config.transform));
const clonedData = Object.assign(Object.assign({}, data), { set: transSet, update: transUpdate });
data.update = transUpdate;
data.set = transSet;
const helpers = createHelpers({

@@ -1352,4 +1421,6 @@ extender,

addTransformer,
validateErrors,
validateWarnings,
stores: {
data: clonedData,
data,
errors,

@@ -1359,2 +1430,3 @@ warnings,

isValid,
isValidating,
isSubmitting,

@@ -1369,3 +1441,3 @@ isDirty,

touched,
data: clonedData,
data,
config,

@@ -1382,22 +1454,4 @@ addValidator,

};
function dataSetCustomizer(dataValue, initialValue) {
if (_isPlainObject(dataValue))
return;
return dataValue !== initialValue;
}
function dataSetTouchedCustomizer(dataValue, touchedValue) {
if (_isPlainObject(dataValue))
return;
return touchedValue || dataValue;
}
function newDataSet(values) {
touched.update((current) => {
const changed = _mergeWith(_cloneDeep(values), config.initialValues, dataSetCustomizer);
return _mergeWith(changed, current, dataSetTouchedCustomizer);
});
isDirty.set(true);
return clonedData.set(values);
}
const formActionConfig = Object.assign({ config, stores: {
data: clonedData,
data,
touched,

@@ -1407,2 +1461,3 @@ errors,

isSubmitting,
isValidating,
isValid,

@@ -1412,6 +1467,7 @@ isDirty,

addValidator }), extender,
validateErrors,
validateWarnings,
_getCurrentExtenders,
_setCurrentExtenders }, helpers.private);
const { form, createSubmitHandler, handleSubmit } = createFormAction(formActionConfig);
data.set = newDataSet;
return Object.assign({ data,

@@ -1423,2 +1479,3 @@ errors,

isSubmitting,
isValidating,
isDirty,

@@ -1448,3 +1505,2 @@ form,

exports.executeTransforms = executeTransforms;
exports.executeValidation = executeValidation;
exports.getFormControls = getFormControls;

@@ -1451,0 +1507,0 @@ exports.getFormDefaultValues = getFormDefaultValues;

import { __rest } from './external/.pnpm/tslib@2.3.1/external/tslib/tslib.es6.js';
import { get } from './get.js';
import { FelteSubmitError } from './error.js';
import { getAllValidators } from './get-validators.js';
import { deepSetTouched } from './deep-set-touched.js';
import { _merge } from './packages/common/dist/esm/utils/merge.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 { deepSome } from './packages/common/dist/esm/utils/deepSome.js';

@@ -58,3 +57,3 @@ import { isFormControl, isSelectElement, isInputElement, isElement } from './packages/common/dist/esm/utils/typeGuards.js';

}
function createFormAction({ helpers, stores, config, extender, _setFormNode, _getFormNode, _getInitialValues, _setCurrentExtenders, _getCurrentExtenders, }) {
function createFormAction({ helpers, stores, config, extender, validateErrors, validateWarnings, _setFormNode, _getFormNode, _getInitialValues, _setCurrentExtenders, _getCurrentExtenders, }) {
const { setFields, setTouched, reset, setInitialValues } = helpers;

@@ -64,7 +63,5 @@ const { addValidator, addTransformer, validate, setIsDirty, setIsSubmitting } = helpers, contextHelpers = __rest(helpers, ["addValidator", "addTransformer", "validate", "setIsDirty", "setIsSubmitting"]);

function createSubmitHandler(altConfig) {
var _a, _b, _c, _d;
const validate = (_a = altConfig === null || altConfig === void 0 ? void 0 : altConfig.validate) !== null && _a !== void 0 ? _a : getAllValidators('validate', config);
const warn = (_b = altConfig === null || altConfig === void 0 ? void 0 : altConfig.warn) !== null && _b !== void 0 ? _b : getAllValidators('warn', config);
const onError = (_c = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onError) !== null && _c !== void 0 ? _c : config.onError;
const onSuccess = (_d = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSuccess) !== null && _d !== void 0 ? _d : config.onSuccess;
var _a, _b;
const onError = (_a = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onError) !== null && _a !== void 0 ? _a : config.onError;
const onSuccess = (_b = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSuccess) !== null && _b !== void 0 ? _b : config.onSuccess;
return async function handleSubmit(event) {

@@ -79,12 +76,8 @@ var _a, _b;

const currentData = get(data);
const shape = deepSet(get(touched), []);
const currentErrors = await executeValidation(currentData, shape, validate);
const currentWarnings = await executeValidation(currentData, shape, warn);
const currentErrors = await validateErrors(currentData, altConfig === null || altConfig === void 0 ? void 0 : altConfig.validate);
const currentWarnings = await validateWarnings(currentData, altConfig === null || altConfig === void 0 ? void 0 : altConfig.warn);
if (currentWarnings)
warnings.set(_merge(deepSet(currentData, []), currentWarnings));
touched.update((t) => {
return deepSet(t, true);
});
touched.set(deepSetTouched(currentData, true));
if (currentErrors) {
errors.set(currentErrors);
const hasErrors = deepSome(currentErrors, (error) => Array.isArray(error) ? error.length >= 1 : !!error);

@@ -196,3 +189,2 @@ if (hasErrors) {

function handleInput(e) {
var _a;
const target = e.target;

@@ -208,4 +200,2 @@ if (!target ||

return;
if ((_a = config.touchTriggerEvents) === null || _a === void 0 ? void 0 : _a.input)
setTouched(getPath(target), true);
isDirty.set(true);

@@ -218,3 +208,2 @@ const inputValue = getInputTextOrNumber(target);

function handleChange(e) {
var _a;
const target = e.target;

@@ -225,4 +214,3 @@ if (!target || !isFormControl(target) || shouldIgnore(target))

return;
if ((_a = config.touchTriggerEvents) === null || _a === void 0 ? void 0 : _a.change)
setTouched(getPath(target), true);
setTouched(getPath(target), true);
if (isSelectElement(target) ||

@@ -247,3 +235,2 @@ ['checkbox', 'radio', 'file'].includes(target.type)) {

function handleBlur(e) {
var _a;
const target = e.target;

@@ -254,4 +241,3 @@ if (!target || !isFormControl(target) || shouldIgnore(target))

return;
if ((_a = config.touchTriggerEvents) === null || _a === void 0 ? void 0 : _a.blur)
setTouched(getPath(target), true);
setTouched(getPath(target), true);
}

@@ -258,0 +244,0 @@ const mutationOptions = { childList: true, subtree: true };

@@ -5,11 +5,7 @@ import { createHelpers } from './helpers.js';

import { executeTransforms } from './packages/common/dist/esm/utils/executeTransforms.js';
import { _mergeWith } from './packages/common/dist/esm/utils/mergeWith.js';
import { _cloneDeep } from './packages/common/dist/esm/utils/cloneDeep.js';
import { _isPlainObject } from './packages/common/dist/esm/utils/isPlainObject.js';
function createForm(config, adapters) {
var _a, _b, _c;
var _a, _b;
(_a = config.extend) !== null && _a !== void 0 ? _a : (config.extend = []);
(_b = config.debounced) !== null && _b !== void 0 ? _b : (config.debounced = {});
(_c = config.touchTriggerEvents) !== null && _c !== void 0 ? _c : (config.touchTriggerEvents = { change: true, blur: true });
if (config.validate && !Array.isArray(config.validate))

@@ -58,3 +54,3 @@ config.validate = [config.validate];

let currentExtenders = [];
const { isSubmitting, data, errors, warnings, touched, isValid, isDirty, cleanup, start, } = createStores(adapters.storeFactory, config);
const { isSubmitting, isValidating, data, errors, warnings, touched, isValid, isDirty, cleanup, start, validateErrors, validateWarnings, } = createStores(adapters.storeFactory, config);
const originalUpdate = data.update;

@@ -64,4 +60,4 @@ const originalSet = data.set;

const transSet = (values) => originalSet(executeTransforms(values, config.transform));
const clonedData = Object.assign(Object.assign({}, data), { set: transSet, update: transUpdate });
data.update = transUpdate;
data.set = transSet;
const helpers = createHelpers({

@@ -72,4 +68,6 @@ extender,

addTransformer,
validateErrors,
validateWarnings,
stores: {
data: clonedData,
data,
errors,

@@ -79,2 +77,3 @@ warnings,

isValid,
isValidating,
isSubmitting,

@@ -89,3 +88,3 @@ isDirty,

touched,
data: clonedData,
data,
config,

@@ -102,22 +101,4 @@ addValidator,

};
function dataSetCustomizer(dataValue, initialValue) {
if (_isPlainObject(dataValue))
return;
return dataValue !== initialValue;
}
function dataSetTouchedCustomizer(dataValue, touchedValue) {
if (_isPlainObject(dataValue))
return;
return touchedValue || dataValue;
}
function newDataSet(values) {
touched.update((current) => {
const changed = _mergeWith(_cloneDeep(values), config.initialValues, dataSetCustomizer);
return _mergeWith(changed, current, dataSetTouchedCustomizer);
});
isDirty.set(true);
return clonedData.set(values);
}
const formActionConfig = Object.assign({ config, stores: {
data: clonedData,
data,
touched,

@@ -127,2 +108,3 @@ errors,

isSubmitting,
isValidating,
isValid,

@@ -132,6 +114,7 @@ isDirty,

addValidator }), extender,
validateErrors,
validateWarnings,
_getCurrentExtenders,
_setCurrentExtenders }, helpers.private);
const { form, createSubmitHandler, handleSubmit } = createFormAction(formActionConfig);
data.set = newDataSet;
return Object.assign({ data,

@@ -143,2 +126,3 @@ errors,

isSubmitting,
isValidating,
isDirty,

@@ -145,0 +129,0 @@ form,

import { get } from './get.js';
import { getAllValidators } from './get-validators.js';
import { deepSetTouched } from './deep-set-touched.js';
import { _get } from './packages/common/dist/esm/utils/get.js';

@@ -10,3 +10,2 @@ import { _set } from './packages/common/dist/esm/utils/set.js';

import { _update } from './packages/common/dist/esm/utils/update.js';
import { executeValidation } from './packages/common/dist/esm/utils/executeValidation.js';
import { _cloneDeep } from './packages/common/dist/esm/utils/cloneDeep.js';

@@ -47,3 +46,3 @@

}
function createHelpers({ stores, config, }) {
function createHelpers({ stores, config, validateErrors, validateWarnings, }) {
var _a;

@@ -90,4 +89,8 @@ let formNode;

function addField(path, value, index) {
const errValue = _isPlainObject(value) ? deepSet(value, []) : [];
const touchedValue = _isPlainObject(value) ? deepSet(value, false) : false;
const touchedValue = _isPlainObject(value)
? deepSetTouched(value, false)
: false;
const errValue = _isPlainObject(touchedValue)
? deepSet(touchedValue, [])
: [];
errors.update(($errors) => {

@@ -110,8 +113,8 @@ return addAtIndex($errors, path, errValue, index);

const initialValue = _get(initialValues, path);
const errValue = _isPlainObject(initialValue)
? deepSet(initialValue, [])
: [];
const touchedValue = _isPlainObject(initialValue)
? deepSet(initialValue, false)
? deepSetTouched(initialValue, false)
: false;
const errValue = _isPlainObject(touchedValue)
? deepSet(touchedValue, [])
: [];
data.update(($data) => {

@@ -136,13 +139,6 @@ const newData = _set($data, path, initialValue);

async function validate() {
const validate = getAllValidators('validate', config);
const warn = getAllValidators('warn', config);
const currentData = get(data);
const shape = deepSet(get(touched), []);
setTouched((t) => {
return deepSet(t, true);
});
const currentErrors = await executeValidation(currentData, shape, validate);
const currentWarnings = await executeValidation(currentData, shape, warn);
warnings.set(currentWarnings || shape);
errors.set(currentErrors || shape);
touched.set(deepSetTouched(currentData, true));
const currentErrors = await validateErrors(currentData);
await validateWarnings(currentData);
return currentErrors;

@@ -149,0 +145,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 } from './packages/common/dist/esm/utils/executeValidation.js';
export { 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

@@ -54,8 +54,4 @@ import { _mergeWith } from './mergeWith.js';

}
async function executeValidation(values, shape, validations) {
const errors = await Promise.all(runValidations(values, validations));
return mergeErrors([shape, ...errors]);
}
export { executeValidation, mergeErrors, runValidations };
export { mergeErrors, runValidations };
//# sourceMappingURL=executeValidation.js.map

@@ -14,5 +14,8 @@ import { _cloneDeep } from './cloneDeep.js';

continue;
let rsValue = customizer(obj, source);
if (typeof rsValue !== 'undefined')
return rsValue;
const keys = Object.keys(source);
for (const key of keys) {
const rsValue = customizer(obj[key], source[key]);
rsValue = customizer(obj[key], source[key]);
if (typeof rsValue !== 'undefined') {

@@ -19,0 +22,0 @@ obj[key] = rsValue;

@@ -0,11 +1,12 @@

import { deepSetTouched } from './deep-set-touched.js';
import { executeTransforms } from './packages/common/dist/esm/utils/executeTransforms.js';
import { _cloneDeep } from './packages/common/dist/esm/utils/cloneDeep.js';
import { deepSome } from './packages/common/dist/esm/utils/deepSome.js';
import { deepSet } from './packages/common/dist/esm/utils/deepSet.js';
import { mergeErrors, runValidations } from './packages/common/dist/esm/utils/executeValidation.js';
import { deepSome } from './packages/common/dist/esm/utils/deepSome.js';
import { _mergeWith } from './packages/common/dist/esm/utils/mergeWith.js';
import { _isPlainObject } from './packages/common/dist/esm/utils/isPlainObject.js';
function createAbortController() {
const signal = { aborted: false };
function createValidationController(priority) {
const signal = { aborted: false, priority };
return {

@@ -18,5 +19,9 @@ signal,

}
function errorFilterer(errValue, touchValue) {
if (_isPlainObject(touchValue))
function errorFilterer(touchValue, errValue) {
if (_isPlainObject(touchValue)) {
if (_isPlainObject(errValue) && Object.keys(errValue).length === 0) {
return deepSet(touchValue, null);
}
return;
}
if (Array.isArray(touchValue)) {

@@ -39,5 +44,9 @@ if (touchValue.some(_isPlainObject))

}
function warningFilterer(errValue, touchValue) {
if (_isPlainObject(touchValue))
function warningFilterer(touchValue, errValue) {
if (_isPlainObject(touchValue)) {
if (_isPlainObject(errValue) && Object.keys(errValue).length === 0) {
return deepSet(touchValue, null);
}
return;
}
if (Array.isArray(touchValue)) {

@@ -61,6 +70,6 @@ if (touchValue.some(_isPlainObject))

function filterErrors([errors, touched]) {
return _mergeWith(errors, touched, errorFilterer);
return _mergeWith(touched, errors, errorFilterer);
}
function filterWarnings([errors, touched]) {
return _mergeWith(errors, touched, warningFilterer);
return _mergeWith(touched, errors, warningFilterer);
}

@@ -76,22 +85,4 @@ function debounce(func, timeout = 300) {

}
function cancellableValidation(store) {
let activeController;
return async function executeValidations($data, shape, validations) {
if (!validations || !$data)
return;
let current = shape !== null && shape !== void 0 ? shape : deepSet($data, []);
const controller = createAbortController();
if (activeController)
activeController.abort();
activeController = controller;
const results = runValidations($data, validations);
results.forEach(async (promise) => {
const result = await promise;
if (controller.signal.aborted)
return;
current = mergeErrors([current, result]);
store.set(current);
});
};
}
// A `derived` store factory that can defer subscription and be constructed
// with any store factory.
function createDerivedFactory(storeFactory) {

@@ -133,2 +124,50 @@ return function derived(storeOrStores, deriver, initialValue) {

: {};
const initialTouched = deepSetTouched(initialValues, false);
const touched = storeFactory(initialTouched);
const validationCount = storeFactory(0);
const [isValidating, startIsValidating, stopIsValidating] = derived([touched, validationCount], ([$touched, $validationCount]) => {
const isTouched = deepSome($touched, (t) => !!t);
return isTouched && $validationCount >= 1;
}, false);
// It is important not to destructure stores created with the factory
// since some stores may be callable.
delete isValidating.set;
delete isValidating.update;
function cancellableValidation(store) {
let activeController;
return async function executeValidations($data, shape, validations, priority = false) {
if (!validations || !$data)
return;
let current = shape && Object.keys(shape).length > 0
? shape
: deepSet($data, []);
// Keeping a controller allows us to cancel previous asynchronous
// validations if they've become stale.
const controller = createValidationController(priority);
// By assigning `priority` we can prevent specific validations
// from being aborted. Used when submitting the form or
// calling the `validate` helper.
if (!(activeController === null || activeController === void 0 ? void 0 : activeController.signal.priority) || priority) {
activeController === null || activeController === void 0 ? void 0 : activeController.abort();
activeController = controller;
}
// If the current controller has priority and we're not trying to
// override it, completely prevent validations
if (activeController.signal.priority && !priority)
return;
validationCount.update((c) => c + 1);
const results = runValidations($data, validations);
results.forEach(async (promise) => {
const result = await promise;
if (controller.signal.aborted)
return;
current = mergeErrors([current, result]);
store.set(current);
});
await Promise.all(results);
activeController = undefined;
validationCount.update((c) => c - 1);
return current;
};
}
let storesShape = deepSet(initialValues, []);

@@ -150,6 +189,7 @@ const data = storeFactory(initialValues);

], mergeErrors, _cloneDeep(initialWarnings));
const initialTouched = deepSet(initialValues, false);
const touched = storeFactory(initialTouched);
const [filteredErrors, startFilteredErrors, stopFilteredErrors] = derived([errors, touched], filterErrors, _cloneDeep(initialErrors));
const [filteredWarnings, startFilteredWarnings, stopFilteredWarnings,] = derived([warnings, touched], filterWarnings, _cloneDeep(initialWarnings));
// This is necessary since, on the first run, validations
// have not run yet. We assume the form is not valid in the first calling
// if there's validation functions assigned in the configuration.
let firstCalled = false;

@@ -172,4 +212,24 @@ const [isValid, startIsValid, stopIsValid] = derived(errors, ([$errors]) => {

const validateWarnings = cancellableValidation(immediateWarnings);
const validateDebouncedErrors = debounce(cancellableValidation(debouncedErrors), (_c = (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.validateTimeout) !== null && _c !== void 0 ? _c : (_d = config.debounced) === null || _d === void 0 ? void 0 : _d.timeout);
const validateDebouncedWarnings = debounce(cancellableValidation(debouncedWarnings), (_f = (_e = config.debounced) === null || _e === void 0 ? void 0 : _e.warnTimeout) !== null && _f !== void 0 ? _f : (_g = config.debounced) === null || _g === void 0 ? void 0 : _g.timeout);
const validateDebouncedErrors = cancellableValidation(debouncedErrors);
const validateDebouncedWarnings = cancellableValidation(debouncedWarnings);
const _validateDebouncedErrors = debounce(validateDebouncedErrors, (_c = (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.validateTimeout) !== null && _c !== void 0 ? _c : (_d = config.debounced) === null || _d === void 0 ? void 0 : _d.timeout);
const _validateDebouncedWarnings = debounce(validateDebouncedWarnings, (_f = (_e = config.debounced) === null || _e === void 0 ? void 0 : _e.warnTimeout) !== null && _f !== void 0 ? _f : (_g = config.debounced) === null || _g === void 0 ? void 0 : _g.timeout);
async function executeErrorsValidation(data, altValidate) {
var _a;
const errors = validateErrors(data, storesShape, altValidate !== null && altValidate !== void 0 ? altValidate : config.validate, true);
if (altValidate)
return errors;
const debouncedErrors = validateDebouncedErrors(data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate, true);
return mergeErrors(await Promise.all([errors, debouncedErrors]));
}
async function executeWarningsValidation(data, altWarn) {
var _a;
const warnings = validateWarnings(data, storesShape, altWarn !== null && altWarn !== void 0 ? altWarn : config.warn, true);
if (altWarn)
return warnings;
const debouncedWarnings = validateDebouncedWarnings(data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.warn, true);
return mergeErrors(await Promise.all([warnings, debouncedWarnings]));
}
let errorsValue = initialErrors;
let warningsValue = initialWarnings;
function start() {

@@ -180,10 +240,14 @@ const dataUnsubscriber = data.subscribe(($data) => {

validateWarnings($data, storesShape, config.warn);
debouncedErrors.set({});
validateDebouncedErrors($data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate);
debouncedWarnings.set({});
validateDebouncedWarnings($data, storesShape, (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.warn);
_validateDebouncedErrors($data, storesShape, (_a = config.debounced) === null || _a === void 0 ? void 0 : _a.validate);
_validateDebouncedWarnings($data, storesShape, (_b = config.debounced) === null || _b === void 0 ? void 0 : _b.warn);
});
touched.subscribe(($touched) => {
const unsubscribeTouched = touched.subscribe(($touched) => {
storesShape = deepSet($touched, []);
});
const unsubscribeErrors = errors.subscribe(($errors) => {
errorsValue = $errors;
});
const unsubscribeWarnings = warnings.subscribe(($warnings) => {
warningsValue = $warnings;
});
startErrors();

@@ -194,2 +258,3 @@ startIsValid();

startFilteredWarnings();
startIsValidating();
function cleanup() {

@@ -202,11 +267,27 @@ dataUnsubscriber();

stopIsValid();
stopIsValidating();
unsubscribeTouched();
unsubscribeErrors();
unsubscribeWarnings();
}
return cleanup;
}
filteredErrors.set = immediateErrors.set;
filteredErrors.update =
immediateErrors.update;
filteredWarnings.set = immediateWarnings.set;
filteredWarnings.update =
immediateWarnings.update;
function publicErrorsUpdater(updater) {
immediateErrors.set(updater(errorsValue));
debouncedErrors.set({});
}
function publicWarningsUpdater(updater) {
immediateWarnings.set(updater(warningsValue));
debouncedWarnings.set({});
}
function publicErrorsSetter(value) {
publicErrorsUpdater(() => value);
}
function publicWarningsSetter(value) {
publicWarningsUpdater(() => value);
}
filteredErrors.set = publicErrorsSetter;
filteredErrors.update = publicErrorsUpdater;
filteredWarnings.set = publicWarningsSetter;
filteredWarnings.update = publicWarningsUpdater;
return {

@@ -220,2 +301,5 @@ data,

isDirty,
isValidating,
validateErrors: executeErrorsValidation,
validateWarnings: executeWarningsValidation,
cleanup: config.preventStoreStart ? () => undefined : start(),

@@ -222,0 +306,0 @@ start,

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

import { Extender, Obj, Stores, FormConfig, TransformFunction, ExtenderHandler, CreateSubmitHandlerConfig, AddValidatorFn, Helpers } from '@felte/common';
import { Extender, Obj, Stores, FormConfig, TransformFunction, ExtenderHandler, CreateSubmitHandlerConfig, Errors, AddValidatorFn, Helpers, ValidationFunction } from '@felte/common';
type FormActionConfig<Data extends Obj> = {

@@ -6,2 +6,4 @@ stores: Stores<Data>;

extender: Extender<Data>[];
validateErrors(data: Data, altValidate?: ValidationFunction<Data> | ValidationFunction<Data>[]): Promise<Errors<Data> | undefined>;
validateWarnings(data: Data, altWarn?: ValidationFunction<Data> | ValidationFunction<Data>[]): Promise<Errors<Data> | undefined>;
helpers: Helpers<Data, string> & {

@@ -17,3 +19,3 @@ addValidator: AddValidatorFn<Data>;

};
declare function createFormAction<Data extends Obj>({ helpers, stores, config, extender, _setFormNode, _getFormNode, _getInitialValues, _setCurrentExtenders, _getCurrentExtenders, }: FormActionConfig<Data>): {
declare function createFormAction<Data extends Obj>({ helpers, stores, config, extender, validateErrors, validateWarnings, _setFormNode, _getFormNode, _getInitialValues, _setCurrentExtenders, _getCurrentExtenders, }: FormActionConfig<Data>): {
form: (node: HTMLFormElement) => {

@@ -20,0 +22,0 @@ destroy(): void;

@@ -1,5 +0,7 @@

import { Extender, FormConfig, Obj, Stores, ValidationFunction, TransformFunction, Helpers } from '@felte/common';
import { Errors, Extender, FormConfig, Obj, Stores, ValidationFunction, TransformFunction, Helpers } from '@felte/common';
type CreateHelpersOptions<Data extends Obj> = {
config: FormConfig<Data>;
stores: Stores<Data>;
validateErrors(data: Data): Promise<Errors<Data> | undefined>;
validateWarnings(data: Data): Promise<Errors<Data> | undefined>;
extender: Extender<Data>[];

@@ -9,3 +11,3 @@ addValidator(validator: ValidationFunction<Data>): void;

};
declare function createHelpers<Data extends Obj>({ stores, config, }: CreateHelpersOptions<Data>): {
declare function createHelpers<Data extends Obj>({ stores, config, validateErrors, validateWarnings, }: CreateHelpersOptions<Data>): {
public: Helpers<Data, string>;

@@ -12,0 +14,0 @@ private: {

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

import { StoreFactory, Obj, FormConfig, Touched, PartialWritableErrors } from '@felte/common';
import { StoreFactory, Obj, FormConfig, Errors, Touched, ValidationFunction, PartialWritableErrors } from '@felte/common';
import { Writable, Readable } from 'svelte/store';

@@ -24,2 +24,8 @@ type Readables = Readable<any> | [Readable<any>, ...Array<Readable<any>>] | Array<Readable<any>>;

isDirty: Writable<boolean> & StoreExt;
isValidating: Readable<boolean> & {
update?: ((updater: (v: boolean) => boolean) => void) | undefined;
set?: ((v: boolean) => void) | undefined;
} & StoreExt;
validateErrors: (data: Data, altValidate?: ValidationFunction<Data> | ValidationFunction<Data>[] | undefined) => Promise<Errors<Data> | undefined>;
validateWarnings: (data: Data, altWarn?: ValidationFunction<Data> | ValidationFunction<Data>[] | undefined) => Promise<Errors<Data> | undefined>;
cleanup: () => void;

@@ -26,0 +32,0 @@ start: () => () => void;

{
"name": "@felte/core",
"version": "1.0.0-next.17",
"version": "1.0.0-next.18",
"description": "Core package for FelteJS",

@@ -27,3 +27,3 @@ "main": "dist/cjs/index.js",

"dependencies": {
"@felte/common": "1.0.0-next.14"
"@felte/common": "1.0.0-next.15"
},

@@ -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

Sorry, the diff of this file is not supported yet

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