@react-form-builder/core
Version:
React JSON Schema Form Builder to create complex, validated, reusable forms with no deep React knowledge required
57 lines (56 loc) • 1.82 kB
JavaScript
import { debounce as u } from "../../../utils/tools.js";
function g(c) {
return c.message ?? `Validation failed: ${c.settings.key}`;
}
class l {
constructor(e, r, t, i, o) {
this.setter = i;
const s = (a) => o?.(a) ?? a.map(g);
this.#e = async (a, d = !1) => {
const n = await t(a, e, r);
if (!(!n || n.length === 0))
return d && !e.showAllValidationErrors ? s([n[0]]) : s(n);
};
}
setter;
#e;
/**
* Creates a DataValidator instance.
* @param store the form viewer settings.
* @param getFormData the function that returns a form data.
* @param resolver the validation function factory.
* @param args the validation function factory arguments.
* @param setter the callback function called to set a validation error.
* @param localizer the function that localizes validation error messages.
* @template T the validation function factory arguments.
* @returns the DataValidator instance.
*/
static create = (e, r, t, i, o, s) => new l(e, r, t(i), o, s);
/**
* Generates an event to perform validation.
* @param value the validated value.
*/
sendValidationEvent = (e) => {
u(this.validate, 200)(e)?.catch(console.error);
};
/**
* Performs a validation of the value.
* @param value the validated value.
* @returns the Promise with the result of the validation.
*/
validate = async (e) => {
const t = (await this.#e(e, !0))?.join(" ");
return this.setter(t), t;
};
/**
* Returns the validation results without triggering an events and changing the state of the form.
* @param value the validated value.
* @returns the validation results.
*/
getValidationResult = (e) => this.#e(e);
}
export {
l as DataValidator,
g as getDefaultErrorMessage
};
//# sourceMappingURL=DataValidator.js.map