json-schema
Library for converting JsonSchema to Form in field validation
For Vue 3
๐ฆ Installation
npm i v3-json-schema-form
๐จ Usage
// index.js import App from "./App.vue"; import { createApp } from "vue"; import SchemaForm from "v3-json-schema-form/dist/schema-form.es.js"; import "v3-json-schema-form/dist/style.css"; const app = createApp(App); app.use(SchemaForm); app.mount("#app"); <!-- component.vue --> <template> <schema-form :schema="schema" :form-data="form" @on-change-form="onChangeForm" @onSubmit="onSubmit" /> </template> <script> import json from "./json-schema"; export default { name: "component", data() { return { schema: json, form: {}, }; }, methods: { onChangeForm(newForm) { this.form = newForm; }, onSubmit() { console.table(this.form); }, }, }; </script>
schema JSON parameters:
properties
โ An Object with entity fields of the form
type
โ field value type (โobjectโ or โarrayโ or โstringโ or โbooleanโ or โnumberโ)title
โ the output title of the field/nodedescription
โ the output description of the field/nodedefault
โ default valueproperties
โ only for type=โobjectโ. This is an object with the same fields as the parent: type, title, properties, items, etc.items
โ only for type=โarrayโ, This is an object describing an element of the form array, which has the same fields as the parent: type, title, properties, items, etc.validation
โ validation field/node:required
โ Boolean. Is it necessary to fill in field/nodeminimum
/maximum
โ limiting the value for numbersminlength
/maxlength
โ limitation of the allowed number of charactershardValue
โ fixed mandatory valueui
:inputType
:- all built-in input types (view here)
- โselectโ
- โtextareaโ
draggable
โ Boolean. Only for inputType=โfileโ โ Drag and Dropautofocus
โ Booleanplaceholder
โ Stringdescription
โ String. Description of the input under the headingmask
โ input mask ({ mask: โ+7(000)000-00-00โ, lazy: true }) (use vue-imask library)customErrors
โ Object, in which the keys are the type of error from the validation field, and the value of the field is the text of the custom errorenum
โ Array with a list of possible values (for types โradioโ, โcheckboxโ, โselectโ)["val-1"]
โ a one-dimensional array with values. Then the value and the label of the element will be the same[{ caption: "one", value: 1, disabled: true }]
โ multidimensional array, where caption, value are specified specificallygrid
โ Object โ custom item position in the gridcolumn
(optional parameter) โ Number โ occupied column (on all screen resolutions)row
(optional parameter) โ Number โ occupied row (on all screen resolutions)media
(optional parameter) โ Object of type{ "768": { column: 1, row: 2 } }
(the key is the width of the screen above which it will be applied value (768+)). media permissions can be as many as you want. Has a higher priority than grid.column or grid.row
Notes
- What you need to get radio, checkbox, select:
- for the radio list, it is necessary that the node has the field
enum: []
, and thetype
isstring || number || boolean
(if there is the default field, then, of the corresponding type"default": "foo"
) - for the list of checkboxes it is necessary that the node has the field
enum: []
, and thetype
isarray
(if there is the default field, then, of the corresponding type"default": ["foo", "bar"]
) - for the list of options select-a it is necessary that the node has the field
enum: []
and the field"ui":{"inputType":"select"}
. If itstype
isarray
, then it is a multiple-choice selector, iftype
with the valuestring||number|| boolean
, then it is a regular select - if you forget to specify the
enum
field, it will be a completely different type of node
- Toggle
- for a standard toggler, you only need to specify
"type": "boolean"
("default": true/false
)