vue-json-schema-form

repository·master·Indexed 25 days ago

https://github.com/lljj-x/vue-json-schema-form

A library for Vue 2 and Vue 3 that dynamically generates form components with built-in validation based on JSON Schema specifications. It provides the VueForm component, which uses a schema for data structure and an optional uiSchema for UI customization. The project includes utility modules for form state management, AJV-based validation, and internationalization, as well as a core package for Vue 2 implementations.

Tokens
29.6K
Snippets
47
Records
142
Agent score
80%

What's inside vue-json-schema-form

  1. Overview of @lljj/vjsf-utils utility modules

    master

    The @lljj/vjsf-utils package provides foundational utility classes for form management. The package is organized into several specialized modules:

    • i18n: Manages current multi-language settings.
    • schema/getDefaultFormState: Calculates schema values based on jsonSchema and formData.
    • schema/validate: Provides AJV-based validation functions.
    • arrayUtils: Utilities for handling array-related form data.
    • formUtils: General utilities for form management.
    • vueUtils: Utilities specifically for Vue-related operations.
  2. Overview of vue-json-schema-form

    master

    vue-json-schema-form

    vue-json-schema-form is a library designed to generate complete form components with full validation based on JSON Schema. It is compatible with both Vue 2 and Vue 3.

    Key Features:

    • Schema-Driven: Generate forms automatically by providing a JSON Schema.
    • Validation: Uses ajv for schema validation.
    • UI Framework Support: Adapts to popular UI libraries.
    • Design Philosophy: Inspired by react-jsonschema-form regarding schema parsing and indexing.

    Useful Links:

  3. How Fields and Widgets work

    master

    The library generates HTML forms by recursively traversing a JSON Schema. It uses two primary abstractions:

    1. Field: Used to render the component corresponding to any node in the schema. A Field typically contains a FormItem component.
    2. Widget: A component used to render specific user input information (e.g., input, select). Widgets are wrapped by the FormItem component.

    Both Field and Widget can be customized using uiSchema.

  4. Core concepts of vue-json-schema-form

    master

    The library is designed to solve the problem of repetitive form configuration by using JSON Schema as the single source of truth.

    Key features include:

    • Schema-driven generation: Provide a JSON Schema, and the library generates the corresponding form.
    • UI Library Integration: It allows for personalized UI views and error message checking. The default view in the current version is based on ElementUi.
    • Validation: It uses the ajv validator to ensure data integrity.
    • Consistency: By using the same schema, both the front-end and the server can maintain identical verification rules.
  5. Configure @lljj/vue3-form-core with a UI library

    master

    To adapt a UI library, use createVue2Core (note: despite the name, this is for Vue 3 core) and provide a globalOptions object. This object maps JSON Schema types, formats, and common components to your specific UI components.

    Key configuration areas:

    • WIDGET_MAP.types: Maps JSON Schema type (e.g., string, number) to UI components.
    • WIDGET_MAP.formats: Maps JSON Schema format (e.g., date, color) to UI components. This has higher priority than types.
    • WIDGET_MAP.common: Maps common form elements like select or radioGroup.
    • WIDGET_MAP.widgetComponents: A list of custom components to be automatically registered as global components during the form's setup phase.
    • COMPONENT_MAP: Maps structural form components like form, formItem, button, and popover.
    • HELPERS: Provides utility functions for form logic, such as determining layout states.
    import createVue2Core from '@lljj/vue3-form-core';
    
    const globalOptions = {
        // widget组件和现有组件库映射关系
        WIDGET_MAP: {
            // 默认按schema type 映射默认widget组件
            types: {
                // type  boolean
                boolean: 'el-switch',
    
                // type  string
                string: 'el-input',
    
                // type number
                number: 'el-input-number',
    
                // type integer
                integer: 'el-input-number',
            },
    
            // 按 schema format 映射默认widget组件,优先级高于 types
            formats: {
                // format: color
                color: 'el-color-picker',
    
                // format: time
                time: TimePickerWidget, // 格式 20:20:39+00:00
    
                // format: date
                date: DatePickerWidget, // 格式 2018-11-13
    
                // format: date-time
                'date-time': DateTimePickerWidget, // 格式 2018-11-13T20:20:39+00:00
            },
    
            // 一些公共常用类型
            common: {
                // select option
                select: SelectWidget,
    
                // radio
                radioGroup: RadioWidget,
    
                // checkout
                checkboxGroup: CheckboxesWidget,
            },
    
            // 这里配置一些为当前ui库适配过的组件,会在运行时自动注册为全局组件,不注册为全局也可不配置
            // Vue3 只有在组件内才能获取到当前的app,所以注册时机是在 form组件setup中,且只会注册一次。
            widgetComponents: {
                CheckboxesWidget,
                RadioWidget,
                SelectWidget,
                TimePickerWidget,
                DatePickerWidget,
                DateTimePickerWidget
            }
        },
    
        // 其它表单相关组件映射关系
        COMPONENT_MAP: {
            // form组件
            form: 'el-form',
    
            // formItem 组件
            formItem: 'el-form-item',
    
            // button 组件
            button: 'el-button',
    
            // popover,用在formLable 左右布局时鼠标移入显示description
            popover: 'el-popover'
        },
        HELPERS: {
            // 是否mini显示 description
            isMiniDes(formProps) {
                return formProps && ['left', 'right'].includes(formProps.labselPosition);
            }
        }
    };
    
    const mySchemaForm = createVue2Core(globalOptions);
  6. Run specific demo tools individually

    master

    For faster compilation, you can run a specific entry point using the --dir flag with yarn run demo:dev:

    • Run only Playground:
    yarn run demo:dev --dir=index
    • Run only Visual Form Schema Generator:
    yarn run demo:dev --dir=schema-generator
    • Run only (H5) Activity Editor:
    yarn run demo:dev --dir=vue-editor
    yarn run demo:dev --dir=index
    yarn run demo:dev --dir=schema-generator
    yarn run demo:dev --dir=vue-editor
  7. Run a specific Vue2 Demo application

    master

    For faster compilation, you can run a single application by specifying the --dir flag with the corresponding entry directory name. Use these commands to run only the application you need:

    • Playground: yarn run demo:dev --dir=index
    • Visual Form Schema Editor: yarn run demo:dev --dir=schema-generator
    • H5 Activity Editor: yarn run demo:dev --dir=vue-editor