Skip to content

Commit

Permalink
JSON editor JSONSchema validation (#4012)
Browse files Browse the repository at this point in the history
Added JSONSchema validation to json_editor. 

New method `json_editor.add_validation(schema)` will enable validation
on the json_editor component.

---

Feature request: #4003

---------

Co-authored-by: Falko Schindler <[email protected]>
  • Loading branch information
jm-amt and falkoschindler authored Nov 27, 2024
1 parent 9fe0d19 commit f6e1377
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
11 changes: 10 additions & 1 deletion nicegui/elements/json_editor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONEditor } from "standalone";
import { JSONEditor, createAjvValidator } from "standalone";

export default {
template: "<div></div>",
Expand All @@ -9,6 +9,8 @@ export default {
this.properties.onSelect = (selection) => {
this.$emit("select", { selection: selection });
};

this.checkValidation();
this.editor = new JSONEditor({
target: this.$el,
props: this.properties,
Expand All @@ -21,8 +23,14 @@ export default {
this.destroyEditor();
},
methods: {
checkValidation() {
if (this.schema !== undefined) {
this.properties.validator = createAjvValidator({ schema: this.schema, schemaDefinitions: {}, ajvOptions: {} });
}
},
update_editor() {
if (this.editor) {
this.checkValidation();
this.editor.updateProps(this.properties);
}
},
Expand All @@ -43,5 +51,6 @@ export default {
},
props: {
properties: Object,
schema: Object,
},
};
5 changes: 5 additions & 0 deletions nicegui/elements/json_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self,
properties: Dict, *,
on_select: Optional[Handler[JsonEditorSelectEventArguments]] = None,
on_change: Optional[Handler[JsonEditorChangeEventArguments]] = None,
schema: Optional[Dict] = None,
) -> None:
"""JSONEditor
Expand All @@ -29,10 +30,14 @@ def __init__(self,
:param properties: dictionary of JSONEditor properties
:param on_select: callback which is invoked when some of the content has been selected
:param on_change: callback which is invoked when the content has changed
:param schema: optional `JSON schema <https://json-schema.org/>`_ for validating the data being edited
"""
super().__init__()
self._props['properties'] = properties

if schema:
self._props['schema'] = schema

if on_select:
self.on_select(on_select)

Expand Down
7 changes: 7 additions & 0 deletions tests/test_json_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ async def get_data():

screen.click('Get Data')
screen.should_contain("Data: {'json': {'a': 1, 'b': 2}}")


def test_json_editor_validation(screen: Screen):
ui.json_editor({'content': {'json': {'x': 0}}}, schema={'type': 'object', 'properties': {'x': {'type': 'string'}}})

screen.open('/')
screen.should_contain('must be string')
33 changes: 33 additions & 0 deletions website/documentation/content/json_editor_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,39 @@ def main_demo() -> None:
on_change=lambda e: ui.notify(f'Change: {e}'))


@doc.demo('Validation', '''
You can use the `schema` parameter to define a [JSON schema](https://json-schema.org/) for validating the data being edited.
In this demo, the editor will warn if the data does not match the schema:
- `id` must be an integer
- `name` must be a string
- `price` must be a number greater than 0
''')
def schema_demo() -> None:
schema = {
'type': 'object',
'properties': {
'id': {
'type': 'integer',
},
'name': {
'type': 'string',
},
'price': {
'type': 'number',
'exclusiveMinimum': 0,
},
},
'required': ['id', 'name', 'price'],
}
data = {
'id': 42,
'name': 'Banana',
'price': 15.0,
}
ui.json_editor({'content': {'json': data}}, schema=schema)


@doc.demo('Run methods', '''
You can run methods of the JSONEditor instance using the `run_editor_method` method.
This demo shows how to expand and collapse all nodes and how to get the current data.
Expand Down

0 comments on commit f6e1377

Please sign in to comment.