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

Haw can we retrive available pointer in schema order

Open onlysli opened this issue 5 years ago • 11 comments

Using react-jsonschema-forms we get formdata as Json object wich does not follow the order of the json schema used as form defination (https://github.com/rjsf-team/react-jsonschema-form/issues/1178).

We are using opis to validate the data received from the client.

We want to create a report on the server site in the same order as the form showed on the client side. For this we need the sequence of the form fields.

Is there a method to extract all fields from the schema in same order as in the schema?

For example:

{
  "$id": "http://example.com/schema.json#",
  
  "definitions": {
    "name": {
      "type": "string",
      "minLength": 1
    },
    "personal": {
      "email": {
        "type": "string",
        "format": "email"
      },
      "birthday": {
        "type": "string",
        "format": "date"
      }
    }
  }
}

Should retrieve something like this:

[
"name", 
"personal",
"personal/email",
"personal/birthday"
]

Thank you in advance.

onlysli avatar Jan 25 '21 17:01 onlysli

I don't understand how this is related to opis/json-schema

Is there a method to extract all fields from the schema in same order as in the schema?

Do you have a method that extracts all fields from the schema in "another" order? If you do, please post an example to reproduce your issue.

sorinsarca avatar Jan 25 '21 19:01 sorinsarca

Is there a method to extract all fields from the schema in same order as in the schema?

Do you have a method that extracts all fields from the schema in "another" order? If you do, please post an example to reproduce your issue.

No, I don't. I'm using opis/json-schema to validate formdata against json schema. What I want is to have a method to extract all fields from the json schema. Is there any method that does that?

Any help would be appreciated. Thanks.

onlysli avatar Jan 26 '21 07:01 onlysli

You cannot extract the fields until you evaluate the schema. Here is an example:

{
  "if": {
    "required": ["age"]
  },
  "then": {
    "required": ["name"]
  },
  "else": {
    "oneOf": [
        {
           "properties": {
               "email": {"type": "string", "format": "email"}
           }
        },
        {
           "properties": {
               "phone": {"type": "string"}
           }
        }
    ]
  }
}

In the above example, what should be retrieved?

sorinsarca avatar Jan 26 '21 07:01 sorinsarca

@sorinsarca I have a similar problem, so I join the discussion.

I am not sure if your fragment is a valid JSON schema, I am not aware that shema suppoets "if", "then", "else" keys. but if it does, I would expect a list of valid pointes.

Suppose you call a method

$result = $validator->availablePointes("abc", $schema)

you should get an array

[
"age",
"name",
"email"
"phone"
]

this represents the applicable pointers to all instances of that schema.

bwl21 avatar Jan 26 '21 08:01 bwl21

I am not sure if your fragment is a valid JSON schema, I am not aware that shema suppoets "if", "then", "else" keys. but if it does, I would expect a list of valid pointes.

It is a valid json-schema, see https://opis.io/json-schema/1.x/conditional-subschemas.html#if-then-else

Ok, lets try some other thing:

{
  "type": "object",
  "patternProperties": {
    "^my": {"type": "string"}
  }
}

Docs: https://opis.io/json-schema/1.x/object.html#patternproperties

Now, what should be retrieved?

sorinsarca avatar Jan 26 '21 08:01 sorinsarca

It is a valid json-schema, see https://opis.io/json-schema/1.x/conditional-subschemas.html#if-then-else

I see and found it indeed in http://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.2.2. Thanks for the hint. I never used this, so I was not aware of that.

@onlysli requested an ordered list of viable pointers into instances, not to retrieve the detail properties.

Pattern properties are really not applicable for this approach unless we simply embed the pattern into the pointer (which results in non valid pointers according to https://tools.ietf.org/html/rfc6901.


...some object definition
{
  "properties":{
  "myProperties":{
    "type": "object",
    "patternProperties": {
      "^my": {"type": "string"}
    }
  }
}

and the expected result could be

[ ".../myProperties/^my" ]

Nevertheless, it would allow a an application to construct a pointer and the validate it. By this we could distinguish the case

  • that the JSON instance does not contain
  • the pointer is not applicable at all, such that the JSON instance cannot contain an object matching to the pointer.

the latter case would be an error which needs tobe reported to the programmer, while the first one would be reported to the user.

@onlysli has a use case with react json schema form (play with it in https://rjsf-team.github.io/react-jsonschema-form/). Which does not support pattern properties either.

bwl21 avatar Jan 26 '21 08:01 bwl21

About 2 hours ago I said

You cannot extract the fields until you evaluate the schema.

When the properties could be extract easily only be reading json, you posted an example. Now, when any property name that starts with "my" is allowed you cannot post an example because there is an infinite list

[
  "mya",
  "myb",
  ...
]

@onlysli requested an ordered list of viable pointers into instances, not to retrieve the detail properties.

I don't understand what is "list of viable pointers into instances" nor "detail properties".

Pattern properties are really not applicable for this approach unless we simply embed the pattern into the pointer (which results in non valid pointers according to https://tools.ietf.org/html/rfc6901.

What? Why? Pattern properties are used to match (usually) more than one property.

Unrelated but, if you want to embed that pattern you should use the percent-encoded form to have valid pointers.

@onlysli has a use case with react json schema form (play with it in https://rjsf-team.github.io/react-jsonschema-form/). Which does not support pattern properties either.

I don't care if rect json schema or other tool supports patternProperties or not, I can give you other examples where you cannot extract those "pointers" without evaluating the schema: https://opis.io/json-schema/1.x/object.html#additionalproperties https://opis.io/json-schema/1.x/object.html#dependencies https://opis.io/json-schema/1.x/object.html#propertynames

In the end, you cannot extract all pointers to data only from a schema, you may catch some (by looking at the properties keyword or similar).

Considering this to be the data (not schema)

{
  "a": {
     "b": 1
  },
  "c": 2
}

we have the following pointers

/a
/a/b
/c

if we are interested only in leafs, then

/a/b
/c

Now, if we have the following schema

{
  "type": "object",
  "properties": {"c": {"type": "integer"}},
  "additionalProperties": true
}

we can extract only /c because other properties are allowed as well - but we don't know them.

Anyway, I don't understand the use-case for this. Why do you need the pointer? When?

sorinsarca avatar Jan 26 '21 09:01 sorinsarca

Hi, everybody! This seems to be an issue not related to the validation process itself, so I'm going to close it. What you do before validation, or after the validation, is out of the library scope. Thanks!

msarca avatar Jan 26 '21 09:01 msarca

{
 "type": "object",
 "properties": {"c": {"type": "integer"}},
 "additionalProperties": true>

}


Result could be 

```JSON
[
"c"
"c/.*"
]

the use case is

  1. data shall be processed in schema order, such that the result e.g. in a report has predictable order
  2. OPIS can help to overcome the fact that in ES5 the order of object properties are not gauaranteed
  3. application should be able to verify semantics of pointes, such that we can have better error messages

I am not happy that you close this ... but it is your decision. Alternative request, could you provide access to the "flattened" schema, where all references are resolved. Then we could solve the problem on our side.

bwl21 avatar Jan 26 '21 09:01 bwl21

data shall be processed in schema order

There is no such thing as "schema order" - json objects are unordered.

OPIS can help to overcome the fact that in ES5 the order of object properties are not gauaranteed

I don't want to overcome that, I'm happy that objects are unordered.

If two properties are "required" you cannot say that one is "more required" than the other - so the order should not matter.

application should be able to verify semantics of pointes, such that we can have better error messages

What semantics a pointer can have? You were interested in an ordered list now we need pointer semantics (whatever that is).

Alternative request, could you provide access to the "flattened" schema, where all references are resolved.

There is no such thing as "flattened" schema because references are resolved only when needed.

If you are interested in error messages you can check the issues, there are some examples on how to format them, but this issue remains closed.

sorinsarca avatar Jan 26 '21 09:01 sorinsarca

well so shall it be ;-( anyhow thanks for OPIS

bwl21 avatar Jan 26 '21 09:01 bwl21