@katachi-forms/core
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -6,5 +6,3 @@ import { AnyZodObject } from 'zod'; | ||
declare type BaseFormConfiguration<FormSchema extends ZodTypeAny> = { | ||
[key in keyof z.infer<FormSchema>]?: FieldConfiguration<z.infer<FormSchema>[key]>; | ||
}; | ||
export declare type BaseFormConfiguration<FormSchema extends ZodAliasFormSchema> = FieldDrivenConfiguration<FormSchema> & PropertyDrivenConfiguration<FormSchema>; | ||
@@ -111,3 +109,6 @@ declare type DefaultFieldConfiguration = { | ||
*/ | ||
getErrors(): string[]; | ||
getErrors(options?: { | ||
limit?: number; | ||
asString?: boolean; | ||
}): string | string[]; | ||
/** | ||
@@ -208,2 +209,6 @@ * Adds an error to the field | ||
export declare type FieldDrivenConfiguration<FormSchema extends ZodAliasFormSchema> = { | ||
[key in keyof z.infer<FormSchema>]?: FieldConfiguration<z.infer<FormSchema>[key]>; | ||
}; | ||
export declare class FieldNotFoundError extends KatachiError { | ||
@@ -222,2 +227,3 @@ constructor(name: string); | ||
protected getSchemaShape(): any; | ||
protected buildFieldConfiguration<T extends keyof z.infer<typeof Form.schema>>(name: T | string): FieldConfiguration<z.infer<FormSchema>[T]>; | ||
/** | ||
@@ -233,3 +239,3 @@ * Prepare a single field for use | ||
*/ | ||
protected initialiseFields(): { [key in keyof z.TypeOf<FormSchema>]: Field<z.TypeOf<FormSchema>[key]>; }; | ||
protected initialiseFields(): typeof Form.fields; | ||
/** | ||
@@ -303,3 +309,6 @@ * Ensure the field exists on the form, or throws a FieldNotFound error | ||
*/ | ||
getErrors(name?: keyof z.infer<FormSchema> | string): string[] | { [key in string | keyof z.TypeOf<FormSchema>]?: string[] | undefined; }; | ||
getErrors(name?: keyof z.infer<FormSchema> | string, options?: { | ||
limit?: number; | ||
asString?: boolean; | ||
}): string | string[] | { [key in string | keyof z.TypeOf<FormSchema>]?: string[] | undefined; }; | ||
/** | ||
@@ -326,5 +335,6 @@ * Add an error or multiple errors to fields on the form | ||
/** | ||
* Check if any fields have errors | ||
* Check if any fields have errors or a particular field if given a name | ||
* @param name field name to check | ||
*/ | ||
hasError(): boolean; | ||
hasError(name?: keyof z.infer<FormSchema> | string): boolean; | ||
/** | ||
@@ -349,5 +359,6 @@ * Get all warnings as an object where the key is the name of the field | ||
/** | ||
* Check if any fields have warnings | ||
* Check if any fields have warnings or a particular field if given a name | ||
* @param name field name to check | ||
*/ | ||
hasWarning(): boolean; | ||
hasWarning(name?: keyof z.infer<FormSchema> | string): boolean; | ||
/** | ||
@@ -364,3 +375,3 @@ * Check if any fields have been modified | ||
export declare type FormConfiguration<FormSchema extends ZodTypeAny> = BaseFormConfiguration<FormSchema> & DefaultFieldConfiguration; | ||
export declare type FormConfiguration<FormSchema extends ZodAliasFormSchema> = BaseFormConfiguration<FormSchema> & DefaultFieldConfiguration; | ||
@@ -379,3 +390,3 @@ export declare const isFieldAlreadyExistsError: (err: any) => any; | ||
declare type LifecycleHooks<ValueType> = { | ||
export declare type LifecycleHooks<ValueType> = { | ||
beforeValueChanged: (newValue: ValueType | null, previousValue: ValueType | null) => void; | ||
@@ -388,7 +399,30 @@ onValueChanged: (newValue: ValueType | null, previousValue: ValueType | null) => void; | ||
export declare type PropertyDrivenConfiguration<FormSchema extends ZodAliasFormSchema> = { | ||
initialValues?: { | ||
[key in keyof z.infer<FormSchema>]?: z.infer<FormSchema>[key]; | ||
}; | ||
initialErrors?: { | ||
[key in keyof z.infer<FormSchema>]?: string[]; | ||
}; | ||
initialWarnings?: { | ||
[key in keyof z.infer<FormSchema>]?: string[]; | ||
}; | ||
lifecycles?: { | ||
[key in keyof z.infer<FormSchema>]?: Partial<LifecycleHooks<z.infer<FormSchema>[key]>>; | ||
}; | ||
validationModes?: { | ||
[key in keyof z.infer<FormSchema>]?: ValidationMode; | ||
}; | ||
validationDelays?: { | ||
[key in keyof z.infer<FormSchema>]?: number; | ||
}; | ||
}; | ||
/** | ||
* These behave as presets for the delay on the debounce function. | ||
*/ | ||
declare type ValidationMode = "eager" | "relaxed" | "custom" | "manual"; | ||
export declare type ValidationMode = "eager" | "relaxed" | "custom" | "manual"; | ||
declare type ZodAliasFormSchema = AnyZodObject | ZodEffects<AnyZodObject>; | ||
export { } |
@@ -1,46 +0,62 @@ | ||
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; | ||
var v = Object.defineProperty; | ||
var W = (s, i, t) => i in s ? v(s, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[i] = t; | ||
var l = (s, i, t) => (W(s, typeof i != "symbol" ? i + "" : i, t), t); | ||
import { ZodString as S, ZodNumber as k, ZodBoolean as K, ZodArray as N, z as j } from "zod"; | ||
function V(s, i, t = {}) { | ||
let e = null, r = null, n = null, a = null, o; | ||
const { leading: c = !1, trailing: d = !0 } = t, f = function(...u) { | ||
r = u, n = this; | ||
const g = (/* @__PURE__ */ new Date()).getTime(), p = F(g); | ||
return p && e === null && c && (a = g, o = s.apply(n, r), r = n = null), e !== null && clearTimeout(e), (e === null || p) && (e = setTimeout(b, i)), o; | ||
}; | ||
function y(u) { | ||
const d = l !== null ? u - l : Number.MAX_VALUE; | ||
return l === null || d >= t; | ||
function F(u) { | ||
const g = a !== null ? u - a : Number.MAX_VALUE; | ||
return a === null || g >= i; | ||
} | ||
function F() { | ||
function b() { | ||
const u = (/* @__PURE__ */ new Date()).getTime(); | ||
if (y(u)) | ||
return O(u); | ||
i = setTimeout(F, t); | ||
if (F(u)) | ||
return A(u); | ||
e = setTimeout(b, i); | ||
} | ||
function O(u) { | ||
return i = null, l = u, A && r && (o = s.apply(a, r), r = a = null), o; | ||
function A(u) { | ||
return e = null, a = u, d && r && (o = s.apply(n, r), r = n = null), o; | ||
} | ||
return m.cancel = function() { | ||
i !== null && clearTimeout(i), l = null, a = null, r = null, i = null; | ||
}, m; | ||
return f.cancel = function() { | ||
e !== null && clearTimeout(e), a = null, n = null, r = null, e = null; | ||
}, f; | ||
} | ||
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 = { | ||
const C = (s) => { | ||
if (s instanceof S) | ||
return ""; | ||
if (s instanceof k) | ||
return 0; | ||
if (s instanceof K) | ||
return !1; | ||
if (s instanceof N) | ||
return []; | ||
}; | ||
class x { | ||
constructor(i, t, e = {}) { | ||
l(this, "name"); | ||
l(this, "schema"); | ||
l(this, "configuration"); | ||
l(this, "currentValue"); | ||
l(this, "errors"); | ||
l(this, "warnings"); | ||
l(this, "lifecycleHooks"); | ||
l(this, "automaticValidation"); | ||
var r, n, a, o, c; | ||
this.name = i, this.schema = t, this.configuration = { | ||
validationMode: "eager", | ||
initialValue: C(t), | ||
initialWarnings: [], | ||
initialErrors: [], | ||
...e | ||
}, 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) ?? (() => { | ||
}), | ||
onValueChanged: ((a = this.configuration.lifecycle) == null ? void 0 : a.onValueChanged) ?? (() => { | ||
onValueChanged: ((n = this.configuration.lifecycle) == null ? void 0 : n.onValueChanged) ?? (() => { | ||
}), | ||
onValueAccessed: ((l = this.configuration.lifecycle) == null ? void 0 : l.onValueAccessed) ?? (() => { | ||
onValueAccessed: ((a = this.configuration.lifecycle) == null ? void 0 : a.onValueAccessed) ?? (() => { | ||
}), | ||
@@ -62,7 +78,7 @@ beforeValidated: ((o = this.configuration.lifecycle) == null ? void 0 : o.beforeValidated) ?? (() => { | ||
case "custom": | ||
return g(this.validate, this.configuration.validationDelay ?? 0); | ||
return V(this.validate, this.configuration.validationDelay ?? 0); | ||
case "eager": | ||
return g(this.validate, 0); | ||
return V(this.validate, 0); | ||
case "relaxed": | ||
return g(this.validate, 300); | ||
return V(this.validate, 300); | ||
default: | ||
@@ -79,4 +95,4 @@ return null; | ||
*/ | ||
beforeValueChanged(t, e) { | ||
this.lifecycleHooks.beforeValueChanged(t, e); | ||
beforeValueChanged(i, t) { | ||
this.lifecycleHooks.beforeValueChanged(i, t); | ||
} | ||
@@ -90,4 +106,4 @@ /** | ||
*/ | ||
onValueChanged(t, e) { | ||
this.automaticValidation && this.automaticValidation(), this.lifecycleHooks.onValueChanged(t, e); | ||
onValueChanged(i, t) { | ||
this.automaticValidation && this.automaticValidation(), this.lifecycleHooks.onValueChanged(i, t); | ||
} | ||
@@ -99,4 +115,4 @@ /** | ||
*/ | ||
onValueAccessed(t) { | ||
this.lifecycleHooks.onValueAccessed(t); | ||
onValueAccessed(i) { | ||
this.lifecycleHooks.onValueAccessed(i); | ||
} | ||
@@ -109,4 +125,4 @@ /** | ||
*/ | ||
beforeValidated(t) { | ||
this.lifecycleHooks.beforeValidated(t); | ||
beforeValidated(i) { | ||
this.lifecycleHooks.beforeValidated(i); | ||
} | ||
@@ -120,4 +136,4 @@ /** | ||
*/ | ||
onValidated(t, e) { | ||
this.lifecycleHooks.onValidated(t, e); | ||
onValidated(i, t) { | ||
this.lifecycleHooks.onValidated(i, t); | ||
} | ||
@@ -155,4 +171,4 @@ /** | ||
*/ | ||
setInitialValue(t, e = !0) { | ||
this.configuration.initialValue = t, e && this.setCurrentValue(t); | ||
setInitialValue(i, t = !0) { | ||
this.configuration.initialValue = i, t && this.setCurrentValue(i); | ||
} | ||
@@ -163,5 +179,5 @@ /** | ||
*/ | ||
setCurrentValue(t) { | ||
const e = this.currentValue; | ||
this.beforeValueChanged(t, e), this.currentValue = t, this.onValueChanged(t, e); | ||
setCurrentValue(i) { | ||
const t = this.currentValue; | ||
this.beforeValueChanged(i, t), this.currentValue = i, this.onValueChanged(i, t); | ||
} | ||
@@ -172,3 +188,3 @@ /** | ||
clearCurrentValue() { | ||
this.setCurrentValue(null); | ||
this.setCurrentValue(C(this.schema) ?? null); | ||
} | ||
@@ -188,4 +204,5 @@ /** | ||
*/ | ||
getErrors() { | ||
return this.errors; | ||
getErrors(i = {}) { | ||
const { limit: t, asString: e } = i; | ||
return t ? e ? this.errors.slice(0, t).join(", ") : this.errors.slice(0, t) : e ? this.errors.join(", ") : this.errors; | ||
} | ||
@@ -196,4 +213,4 @@ /** | ||
*/ | ||
addError(t) { | ||
this.errors.push(t); | ||
addError(i) { | ||
this.errors.push(i); | ||
} | ||
@@ -204,4 +221,4 @@ /** | ||
*/ | ||
addErrors(t) { | ||
this.errors.push(...t); | ||
addErrors(i) { | ||
this.errors.push(...i); | ||
} | ||
@@ -213,4 +230,4 @@ /** | ||
*/ | ||
setError(t) { | ||
this.errors = [t]; | ||
setError(i) { | ||
this.errors = [i]; | ||
} | ||
@@ -222,4 +239,4 @@ /** | ||
*/ | ||
setErrors(t) { | ||
this.errors = t; | ||
setErrors(i) { | ||
this.errors = i; | ||
} | ||
@@ -256,4 +273,4 @@ /** | ||
*/ | ||
addWarning(t) { | ||
this.warnings.push(t); | ||
addWarning(i) { | ||
this.warnings.push(i); | ||
} | ||
@@ -264,4 +281,4 @@ /** | ||
*/ | ||
addWarnings(t) { | ||
this.warnings.push(...t); | ||
addWarnings(i) { | ||
this.warnings.push(...i); | ||
} | ||
@@ -273,4 +290,4 @@ /** | ||
*/ | ||
setWarning(t) { | ||
this.warnings = [t]; | ||
setWarning(i) { | ||
this.warnings = [i]; | ||
} | ||
@@ -282,4 +299,4 @@ /** | ||
*/ | ||
setWarnings(t) { | ||
this.warnings = t; | ||
setWarnings(i) { | ||
this.warnings = i; | ||
} | ||
@@ -313,4 +330,4 @@ /** | ||
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; | ||
} catch (i) { | ||
return i instanceof j.ZodError ? this.errors = i.issues.map((t) => t.message) : i instanceof Error ? this.errors = [i.message] : this.errors = ["An unknown error occurred"], !1; | ||
} finally { | ||
@@ -321,15 +338,15 @@ this.onValidated(this.errors, this.warnings); | ||
} | ||
const f = (s, t) => { | ||
Object.values(s).forEach((e) => { | ||
t(e); | ||
const E = (s, i) => { | ||
Object.values(s).forEach((t) => { | ||
i(t); | ||
}); | ||
}; | ||
function E(s) { | ||
function m(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`; | ||
class y extends Error { | ||
constructor(t, e = "Generic") { | ||
super(t); | ||
l(this, "katachiError", !0); | ||
this.name = `Katachi${e}Error`; | ||
} | ||
@@ -340,26 +357,49 @@ isKatachiError() { | ||
} | ||
class x extends V { | ||
constructor(t) { | ||
super(`Field '${t}' not found on form`, "FieldNotFound"); | ||
class O extends y { | ||
constructor(i) { | ||
super(`Field '${i}' not found on form`, "FieldNotFound"); | ||
} | ||
} | ||
class w extends V { | ||
constructor(t) { | ||
super(`Field '${t}' already exists on form`, "FieldAlreadyExists"); | ||
class w extends y { | ||
constructor(i) { | ||
super(`Field '${i}' 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(); | ||
const h = (s, i, t, e) => { | ||
i[s] ? t(i[s]) : e && e(); | ||
}, I = (s) => Object.fromEntries( | ||
Object.entries(s).filter(([, i]) => i !== void 0) | ||
); | ||
class M { | ||
constructor(i, t = {}) { | ||
l(this, "schema"); | ||
l(this, "defaultFieldConfiguration"); | ||
l(this, "configuration"); | ||
l(this, "fields"); | ||
this.schema = i, this.defaultFieldConfiguration = t.defaultConfig ?? {}, this.configuration = t, this.fields = this.initialiseFields(); | ||
} | ||
getSchemaShape() { | ||
return E(this.schema) ? this.schema._def.schema.shape : this.schema.shape; | ||
return m(this.schema) ? this.schema._def.schema.shape : this.schema.shape; | ||
} | ||
buildFieldConfiguration(i) { | ||
var n, a, o, c, d, f; | ||
const t = this.configuration[i], e = { | ||
initialValue: (n = this.configuration.initialValues) == null ? void 0 : n[i], | ||
initialErrors: (a = this.configuration.initialErrors) == null ? void 0 : a[i], | ||
initialWarnings: (o = this.configuration.initialWarnings) == null ? void 0 : o[i], | ||
validationMode: (c = this.configuration.validationModes) == null ? void 0 : c[i], | ||
validationDelay: (d = this.configuration.validationDelays) == null ? void 0 : d[i], | ||
lifecycle: (f = this.configuration.lifecycles) == null ? void 0 : f[i] | ||
}, r = { | ||
...this.defaultFieldConfiguration.lifecycle, | ||
...e.lifecycle, | ||
...t == null ? void 0 : t.lifecycle | ||
}; | ||
return { | ||
...this.defaultFieldConfiguration, | ||
...I(e), | ||
...t, | ||
...Object.keys(r).length > 0 ? { lifecycle: r } : {} | ||
}; | ||
} | ||
/** | ||
@@ -370,8 +410,9 @@ * Prepare a single field for use | ||
*/ | ||
initialiseField(t) { | ||
const e = this.getSchemaShape()[t]; | ||
return new p(t, e, { | ||
...this.defaultFieldConfiguration, | ||
...this.configuration[t] | ||
}); | ||
initialiseField(i) { | ||
const t = this.getSchemaShape()[i]; | ||
return new x( | ||
i, | ||
t, | ||
this.buildFieldConfiguration(i) | ||
); | ||
} | ||
@@ -383,6 +424,8 @@ /** | ||
initialiseFields() { | ||
let t = {}; | ||
return Object.keys(this.getSchemaShape()).forEach((e) => { | ||
t[e] = this.initialiseField(e); | ||
}), t; | ||
return Object.fromEntries( | ||
Object.entries(this.getSchemaShape()).map(([i]) => [ | ||
i, | ||
this.initialiseField(i) | ||
]) | ||
); | ||
} | ||
@@ -395,5 +438,5 @@ /** | ||
*/ | ||
assertFieldExists(t) { | ||
if (!this.fields[t]) | ||
throw new x(t); | ||
assertFieldExists(i) { | ||
if (!this.fields[i]) | ||
throw new O(i); | ||
} | ||
@@ -410,4 +453,4 @@ /** | ||
*/ | ||
getField(t) { | ||
return this.assertFieldExists(t), this.fields[t]; | ||
getField(i) { | ||
return this.assertFieldExists(i), this.fields[i]; | ||
} | ||
@@ -421,14 +464,14 @@ /** | ||
*/ | ||
registerField(t, e, i = {}) { | ||
if (this.fields[t]) | ||
throw new w(t); | ||
registerField(i, t, e = {}) { | ||
if (this.fields[i]) | ||
throw new w(i); | ||
let r; | ||
E(this.schema) ? r = this.schema._def.schema.extend({ | ||
[t]: e | ||
m(this.schema) ? r = this.schema._def.schema.extend({ | ||
[i]: t | ||
}) : r = this.schema.extend({ | ||
[t]: e | ||
}), this.schema = r, this.fields[t] = new p( | ||
[i]: t | ||
}), this.schema = r, this.fields[i] = new x( | ||
i, | ||
t, | ||
e, | ||
i | ||
e | ||
); | ||
@@ -452,4 +495,4 @@ } | ||
*/ | ||
getValue(t) { | ||
return this.assertFieldExists(t), this.fields[t].getCurrentValue(); | ||
getValue(i) { | ||
return this.assertFieldExists(i), this.fields[i].getCurrentValue(); | ||
} | ||
@@ -461,5 +504,5 @@ /** | ||
return Object.fromEntries( | ||
Object.entries(this.fields).map(([t, e]) => [ | ||
t, | ||
e.getCurrentValue() | ||
Object.entries(this.fields).map(([i, t]) => [ | ||
i, | ||
t.getCurrentValue() | ||
]) | ||
@@ -473,4 +516,4 @@ ); | ||
*/ | ||
setValue(t, e) { | ||
this.assertFieldExists(t), this.fields[t].setCurrentValue(e); | ||
setValue(i, t) { | ||
this.assertFieldExists(i), this.fields[i].setCurrentValue(t); | ||
} | ||
@@ -483,6 +526,6 @@ /** | ||
*/ | ||
setInitialValues(t, e = !0) { | ||
Object.entries(t).forEach(([i, r]) => { | ||
r && h(i, this.fields, (a) => { | ||
a.setInitialValue(r, e); | ||
setInitialValues(i, t = !0) { | ||
Object.entries(i).forEach(([e, r]) => { | ||
r && h(e, this.fields, (n) => { | ||
n.setInitialValue(r, t); | ||
}); | ||
@@ -495,6 +538,6 @@ }); | ||
*/ | ||
setValues(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
i && h(e, this.fields, (r) => { | ||
r.setCurrentValue(i); | ||
setValues(i) { | ||
Object.entries(i).forEach(([t, e]) => { | ||
e && h(t, this.fields, (r) => { | ||
r.setCurrentValue(e); | ||
}); | ||
@@ -506,8 +549,8 @@ }); | ||
*/ | ||
getErrors(t) { | ||
if (t) | ||
return this.assertFieldExists(t), this.fields[t].getErrors(); | ||
getErrors(i, t) { | ||
if (i) | ||
return this.assertFieldExists(i), this.fields[i].getErrors(t); | ||
let e = {}; | ||
return Object.entries(this.fields).forEach(([i, r]) => { | ||
e[i] = r.getErrors(); | ||
return Object.entries(this.fields).forEach(([r, n]) => { | ||
e[r] = n.getErrors(); | ||
}), e; | ||
@@ -519,10 +562,10 @@ } | ||
*/ | ||
addErrors(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
!i || !i.length || h(e, this.fields, (r) => { | ||
if (Array.isArray(i)) { | ||
r.addErrors(i); | ||
addErrors(i) { | ||
Object.entries(i).forEach(([t, e]) => { | ||
!e || !e.length || h(t, this.fields, (r) => { | ||
if (Array.isArray(e)) { | ||
r.addErrors(e); | ||
return; | ||
} | ||
r.addError(i); | ||
r.addError(e); | ||
}); | ||
@@ -535,10 +578,10 @@ }); | ||
*/ | ||
setErrors(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
!i || !i.length || h(e, this.fields, (r) => { | ||
if (Array.isArray(i)) { | ||
r.setErrors(i); | ||
setErrors(i) { | ||
Object.entries(i).forEach(([t, e]) => { | ||
!e || !e.length || h(t, this.fields, (r) => { | ||
if (Array.isArray(e)) { | ||
r.setErrors(e); | ||
return; | ||
} | ||
r.setError(i); | ||
r.setError(e); | ||
}); | ||
@@ -552,19 +595,19 @@ }); | ||
*/ | ||
clearErrors(t, e) { | ||
if (!t) { | ||
f(this.fields, (i) => { | ||
i.clearErrors(); | ||
clearErrors(i, t) { | ||
if (!i) { | ||
E(this.fields, (e) => { | ||
e.clearErrors(); | ||
}); | ||
return; | ||
} | ||
if (e) | ||
switch (t) { | ||
if (t) | ||
switch (i) { | ||
case "include": | ||
f(this.fields, (i) => { | ||
e.includes(i.getName()) && i.clearErrors(); | ||
E(this.fields, (e) => { | ||
t.includes(e.getName()) && e.clearErrors(); | ||
}); | ||
return; | ||
case "exclude": | ||
f(this.fields, (i) => { | ||
e.includes(i.getName()) || i.clearErrors(); | ||
E(this.fields, (e) => { | ||
t.includes(e.getName()) || e.clearErrors(); | ||
}); | ||
@@ -575,6 +618,7 @@ return; | ||
/** | ||
* Check if any fields have errors | ||
* Check if any fields have errors or a particular field if given a name | ||
* @param name field name to check | ||
*/ | ||
hasError() { | ||
return Object.values(this.fields).some((t) => t.hasError()); | ||
hasError(i) { | ||
return i ? (this.assertFieldExists(i), this.fields[i].hasError()) : Object.values(this.fields).some((t) => t.hasError()); | ||
} | ||
@@ -584,9 +628,9 @@ /** | ||
*/ | ||
getWarnings(t) { | ||
if (t) | ||
return this.assertFieldExists(t), this.fields[t].getWarnings(); | ||
let e = {}; | ||
return Object.entries(this.fields).forEach(([i, r]) => { | ||
e[i] = r.getWarnings(); | ||
}), e; | ||
getWarnings(i) { | ||
if (i) | ||
return this.assertFieldExists(i), this.fields[i].getWarnings(); | ||
let t = {}; | ||
return Object.entries(this.fields).forEach(([e, r]) => { | ||
t[e] = r.getWarnings(); | ||
}), t; | ||
} | ||
@@ -597,10 +641,10 @@ /** | ||
*/ | ||
addWarnings(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
!i || !i.length || h(e, this.fields, (r) => { | ||
if (typeof i == "string") { | ||
r.addWarning(i); | ||
addWarnings(i) { | ||
Object.entries(i).forEach(([t, e]) => { | ||
!e || !e.length || h(t, this.fields, (r) => { | ||
if (typeof e == "string") { | ||
r.addWarning(e); | ||
return; | ||
} | ||
r.addWarnings(i); | ||
r.addWarnings(e); | ||
}); | ||
@@ -613,10 +657,10 @@ }); | ||
*/ | ||
setWarnings(t) { | ||
Object.entries(t).forEach(([e, i]) => { | ||
!i || !i.length || h(e, this.fields, (r) => { | ||
if (typeof i == "string") { | ||
r.setWarning(i); | ||
setWarnings(i) { | ||
Object.entries(i).forEach(([t, e]) => { | ||
!e || !e.length || h(t, this.fields, (r) => { | ||
if (typeof e == "string") { | ||
r.setWarning(e); | ||
return; | ||
} | ||
r.setWarnings(i); | ||
r.setWarnings(e); | ||
}); | ||
@@ -626,6 +670,7 @@ }); | ||
/** | ||
* Check if any fields have warnings | ||
* Check if any fields have warnings or a particular field if given a name | ||
* @param name field name to check | ||
*/ | ||
hasWarning() { | ||
return Object.values(this.fields).some((t) => t.hasWarning()); | ||
hasWarning(i) { | ||
return i ? (this.assertFieldExists(i), this.fields[i].hasWarning()) : Object.values(this.fields).some((t) => t.hasWarning()); | ||
} | ||
@@ -636,3 +681,3 @@ /** | ||
isModified() { | ||
return Object.values(this.fields).some((t) => t.isModified()); | ||
return Object.values(this.fields).some((i) => i.isModified()); | ||
} | ||
@@ -642,10 +687,10 @@ applyEffectValidations() { | ||
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); | ||
} catch (i) { | ||
let t = !0; | ||
return i instanceof j.ZodError ? (Object.entries(i.formErrors.fieldErrors).forEach( | ||
([e, r]) => { | ||
const n = this.fields[e].getErrors(), a = (r == null ? void 0 : r.filter((o) => !n.includes(o))) ?? []; | ||
a.length > 0 && (this.fields[e].addErrors(a), t = !1); | ||
} | ||
), e) : (t instanceof Error, !1); | ||
), t) : (i instanceof Error, !1); | ||
} | ||
@@ -658,27 +703,27 @@ } | ||
this.clearErrors(); | ||
let t = !0; | ||
return f(this.fields, (e) => { | ||
e.validate() || (t = !1); | ||
}), E(this.schema) && (t = this.applyEffectValidations()), t; | ||
let i = !0; | ||
return E(this.fields, (t) => { | ||
t.validate() || (i = !1); | ||
}), m(this.schema) && (i = this.applyEffectValidations()), i; | ||
} | ||
} | ||
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); | ||
const Z = (s) => { | ||
var i; | ||
return ((i = s == null ? void 0 : s.isKatachiError) == null ? void 0 : i.call(s)) || (s == null ? void 0 : s.name) === "KatachiGenericError" || s instanceof y; | ||
}, D = (s) => { | ||
var i; | ||
return ((i = s == null ? void 0 : s.isKatachiError) == null ? void 0 : i.call(s)) && ((s == null ? void 0 : s.name) === "KatachiFieldAlreadyExistsError" || s instanceof w); | ||
}, _ = (s) => { | ||
var i; | ||
return ((i = s == null ? void 0 : s.isKatachiError) == null ? void 0 : i.call(s)) && ((s == null ? void 0 : s.name) === "KatachiFieldNotFoundError" || s instanceof O); | ||
}; | ||
export { | ||
p as Field, | ||
x as Field, | ||
w as FieldAlreadyExistsError, | ||
x as FieldNotFoundError, | ||
S as Form, | ||
V as KatachiError, | ||
I as isFieldAlreadyExistsError, | ||
N as isFieldNotFoundError, | ||
K as isGenericKatachiError | ||
O as FieldNotFoundError, | ||
M as Form, | ||
y as KatachiError, | ||
D as isFieldAlreadyExistsError, | ||
_ as isFieldNotFoundError, | ||
Z as isGenericKatachiError | ||
}; |
{ | ||
"name": "@katachi-forms/core", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"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
53456
1163