json-forms icon indicating copy to clipboard operation
json-forms copied to clipboard

validating parts of an object for the oneOf component.

Open ocschwar opened this issue 10 years ago • 4 comments

A oneOf element defines an object that validates against exactly one alternative in a list of definitions.

So to generate a preloaded form with a oneOf element, you need to validate that branch of the object against each alternative and select the right one to preload the form with the right elements.

On my own branch, I have each alternative identify itself with an element that's defined to have an enum with one member. But for general use, we need to be able to validate sub-tree against the right list of sub-schemas to get preloading done right.

ocschwar avatar Jan 13 '16 18:01 ocschwar

Can you post an example to reproduce the issue (schema and initial data)? Thanks!

idelvall avatar Jan 13 '16 18:01 idelvall

Hi again, I' ve working on this but I can't understand what exactly the problem is. At first I thought that validation was not working for oneOf schemas, but indeed it works (http://brutusin.org/json-forms/#7). Can you concrete a little more please?

idelvall avatar Jan 21 '16 12:01 idelvall

Here’s a quick example: a schema with a “one of” and three possible instances:

{ "id": "http://cse.fraunhofer.org/schemas/premises_electric", "$schema": "http://json-schema.org/draft-04/schema#", "description":"example", "type":"object", "properties":{ "foo":{ "oneOf":[ {"type":"object", "properties":{ "bar":{"type":"string"} }}, {"type":"object", "properties":{ "baz":{"type":"string"} }}, {"type":"object", "properties":{ "quux":{"type":"string"} }}, ] } } }

{ "foo":{"bar":"example"} } { "foo":{"baz":"example"} } { "foo":{"quux":"example"} }

When you preload one of these onto jsonforms, the menu is there, set to the null option. The moment you select the right option of the three, the object’s attributes get preset properly.

To get the right option to be selected automatically, you need to run the validator against The “foo” subset of the object’s attributes. The standard says the object should validate against exactly one option.

From: Ignacio del Valle Alles <[email protected]mailto:[email protected]> Reply-To: brutusin/json-forms <[email protected]mailto:[email protected]> Date: Thursday, January 21, 2016 at 7:33 AM To: brutusin/json-forms <[email protected]mailto:[email protected]> Cc: Omri Schwarz <[email protected]mailto:[email protected]> Subject: Re: [json-forms] validating parts of an object for the oneOf component. (#13)

Hi again, I' ve working on this but i am can't understand what exactly the problem is. At first I thought that validation was not working for oneOf schemas, but indeed it works (http://brutusin.org/json-forms/#7). Can you concrete a little more please?

— Reply to this email directly or view it on GitHubhttps://github.com/brutusin/json-forms/issues/13#issuecomment-173557511.

ocschwar avatar Jan 21 '16 12:01 ocschwar

Thanks, it's clear now.

Yes, given that oneOf choice is not known a priori, an iteration over the possible schemas has to be performed until finding the best choice. I guess this is what you are doing. I'll take a loot into it.

Notice than "best choice" not necessarily means passing validation since current implementation does not purely validate data against the schema (for example, having data with more properties than the schema would pass), so I you have two schemas as choices where one is a subset of the other, and data is from the big schema, it would pass also on the small schema.

idelvall avatar Jan 21 '16 14:01 idelvall