UNPKG

vue-yup-form

Version:

Headless form validation with Vue and Yup

170 lines (169 loc) 4.91 kB
// src/index.ts import { computed, isRef, shallowRef } from "vue-demi"; import { ArraySchema, NumberSchema } from "yup"; var Field = class { constructor(value, schema, validateOptions) { this.$_valueRef = isRef(value) ? value : shallowRef(value); const isSchemaTypeFunction = typeof schema === "function"; const initialSchema = isSchemaTypeFunction ? schema() : schema; const isNumberSchemaField = initialSchema instanceof NumberSchema; this.$_errorRef = computed( schema ? () => { const obtainedSchema = isSchemaTypeFunction ? schema() : schema; const targetValue = isNumberSchemaField && typeof this.$_valueRef.value !== "number" ? void 0 : this.$_valueRef.value; try { obtainedSchema.validateSync(targetValue, validateOptions); } catch (e) { return e; } return void 0; } : () => void 0 ); this.$label = (initialSchema == null ? void 0 : initialSchema.describe().label) || ""; } get $value() { return this.$_valueRef.value; } set $value(value) { this.$_valueRef.value = value; } get $error() { return this.$_errorRef.value; } get $errorMessages() { var _a; return ((_a = this.$_errorRef.value) == null ? void 0 : _a.errors) || []; } }; var PrivateField = class extends Field { }; var FormsField = class { constructor(generateForm, initialValueListOrLength, schema, validateOptions) { let count = 1; this.$generateFormWithKey = (initialValue) => { const form = generateForm(initialValue); form.$key = count++; return form; }; this.$_formsRef = shallowRef([]); this.$initialize(initialValueListOrLength); const isSchemaTypeFunction = typeof schema === "function"; this.$_errorRef = computed( schema ? () => { const obtainedSchema = isSchemaTypeFunction ? schema(new ArraySchema()) : schema; try { obtainedSchema.validateSync( this.$_formsRef.value, validateOptions ); } catch (e) { return e; } return void 0; } : () => void 0 ); const initialSchema = isSchemaTypeFunction ? schema(new ArraySchema()) : schema; this.$label = (initialSchema == null ? void 0 : initialSchema.describe().label) || ""; } get $forms() { return this.$_formsRef.value; } get $writableForms() { return this.$_formsRef.value; } set $writableForms(value) { this.$_formsRef.value = value; } get $error() { return this.$_errorRef.value; } get $errorMessages() { var _a; return ((_a = this.$_errorRef.value) == null ? void 0 : _a.errors) || []; } $initialize(initialValueListOrLength = []) { const initialValueList = typeof initialValueListOrLength === "number" ? [...Array(initialValueListOrLength)] : initialValueListOrLength; this.$_formsRef.value = initialValueList.map(this.$generateFormWithKey); } $append(initialValue) { const forms = this.$_formsRef.value.slice(); forms.push(this.$generateFormWithKey(initialValue)); this.$_formsRef.value = forms; } $prepend(initialValue) { const forms = this.$_formsRef.value.slice(); forms.unshift(this.$generateFormWithKey(initialValue)); this.$_formsRef.value = forms; } $remove(index) { const forms = this.$_formsRef.value.slice(); forms.splice(index + (index < 0 ? this.$_formsRef.value.length : 0), 1); this.$_formsRef.value = forms; } }; function defineForm(form) { return form; } function field(value, schema, validateOptions) { return new Field(value, schema, validateOptions); } function privateField(value, schema, validateOptions) { return new PrivateField(value, schema, validateOptions); } function formsField(generateForm, initialValueListOrLength = [], schema, validateOptions) { return new FormsField( generateForm, initialValueListOrLength, schema, validateOptions ); } function isValidForm(form) { for (const value of Object.values(form)) { if (typeof value !== "object") { continue; } if (value instanceof Field) { if (value.$error) { return false; } } else if ("$forms" in value) { if (value.$error || !value.$forms.every(isValidForm)) { return false; } } else if (!isValidForm(value)) { return false; } } return true; } function toObject(form) { const obj = {}; for (const [key, value] of Object.entries(form)) { if (typeof value !== "object") { continue; } if (value instanceof Field) { if (!(value instanceof PrivateField)) { obj[key] = value.$value; } } else if ("$forms" in value) { obj[key] = value.$forms.map(toObject); } else { obj[key] = toObject(value); } } return obj; } export { defineForm, field, formsField, isValidForm, privateField, toObject };