@katachi-forms/core
Advanced tools
Comparing version 0.0.1 to 0.0.2
import { AnyZodObject } from 'zod'; | ||
import { z } from 'zod'; | ||
import { ZodEffects } from 'zod'; | ||
import { ZodTypeAny } from 'zod'; | ||
@@ -23,2 +24,6 @@ | ||
constructor(name: string, schema: ZodTypeAny, configuration?: FieldConfiguration<FieldValueType>); | ||
/** | ||
* Set up the automatic validation | ||
* @protected | ||
*/ | ||
protected initialiseAutomaticValidation(): ((() => boolean) & { | ||
@@ -81,2 +86,10 @@ cancel: () => void; | ||
/** | ||
* Used to update the initial value of the field, and optionally the | ||
* current value. Useful for when initial values are fetched async, | ||
* and you want to detect it the form has been changed | ||
* @param value | ||
* @param overrideCurrentValue | ||
*/ | ||
setInitialValue(value: FieldValueType, overrideCurrentValue?: boolean): void; | ||
/** | ||
* Sets the current value of the field | ||
@@ -106,2 +119,7 @@ * @param value - The value to set | ||
/** | ||
* Adds multiple errors to the field | ||
* @param errors | ||
*/ | ||
addErrors(errors: string[]): void; | ||
/** | ||
* Sets the error of the field | ||
@@ -142,2 +160,7 @@ * This will overwrite any existing errors | ||
/** | ||
* Add multiple warnings to the field | ||
* @param warnings | ||
*/ | ||
addWarnings(warnings: string[]): void; | ||
/** | ||
* Sets the warning of the field | ||
@@ -174,2 +197,6 @@ * This will overwrite any existing warnings | ||
export declare class FieldAlreadyExistsError extends KatachiError { | ||
constructor(name: string); | ||
} | ||
export declare type FieldConfiguration<ValueType> = { | ||
@@ -184,3 +211,7 @@ initialValue?: ValueType; | ||
export declare class Form<FormSchema extends AnyZodObject> { | ||
export declare class FieldNotFoundError extends KatachiError { | ||
constructor(name: string); | ||
} | ||
export declare class Form<FormSchema extends AnyZodObject | ZodEffects<AnyZodObject>> { | ||
protected schema: FormSchema; | ||
@@ -193,32 +224,137 @@ protected defaultFieldConfiguration: Omit<FieldConfiguration<z.infer<ZodTypeAny>>, "initialValue">; | ||
constructor(schema: FormSchema, configuration?: FormConfiguration<FormSchema>); | ||
protected getSchemaShape(): any; | ||
/** | ||
* Prepare a single field for use | ||
* @param name | ||
* @protected | ||
*/ | ||
protected initialiseField(name: keyof z.infer<typeof Form.schema> | string): Field<z.TypeOf<FormSchema>[keyof z.TypeOf<FormSchema>]>; | ||
/** | ||
* Prepare all fields on the schema for use and store them on the form | ||
* @protected | ||
*/ | ||
protected initialiseFields(): { [key in keyof z.TypeOf<FormSchema>]: Field<z.TypeOf<FormSchema>[key]>; }; | ||
/** | ||
* Ensure the field exists on the form, or throws a FieldNotFound error | ||
* @param name | ||
* @throws FieldNotFoundError | ||
* @protected | ||
*/ | ||
protected assertFieldExists(name: keyof z.infer<typeof Form.schema> | string): void; | ||
/** | ||
* Return all fields as an object where the key is the name of the field | ||
*/ | ||
getFields(): { [key in keyof z.TypeOf<FormSchema>]: Field<z.TypeOf<FormSchema>[key]>; }; | ||
/** | ||
* Get a single field by name | ||
* @param name | ||
*/ | ||
getField(name: keyof z.infer<typeof Form.schema> | string): { | ||
[key in keyof z.TypeOf<FormSchema>]: Field<z.TypeOf<FormSchema>[key]>; | ||
}[keyof z.TypeOf<FormSchema>]; | ||
/** | ||
* Dynamically register fields on the form (not on the schema), should be used | ||
* cautiously. | ||
* @param name | ||
* @param schema | ||
* @param configuration | ||
*/ | ||
registerField<FieldSchema extends ZodTypeAny>(name: string, schema: FieldSchema, configuration?: FieldConfiguration<z.infer<typeof schema>>): void; | ||
/** | ||
* Get the full schema of the form | ||
*/ | ||
getSchema(): FormSchema; | ||
/** | ||
* Get the current configuration of the form | ||
*/ | ||
getConfiguration(): BaseFormConfiguration<FormSchema>; | ||
getValue(name: keyof z.infer<FormSchema>): z.TypeOf<FormSchema>[keyof z.TypeOf<FormSchema>] | null; | ||
setValue(name: keyof z.infer<FormSchema>, value: z.infer<FormSchema>[keyof z.infer<FormSchema>]): void; | ||
/** | ||
* Get the value of a field by name | ||
* @param name | ||
*/ | ||
getValue(name: keyof z.infer<FormSchema> | string): z.TypeOf<FormSchema>[keyof z.TypeOf<FormSchema>] | null; | ||
/** | ||
* Get the value of all fields | ||
*/ | ||
getValues(): z.infer<FormSchema>; | ||
/** | ||
* Set the value of a field by name | ||
* @param name | ||
* @param value | ||
*/ | ||
setValue(name: keyof z.infer<FormSchema> | string, value: z.infer<FormSchema>[keyof z.infer<FormSchema>]): void; | ||
/** | ||
* Set the initial values of all fields, optionally updating the current | ||
* values | ||
* @param values | ||
* @param overrideCurrentValues | ||
*/ | ||
setInitialValues(values: Partial<{ | ||
[key in keyof z.infer<FormSchema> | string]: z.infer<FormSchema>[key]; | ||
}>, overrideCurrentValues?: boolean): void; | ||
/** | ||
* Set the values of all fields | ||
* @param values | ||
*/ | ||
setValues(values: Partial<{ | ||
[key in keyof z.infer<FormSchema>]: z.infer<FormSchema>[key]; | ||
[key in keyof z.infer<FormSchema> | string]: z.infer<FormSchema>[key]; | ||
}>): void; | ||
getErrors(): { [key in keyof z.TypeOf<FormSchema>]?: string[] | undefined; }; | ||
/** | ||
* Retrieve all errors as an object where the key is the name of the field | ||
*/ | ||
getErrors(name?: keyof z.infer<FormSchema> | string): string[] | { [key in string | keyof z.TypeOf<FormSchema>]?: string[] | undefined; }; | ||
/** | ||
* Add an error or multiple errors to fields on the form | ||
* @param errors | ||
*/ | ||
addErrors(errors: Partial<{ | ||
[key in keyof z.infer<FormSchema>]: string; | ||
[key in keyof z.infer<FormSchema> | string]: string | string[]; | ||
}>): void; | ||
/** | ||
* Override the errors of fields on the form | ||
* @param errors | ||
*/ | ||
setErrors(errors: Partial<{ | ||
[key in keyof z.infer<FormSchema>]: string | string[]; | ||
[key in keyof z.infer<FormSchema> | string]: string | string[]; | ||
}>): void; | ||
/** | ||
* Clear all current errors for all fields, or if specified all supplied fields | ||
* @param mode | ||
* @param applyToFields | ||
*/ | ||
clearErrors(mode?: "include" | "exclude", applyToFields?: (keyof z.infer<FormSchema> | string)[]): void; | ||
/** | ||
* Check if any fields have errors | ||
*/ | ||
hasError(): boolean; | ||
getWarnings(): { [key in keyof z.TypeOf<FormSchema>]?: string[] | undefined; }; | ||
/** | ||
* Get all warnings as an object where the key is the name of the field | ||
*/ | ||
getWarnings(name?: keyof z.infer<FormSchema> | string): string[] | { [key in keyof z.TypeOf<FormSchema>]?: string[] | undefined; }; | ||
/** | ||
* Add a warning to fields on the form | ||
* @param warnings | ||
*/ | ||
addWarnings(warnings: Partial<{ | ||
[key in keyof z.infer<FormSchema>]: string; | ||
[key in keyof z.infer<FormSchema> | string]: string | string[]; | ||
}>): void; | ||
/** | ||
* Override the warnings of fields on the form | ||
* @param warnings | ||
*/ | ||
setWarnings(warnings: Partial<{ | ||
[key in keyof z.infer<FormSchema>]: string | string[]; | ||
[key in keyof z.infer<FormSchema> | string]: string | string[]; | ||
}>): void; | ||
/** | ||
* Check if any fields have warnings | ||
*/ | ||
hasWarning(): boolean; | ||
/** | ||
* Check if any fields have been modified | ||
*/ | ||
isModified(): boolean; | ||
protected applyEffectValidations(): boolean; | ||
/** | ||
* Validate all fields | ||
*/ | ||
validate(): boolean; | ||
@@ -229,2 +365,14 @@ } | ||
export declare const isFieldAlreadyExistsError: (err: any) => any; | ||
export declare const isFieldNotFoundError: (err: any) => any; | ||
export declare const isGenericKatachiError: (err: any) => any; | ||
export declare class KatachiError extends Error { | ||
protected katachiError: boolean; | ||
constructor(message: string, errorType?: string); | ||
isKatachiError(): boolean; | ||
} | ||
declare type LifecycleHooks<ValueType> = { | ||
@@ -231,0 +379,0 @@ beforeValueChanged: (newValue: ValueType | null, previousValue: ValueType | null) => void; |
@@ -1,42 +0,42 @@ | ||
var y = Object.defineProperty; | ||
var p = (n, e, t) => e in n ? y(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t; | ||
var r = (n, e, t) => (p(n, typeof e != "symbol" ? e + "" : e, t), t); | ||
import { z as w } from "zod"; | ||
function f(n, e, t = {}) { | ||
let i = null, s = null, a = null, l = null, u; | ||
const { leading: h = !1, trailing: E = !0 } = t, d = function(...o) { | ||
s = o, a = this; | ||
const c = (/* @__PURE__ */ new Date()).getTime(), m = g(c); | ||
return m && i === null && h && (l = c, u = n.apply(a, s), s = a = null), i !== null && clearTimeout(i), (i === null || m) && (i = setTimeout(V, e)), u; | ||
var j = Object.defineProperty; | ||
var W = (s, t, e) => t in s ? j(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e; | ||
var n = (s, t, e) => (W(s, typeof t != "symbol" ? t + "" : t, e), e); | ||
import { z as C } from "zod"; | ||
function g(s, t, e = {}) { | ||
let i = null, r = null, a = null, l = null, o; | ||
const { leading: c = !1, trailing: A = !0 } = e, m = function(...u) { | ||
r = u, a = this; | ||
const d = (/* @__PURE__ */ new Date()).getTime(), b = y(d); | ||
return b && i === null && c && (l = d, o = s.apply(a, r), r = a = null), i !== null && clearTimeout(i), (i === null || b) && (i = setTimeout(F, t)), o; | ||
}; | ||
function g(o) { | ||
const c = l !== null ? o - l : Number.MAX_VALUE; | ||
return l === null || c >= e; | ||
function y(u) { | ||
const d = l !== null ? u - l : Number.MAX_VALUE; | ||
return l === null || d >= t; | ||
} | ||
function V() { | ||
const o = (/* @__PURE__ */ new Date()).getTime(); | ||
if (g(o)) | ||
return b(o); | ||
i = setTimeout(V, e); | ||
function F() { | ||
const u = (/* @__PURE__ */ new Date()).getTime(); | ||
if (y(u)) | ||
return O(u); | ||
i = setTimeout(F, t); | ||
} | ||
function b(o) { | ||
return i = null, l = o, E && s && (u = n.apply(a, s), s = a = null), u; | ||
function O(u) { | ||
return i = null, l = u, A && r && (o = s.apply(a, r), r = a = null), o; | ||
} | ||
return d.cancel = function() { | ||
i !== null && clearTimeout(i), l = null, a = null, s = null, i = null; | ||
}, d; | ||
return m.cancel = function() { | ||
i !== null && clearTimeout(i), l = null, a = null, r = null, i = null; | ||
}, m; | ||
} | ||
class C { | ||
constructor(e, t, i = {}) { | ||
r(this, "name"); | ||
r(this, "schema"); | ||
r(this, "configuration"); | ||
r(this, "currentValue"); | ||
r(this, "errors"); | ||
r(this, "warnings"); | ||
r(this, "lifecycleHooks"); | ||
r(this, "automaticValidation"); | ||
var s, a, l, u, h; | ||
this.name = e, this.schema = t, this.configuration = i, this.currentValue = this.configuration.initialValue ?? null, this.errors = this.configuration.initialErrors ?? [], this.warnings = this.configuration.initialWarnings ?? [], this.lifecycleHooks = { | ||
beforeValueChanged: ((s = this.configuration.lifecycle) == null ? void 0 : s.beforeValueChanged) ?? (() => { | ||
class p { | ||
constructor(t, e, i = {}) { | ||
n(this, "name"); | ||
n(this, "schema"); | ||
n(this, "configuration"); | ||
n(this, "currentValue"); | ||
n(this, "errors"); | ||
n(this, "warnings"); | ||
n(this, "lifecycleHooks"); | ||
n(this, "automaticValidation"); | ||
var r, a, l, o, c; | ||
this.name = t, this.schema = e, this.configuration = i, this.currentValue = this.configuration.initialValue ?? null, this.errors = this.configuration.initialErrors ?? [], this.warnings = this.configuration.initialWarnings ?? [], this.lifecycleHooks = { | ||
beforeValueChanged: ((r = this.configuration.lifecycle) == null ? void 0 : r.beforeValueChanged) ?? (() => { | ||
}), | ||
@@ -47,8 +47,12 @@ onValueChanged: ((a = this.configuration.lifecycle) == null ? void 0 : a.onValueChanged) ?? (() => { | ||
}), | ||
beforeValidated: ((u = this.configuration.lifecycle) == null ? void 0 : u.beforeValidated) ?? (() => { | ||
beforeValidated: ((o = this.configuration.lifecycle) == null ? void 0 : o.beforeValidated) ?? (() => { | ||
}), | ||
onValidated: ((h = this.configuration.lifecycle) == null ? void 0 : h.onValidated) ?? (() => { | ||
onValidated: ((c = this.configuration.lifecycle) == null ? void 0 : c.onValidated) ?? (() => { | ||
}) | ||
}, this.automaticValidation = this.initialiseAutomaticValidation(); | ||
} | ||
/** | ||
* Set up the automatic validation | ||
* @protected | ||
*/ | ||
initialiseAutomaticValidation() { | ||
@@ -59,7 +63,7 @@ switch (this.configuration.validationMode) { | ||
case "custom": | ||
return f(this.validate, this.configuration.validationDelay ?? 0); | ||
return g(this.validate, this.configuration.validationDelay ?? 0); | ||
case "eager": | ||
return f(this.validate, 0); | ||
return g(this.validate, 0); | ||
case "relaxed": | ||
return f(this.validate, 300); | ||
return g(this.validate, 300); | ||
default: | ||
@@ -76,4 +80,4 @@ return null; | ||
*/ | ||
beforeValueChanged(e, t) { | ||
this.lifecycleHooks.beforeValueChanged(e, t); | ||
beforeValueChanged(t, e) { | ||
this.lifecycleHooks.beforeValueChanged(t, e); | ||
} | ||
@@ -87,4 +91,4 @@ /** | ||
*/ | ||
onValueChanged(e, t) { | ||
this.automaticValidation && this.automaticValidation(), this.lifecycleHooks.onValueChanged(e, t); | ||
onValueChanged(t, e) { | ||
this.automaticValidation && this.automaticValidation(), this.lifecycleHooks.onValueChanged(t, e); | ||
} | ||
@@ -96,4 +100,4 @@ /** | ||
*/ | ||
onValueAccessed(e) { | ||
this.lifecycleHooks.onValueAccessed(e); | ||
onValueAccessed(t) { | ||
this.lifecycleHooks.onValueAccessed(t); | ||
} | ||
@@ -106,4 +110,4 @@ /** | ||
*/ | ||
beforeValidated(e) { | ||
this.lifecycleHooks.beforeValidated(e); | ||
beforeValidated(t) { | ||
this.lifecycleHooks.beforeValidated(t); | ||
} | ||
@@ -117,4 +121,4 @@ /** | ||
*/ | ||
onValidated(e, t) { | ||
this.lifecycleHooks.onValidated(e, t); | ||
onValidated(t, e) { | ||
this.lifecycleHooks.onValidated(t, e); | ||
} | ||
@@ -146,8 +150,18 @@ /** | ||
/** | ||
* Used to update the initial value of the field, and optionally the | ||
* current value. Useful for when initial values are fetched async, | ||
* and you want to detect it the form has been changed | ||
* @param value | ||
* @param overrideCurrentValue | ||
*/ | ||
setInitialValue(t, e = !0) { | ||
this.configuration.initialValue = t, e && this.setCurrentValue(t); | ||
} | ||
/** | ||
* Sets the current value of the field | ||
* @param value - The value to set | ||
*/ | ||
setCurrentValue(e) { | ||
const t = this.currentValue; | ||
this.beforeValueChanged(e, t), this.currentValue = e, this.onValueChanged(e, t); | ||
setCurrentValue(t) { | ||
const e = this.currentValue; | ||
this.beforeValueChanged(t, e), this.currentValue = t, this.onValueChanged(t, e); | ||
} | ||
@@ -180,6 +194,13 @@ /** | ||
*/ | ||
addError(e) { | ||
this.errors.push(e); | ||
addError(t) { | ||
this.errors.push(t); | ||
} | ||
/** | ||
* Adds multiple errors to the field | ||
* @param errors | ||
*/ | ||
addErrors(t) { | ||
this.errors.push(...t); | ||
} | ||
/** | ||
* Sets the error of the field | ||
@@ -189,4 +210,4 @@ * This will overwrite any existing errors | ||
*/ | ||
setError(e) { | ||
this.errors = [e]; | ||
setError(t) { | ||
this.errors = [t]; | ||
} | ||
@@ -198,4 +219,4 @@ /** | ||
*/ | ||
setErrors(e) { | ||
this.errors = e; | ||
setErrors(t) { | ||
this.errors = t; | ||
} | ||
@@ -214,3 +235,3 @@ /** | ||
resetErrorsToInitial() { | ||
this.errors = this.configuration.initialErrors ?? []; | ||
this.setErrors(this.configuration.initialErrors ?? []); | ||
} | ||
@@ -233,6 +254,13 @@ /** | ||
*/ | ||
addWarning(e) { | ||
this.warnings.push(e); | ||
addWarning(t) { | ||
this.warnings.push(t); | ||
} | ||
/** | ||
* Add multiple warnings to the field | ||
* @param warnings | ||
*/ | ||
addWarnings(t) { | ||
this.warnings.push(...t); | ||
} | ||
/** | ||
* Sets the warning of the field | ||
@@ -242,4 +270,4 @@ * This will overwrite any existing warnings | ||
*/ | ||
setWarning(e) { | ||
this.warnings = [e]; | ||
setWarning(t) { | ||
this.warnings = [t]; | ||
} | ||
@@ -251,4 +279,4 @@ /** | ||
*/ | ||
setWarnings(e) { | ||
this.warnings = e; | ||
setWarnings(t) { | ||
this.warnings = t; | ||
} | ||
@@ -267,3 +295,3 @@ /** | ||
resetWarningsToInitial() { | ||
this.warnings = this.configuration.initialWarnings ?? []; | ||
this.setWarnings(this.configuration.initialWarnings ?? []); | ||
} | ||
@@ -282,5 +310,5 @@ /** | ||
try { | ||
return this.schema.parse(this.currentValue), this.clearErrors(), !0; | ||
} catch (e) { | ||
return e instanceof w.ZodError ? this.errors = e.issues.map((t) => t.message) : e instanceof Error ? this.errors = [e.message] : this.errors = ["An unknown error occurred"], !1; | ||
return this.hasError() && this.clearErrors(), this.schema.parse(this.currentValue), !0; | ||
} catch (t) { | ||
return t instanceof C.ZodError ? this.errors = t.issues.map((e) => e.message) : t instanceof Error ? this.errors = [t.message] : this.errors = ["An unknown error occurred"], !1; | ||
} finally { | ||
@@ -291,104 +319,341 @@ this.onValidated(this.errors, this.warnings); | ||
} | ||
class j { | ||
constructor(e, t = {}) { | ||
r(this, "schema"); | ||
r(this, "defaultFieldConfiguration"); | ||
r(this, "configuration"); | ||
r(this, "fields"); | ||
this.schema = e, this.defaultFieldConfiguration = t.defaultConfig ?? {}, this.configuration = t, this.fields = this.initialiseFields(); | ||
const f = (s, t) => { | ||
Object.values(s).forEach((e) => { | ||
t(e); | ||
}); | ||
}; | ||
function E(s) { | ||
return s._def.typeName === "ZodEffects"; | ||
} | ||
class V extends Error { | ||
constructor(e, i = "Generic") { | ||
super(e); | ||
n(this, "katachiError", !0); | ||
this.name = `Katachi${i}Error`; | ||
} | ||
initialiseField(e) { | ||
const t = this.schema.shape[e]; | ||
return new C(e, t, { | ||
isKatachiError() { | ||
return this.katachiError; | ||
} | ||
} | ||
class x extends V { | ||
constructor(t) { | ||
super(`Field '${t}' not found on form`, "FieldNotFound"); | ||
} | ||
} | ||
class w extends V { | ||
constructor(t) { | ||
super(`Field '${t}' already exists on form`, "FieldAlreadyExists"); | ||
} | ||
} | ||
const h = (s, t, e, i) => { | ||
t[s] ? e(t[s]) : i && i(); | ||
}; | ||
class S { | ||
constructor(t, e = {}) { | ||
n(this, "schema"); | ||
n(this, "defaultFieldConfiguration"); | ||
n(this, "configuration"); | ||
n(this, "fields"); | ||
this.schema = t, this.defaultFieldConfiguration = e.defaultConfig ?? {}, this.configuration = e, this.fields = this.initialiseFields(); | ||
} | ||
getSchemaShape() { | ||
return E(this.schema) ? this.schema._def.schema.shape : this.schema.shape; | ||
} | ||
/** | ||
* Prepare a single field for use | ||
* @param name | ||
* @protected | ||
*/ | ||
initialiseField(t) { | ||
const e = this.getSchemaShape()[t]; | ||
return new p(t, e, { | ||
...this.defaultFieldConfiguration, | ||
...this.configuration[e] | ||
...this.configuration[t] | ||
}); | ||
} | ||
/** | ||
* Prepare all fields on the schema for use and store them on the form | ||
* @protected | ||
*/ | ||
initialiseFields() { | ||
let e = {}; | ||
return Object.keys(this.schema.shape).forEach((t) => { | ||
e[t] = this.initialiseField(t); | ||
}), e; | ||
let t = {}; | ||
return Object.keys(this.getSchemaShape()).forEach((e) => { | ||
t[e] = this.initialiseField(e); | ||
}), t; | ||
} | ||
/** | ||
* Ensure the field exists on the form, or throws a FieldNotFound error | ||
* @param name | ||
* @throws FieldNotFoundError | ||
* @protected | ||
*/ | ||
assertFieldExists(t) { | ||
if (!this.fields[t]) | ||
throw new x(t); | ||
} | ||
/** | ||
* Return all fields as an object where the key is the name of the field | ||
*/ | ||
getFields() { | ||
return this.fields; | ||
} | ||
getField(e) { | ||
return this.fields[e]; | ||
/** | ||
* Get a single field by name | ||
* @param name | ||
*/ | ||
getField(t) { | ||
return this.assertFieldExists(t), this.fields[t]; | ||
} | ||
registerField(e, t, i = {}) { | ||
const s = this.schema.extend({ | ||
[e]: t | ||
}); | ||
this.schema = s, this.fields[e] = new C( | ||
/** | ||
* Dynamically register fields on the form (not on the schema), should be used | ||
* cautiously. | ||
* @param name | ||
* @param schema | ||
* @param configuration | ||
*/ | ||
registerField(t, e, i = {}) { | ||
if (this.fields[t]) | ||
throw new w(t); | ||
let r; | ||
E(this.schema) ? r = this.schema._def.schema.extend({ | ||
[t]: e | ||
}) : r = this.schema.extend({ | ||
[t]: e | ||
}), this.schema = r, this.fields[t] = new p( | ||
t, | ||
e, | ||
t, | ||
i | ||
); | ||
} | ||
/** | ||
* Get the full schema of the form | ||
*/ | ||
getSchema() { | ||
return this.schema; | ||
} | ||
/** | ||
* Get the current configuration of the form | ||
*/ | ||
getConfiguration() { | ||
return this.configuration; | ||
} | ||
getValue(e) { | ||
return this.fields[e].getCurrentValue(); | ||
/** | ||
* Get the value of a field by name | ||
* @param name | ||
*/ | ||
getValue(t) { | ||
return this.assertFieldExists(t), this.fields[t].getCurrentValue(); | ||
} | ||
setValue(e, t) { | ||
this.fields[e].setCurrentValue(t); | ||
/** | ||
* Get the value of all fields | ||
*/ | ||
getValues() { | ||
return Object.fromEntries( | ||
Object.entries(this.fields).map(([t, e]) => [ | ||
t, | ||
e.getCurrentValue() | ||
]) | ||
); | ||
} | ||
setValues(e) { | ||
Object.entries(e).forEach(([t, i]) => { | ||
i && this.fields[t].setCurrentValue(i); | ||
/** | ||
* Set the value of a field by name | ||
* @param name | ||
* @param value | ||
*/ | ||
setValue(t, e) { | ||
this.assertFieldExists(t), this.fields[t].setCurrentValue(e); | ||
} | ||
/** | ||
* Set the initial values of all fields, optionally updating the current | ||
* values | ||
* @param values | ||
* @param overrideCurrentValues | ||
*/ | ||
setInitialValues(t, e = !0) { | ||
Object.entries(t).forEach(([i, r]) => { | ||
r && h(i, this.fields, (a) => { | ||
a.setInitialValue(r, e); | ||
}); | ||
}); | ||
} | ||
getErrors() { | ||
/** | ||
* Set the values of all fields | ||
* @param values | ||
*/ | ||
setValues(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
i && h(e, this.fields, (r) => { | ||
r.setCurrentValue(i); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Retrieve all errors as an object where the key is the name of the field | ||
*/ | ||
getErrors(t) { | ||
if (t) | ||
return this.assertFieldExists(t), this.fields[t].getErrors(); | ||
let e = {}; | ||
return Object.entries(this.fields).forEach(([t, i]) => { | ||
e[t] = i.getErrors(); | ||
return Object.entries(this.fields).forEach(([i, r]) => { | ||
e[i] = r.getErrors(); | ||
}), e; | ||
} | ||
addErrors(e) { | ||
Object.entries(e).forEach(([t, i]) => { | ||
!i || !i.length || this.fields[t].addError(i); | ||
/** | ||
* Add an error or multiple errors to fields on the form | ||
* @param errors | ||
*/ | ||
addErrors(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
!i || !i.length || h(e, this.fields, (r) => { | ||
if (Array.isArray(i)) { | ||
r.addErrors(i); | ||
return; | ||
} | ||
r.addError(i); | ||
}); | ||
}); | ||
} | ||
setErrors(e) { | ||
Object.entries(e).forEach(([t, i]) => { | ||
!i || !i.length || (typeof i == "string" && (i = [i]), this.fields[t].setErrors(i)); | ||
/** | ||
* Override the errors of fields on the form | ||
* @param errors | ||
*/ | ||
setErrors(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
!i || !i.length || h(e, this.fields, (r) => { | ||
if (Array.isArray(i)) { | ||
r.setErrors(i); | ||
return; | ||
} | ||
r.setError(i); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Clear all current errors for all fields, or if specified all supplied fields | ||
* @param mode | ||
* @param applyToFields | ||
*/ | ||
clearErrors(t, e) { | ||
if (!t) { | ||
f(this.fields, (i) => { | ||
i.clearErrors(); | ||
}); | ||
return; | ||
} | ||
if (e) | ||
switch (t) { | ||
case "include": | ||
f(this.fields, (i) => { | ||
e.includes(i.getName()) && i.clearErrors(); | ||
}); | ||
return; | ||
case "exclude": | ||
f(this.fields, (i) => { | ||
e.includes(i.getName()) || i.clearErrors(); | ||
}); | ||
return; | ||
} | ||
} | ||
/** | ||
* Check if any fields have errors | ||
*/ | ||
hasError() { | ||
return Object.values(this.fields).some((e) => e.hasError()); | ||
return Object.values(this.fields).some((t) => t.hasError()); | ||
} | ||
getWarnings() { | ||
/** | ||
* Get all warnings as an object where the key is the name of the field | ||
*/ | ||
getWarnings(t) { | ||
if (t) | ||
return this.assertFieldExists(t), this.fields[t].getWarnings(); | ||
let e = {}; | ||
return Object.entries(this.fields).forEach(([t, i]) => { | ||
e[t] = i.getWarnings(); | ||
return Object.entries(this.fields).forEach(([i, r]) => { | ||
e[i] = r.getWarnings(); | ||
}), e; | ||
} | ||
addWarnings(e) { | ||
Object.entries(e).forEach(([t, i]) => { | ||
!i || !i.length || this.fields[t].addWarning(i); | ||
/** | ||
* Add a warning to fields on the form | ||
* @param warnings | ||
*/ | ||
addWarnings(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
!i || !i.length || h(e, this.fields, (r) => { | ||
if (typeof i == "string") { | ||
r.addWarning(i); | ||
return; | ||
} | ||
r.addWarnings(i); | ||
}); | ||
}); | ||
} | ||
setWarnings(e) { | ||
Object.entries(e).forEach(([t, i]) => { | ||
!i || !i.length || (typeof i == "string" && (i = [i]), this.fields[t].setWarnings(i)); | ||
/** | ||
* Override the warnings of fields on the form | ||
* @param warnings | ||
*/ | ||
setWarnings(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
!i || !i.length || h(e, this.fields, (r) => { | ||
if (typeof i == "string") { | ||
r.setWarning(i); | ||
return; | ||
} | ||
r.setWarnings(i); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Check if any fields have warnings | ||
*/ | ||
hasWarning() { | ||
return Object.values(this.fields).some((e) => e.hasWarning()); | ||
return Object.values(this.fields).some((t) => t.hasWarning()); | ||
} | ||
/** | ||
* Check if any fields have been modified | ||
*/ | ||
isModified() { | ||
return Object.values(this.fields).some((t) => t.isModified()); | ||
} | ||
applyEffectValidations() { | ||
try { | ||
return this.schema.parse(this.getValues()), !0; | ||
} catch (t) { | ||
let e = !0; | ||
return t instanceof C.ZodError ? (Object.entries(t.formErrors.fieldErrors).forEach( | ||
([i, r]) => { | ||
const a = this.fields[i].getErrors(), l = (r == null ? void 0 : r.filter((o) => !a.includes(o))) ?? []; | ||
l.length > 0 && (this.fields[i].addErrors(l), e = !1); | ||
} | ||
), e) : (t instanceof Error, !1); | ||
} | ||
} | ||
/** | ||
* Validate all fields | ||
*/ | ||
validate() { | ||
let e = !0; | ||
return Object.values(this.fields).forEach((t) => { | ||
t.validate() || (e = !1); | ||
}), e; | ||
this.clearErrors(); | ||
let t = !0; | ||
return f(this.fields, (e) => { | ||
e.validate() || (t = !1); | ||
}), E(this.schema) && (t = this.applyEffectValidations()), t; | ||
} | ||
} | ||
const K = (s) => { | ||
var t; | ||
return ((t = s == null ? void 0 : s.isKatachiError) == null ? void 0 : t.call(s)) || (s == null ? void 0 : s.name) === "KatachiGenericError" || s instanceof V; | ||
}, I = (s) => { | ||
var t; | ||
return ((t = s == null ? void 0 : s.isKatachiError) == null ? void 0 : t.call(s)) && ((s == null ? void 0 : s.name) === "KatachiFieldAlreadyExistsError" || s instanceof w); | ||
}, N = (s) => { | ||
var t; | ||
return ((t = s == null ? void 0 : s.isKatachiError) == null ? void 0 : t.call(s)) && ((s == null ? void 0 : s.name) === "KatachiFieldNotFoundError" || s instanceof x); | ||
}; | ||
export { | ||
C as Field, | ||
j as Form | ||
p as Field, | ||
w as FieldAlreadyExistsError, | ||
x as FieldNotFoundError, | ||
S as Form, | ||
V as KatachiError, | ||
I as isFieldAlreadyExistsError, | ||
N as isFieldNotFoundError, | ||
K as isGenericKatachiError | ||
}; |
{ | ||
"name": "@katachi-forms/core", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
47742
1077