-
-
Notifications
You must be signed in to change notification settings - Fork 609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON editor JSONSchema validation #4012
JSON editor JSONSchema validation #4012
Conversation
Thanks for the pull request, @jm-amt! |
I assume the issue will be the schema version from the example. Try just remove the from nicegui import ui
schema = {
"$id": "https://example.com/product.schema.json",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"productId": {
"description": "The unique identifier for a product",
"type": "integer"
},
"productName": {
"description": "Name of the product",
"type": "string"
},
"price": {
"description": "The price of the product",
"type": "number",
"exclusiveMinimum": 0
}
},
"required": ["productId", "productName", "price"]
}
@ui.page("/")
async def index():
data = {
"productId": "10", # String not number
"productName": "Test Product",
"price": -1.01 # needs to be positive
}
ui.json_editor({"content": {"json": data}}, add_validation=schema)
ui.run() |
Thanks for the update, @jm-amt! So a minimum working example is like this: @ui.page('/')
def index():
ui.json_editor({'content': {'json': {'number': 'nan'}}},
add_validation={'type': 'object', 'properties': {'number': {'type': 'integer'}}}) Unfortunately it doesn't work on the auto-index page (without the I wonder if we can simplify the approach and pass the schema via the self._props['schema'] = schema with |
I cannot say I had ever used auto indexing. I think your idea of going through props is a much better idea. I removed the add_validation method and replaced it with the This would now be a minimum working example (auto index page issue has been fixed): ui.json_editor({'content': {'json': {'number': 'nan'}}}, schema={'type': 'object', 'properties': {'number': {'type': 'integer'}}}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, @jm-amt!
I just added a demo for the documentation and a simply pytest.
Let's merge! 🚀
Added JSONSchema validation to json_editor.
New method
json_editor.add_validation(schema)
will enable validation on the json_editor component.Feature request: #4003