{"version":3,"file":"required.CmhJkffP.js","sources":["../../../../../../node_modules/svelte-forms/form.js","../../../../../../node_modules/is-promise/index.mjs","../../../../../../node_modules/svelte-forms/types.js","../../../../../../node_modules/svelte-forms/createFieldStore.js","../../../../../../node_modules/svelte-forms/field.js","../../../../../../node_modules/svelte-forms/validators/required.js"],"sourcesContent":["import { derived, get } from 'svelte/store';\r\nexport function form(...fields) {\r\n let names = [];\r\n let doubles = [];\r\n fields.forEach((field) => {\r\n const obj = get(field);\r\n if (names.includes(obj.name)) {\r\n doubles = doubles.includes(obj.name) ? doubles : [...doubles, obj.name];\r\n }\r\n else {\r\n names = [...names, obj.name];\r\n }\r\n });\r\n if (doubles.length) {\r\n throw new Error(`Cannot have the fields with the same name: ${doubles.join(', ')}`);\r\n }\r\n const store = derived(fields, (values) => {\r\n return {\r\n valid: values.every((value) => value.valid),\r\n dirty: values.some((value) => value.dirty),\r\n // Summary as a getter to avoid useless computation of data\r\n // if no one wants it\r\n get summary() {\r\n return values.reduce((carry, f) => {\r\n carry[f.name] = f.value;\r\n return carry;\r\n }, {});\r\n },\r\n errors: values\r\n .map((value) => {\r\n return value.errors.map((e) => {\r\n if (e.includes('.')) {\r\n return e;\r\n }\r\n return `${value.name}.${e}`;\r\n });\r\n })\r\n .flat()\r\n .filter((value, index, self) => self.indexOf(value) === index),\r\n hasError(name) {\r\n return this.errors.findIndex((e) => e === name) !== -1;\r\n }\r\n };\r\n });\r\n const { subscribe } = store;\r\n function reset() {\r\n fields.forEach((field) => field.reset && field.reset());\r\n }\r\n function clear() {\r\n fields.forEach((field) => field.clear && field.clear());\r\n }\r\n async function validate() {\r\n for (const field of fields) {\r\n if (field.validate)\r\n await field.validate();\r\n }\r\n }\r\n function getField(name) {\r\n return fields.find((f) => get(f).name === name);\r\n }\r\n function summary() {\r\n return get(store).summary;\r\n }\r\n return { subscribe, reset, validate, getField, summary, clear };\r\n}\r\n","export default function isPromise(obj) {\n return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';\n}\n","export const defaultFieldOptions = {\r\n valid: true,\r\n checkOnInit: false,\r\n validateOnChange: true,\r\n stopAtFirstError: false\r\n};\r\nexport function isField(field) {\r\n const keys = Object.keys(field);\r\n return ['name', 'value', 'valid', 'invalid', 'errors'].every((key) => keys.includes(key));\r\n}\r\n","import isPromise from 'is-promise';\r\nimport { writable, get } from 'svelte/store';\r\nimport { isField } from './types.js';\r\nexport function createFieldOject(name, value, errors = [], partialField = {}) {\r\n const field = {\r\n name,\r\n value,\r\n valid: true,\r\n invalid: false,\r\n errors: [],\r\n dirty: false\r\n };\r\n return processField(field, errors, partialField);\r\n}\r\nexport function getValue(value) {\r\n const isStore = function (v) {\r\n return value.subscribe !== undefined;\r\n };\r\n const isField = function (v) {\r\n return !!value.name && value.valid !== undefined;\r\n };\r\n if (isStore(value)) {\r\n return get(value).value;\r\n }\r\n else if (isField(value)) {\r\n return value.value;\r\n }\r\n return value;\r\n}\r\nexport async function getErrors(value, validators, stopAtFirstError = false) {\r\n const v = getValue(value);\r\n let errors = [];\r\n for (const validator of validators) {\r\n let result = validator(v);\r\n if (isPromise(result)) {\r\n result = await result;\r\n }\r\n if (stopAtFirstError && !result.valid) {\r\n errors = [result];\r\n break;\r\n }\r\n errors = [...errors, result];\r\n }\r\n return errors;\r\n}\r\nexport function processField(field, validations, partialField = {}) {\r\n if (validations) {\r\n const errors = validations.filter((v) => !v.valid).map((v) => v.name);\r\n const valid = !errors.length;\r\n return { ...field, valid: valid, invalid: !valid, errors, ...partialField };\r\n // return { ...field, dirty: field.dirty || !!validations.length, valid, invalid: !valid, errors, ...partialField };\r\n }\r\n return field;\r\n}\r\nexport function createFieldStore(name, v, validators = [], options) {\r\n const value = {\r\n name,\r\n value: v,\r\n valid: options.valid,\r\n invalid: !options.valid,\r\n dirty: false,\r\n errors: []\r\n };\r\n const store = writable(value);\r\n const { subscribe, update, set: _set } = store;\r\n async function set(field, forceValidation = false) {\r\n if (!isField(field)) {\r\n field = processField(get(store), [], { value: field });\r\n }\r\n if (forceValidation || options.validateOnChange) {\r\n let validations = await getErrors(field, validators, options.stopAtFirstError);\r\n _set(processField(field, validations, { dirty: true }));\r\n }\r\n else {\r\n _set(processField(field, null, { dirty: true }));\r\n }\r\n }\r\n async function validate() {\r\n const errors = await getErrors(store, validators, options.stopAtFirstError);\r\n let obj;\r\n update((field) => {\r\n obj = processField(field, errors, { dirty: false });\r\n return obj;\r\n });\r\n return obj;\r\n }\r\n function reset() {\r\n _set(processField({\r\n dirty: false,\r\n errors: [],\r\n name,\r\n valid: options.valid,\r\n invalid: !options.valid,\r\n value: v\r\n }));\r\n }\r\n if (options.checkOnInit) {\r\n set(value);\r\n }\r\n function clear() {\r\n _set(processField({\r\n dirty: false,\r\n errors: [],\r\n name,\r\n valid: options.valid,\r\n invalid: !options.valid,\r\n value: null\r\n }));\r\n }\r\n return { subscribe, update, set, validate, reset, clear };\r\n}\r\n","import { createFieldStore } from './createFieldStore.js';\r\nimport { defaultFieldOptions } from './types.js';\r\nexport function field(name, value, validators = [], options = {}) {\r\n return createFieldStore(name, value, validators, { ...defaultFieldOptions, ...options });\r\n}\r\n","export function required() {\r\n return (val) => {\r\n let valid = true;\r\n if (val === undefined || val === null)\r\n valid = false;\r\n if (typeof val === 'string') {\r\n const tmp = val.replace(/\\s/g, '');\r\n valid = tmp.length > 0;\r\n }\r\n return { valid, name: 'required' };\r\n };\r\n}\r\n"],"names":["form","fields","names","doubles","field","obj","get","store","derived","values","value","carry","f","e","index","self","name","subscribe","reset","clear","validate","getField","summary","isPromise","defaultFieldOptions","isField","keys","key","getValue","isStore","v","getErrors","validators","stopAtFirstError","errors","validator","result","processField","validations","partialField","valid","createFieldStore","options","writable","update","_set","set","forceValidation","required","val"],"mappings":"sFACO,SAASA,KAAQC,EAAQ,CAC5B,IAAIC,EAAQ,CAAA,EACRC,EAAU,CAAA,EAUd,GATAF,EAAO,QAASG,GAAU,CACtB,MAAMC,EAAMC,EAAIF,CAAK,EACjBF,E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