跪拜 Guibai
← All articles
Vue.js · Low-code

Schema-Driven Validation in Vue 3: vue-juan-form Puts Rules in the Schema, Not the Handlers

By 全栈小卷 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Vue 3 teams building schema-driven forms get a validation model that lives in data rather than scattered handlers. The layer API makes partial submits a first-class operation, which simplifies complex multi-section UIs like insurance or checkout flows.

Summary

vue-juan-form renders Vue 3 pages from a schema and lets developers attach validation rules directly to field definitions. A string DSL handles common cases: 'required|email', 'min:18|max:60', 'matches:/^1[3-9]\d{9}$/'. Rules run on the current field while editing, on the whole form at submit, and can be scoped to a specific layer via an injected 'layer' object and its check() method.

Custom rules are plain functions that throw RuleError, and reusable ones register globally with defineRule. The library also validates at the row and table level: cross-row uniqueness, sum constraints, and dynamic add-row guards all live in the schema. Failed submissions automatically scroll to and focus the first invalid field. The current release only supports Element Plus components.

Takeaways
— Validation rules are declared in the schema string, separated by '|', with ':' for parameters and ',' for multiple args.
— A leading '+' on a rule causes it to run even when the field value is empty, useful for cross-field and cleanup logic.
— defineRule registers a reusable rule; rules receive the validated scope and can inject validationContext or node.
— Injecting 'layer' and calling check() validates only the current layer (group, row, table, or whole form), enabling per-section submits.
— Tables and lists accept 'validation' on the container itself, so aggregate checks like sum caps work without extra input fields.
— On failed submit, the engine scrolls to the first error field and focuses its input by default, no extra config.
— The current version is scoped to Element Plus; UI components are imported on demand.
Conclusions

The schema DSL offloads rule orchestration to a declarative string, but it substitutes a small syntax for what might be clearer function calls; teams will pay a learning curve for complex expressions like '+notAllEmpty:email,phone'.

Treating any container as a validation layer blurs the line between form, group, and table, which suits low-code builders that need uniform submission logic across heterogeneous UIs.

Concepts & terms
Layer API
An injected handle that scopes check() to a specific part of the form, such as a group, row, or table, allowing partial validation without validating the whole form.
RuleError
A custom error thrown inside a validation rule to signal failure; the engine catches it and shows the message on the field.
defineRule
Global registration of a named validation rule, making it reusable across schemas; the rule can inject a validation context to access values and arguments.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗