Skip to content
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

pattern validation for propertyNames #326

Closed
kimtkyeom opened this issue Sep 29, 2020 · 5 comments
Closed

pattern validation for propertyNames #326

kimtkyeom opened this issue Sep 29, 2020 · 5 comments

Comments

@kimtkyeom
Copy link

kimtkyeom commented Sep 29, 2020

We are using json-schema-validator for validate given json string is valid.
Recently, we need to validate json fields in object has valid format (using regex)
From draft-v6, json schema specification supports propertyNames.
However json-schema-validator currently not supports patterns, is there any plan to support propertyNames features (Seeing source code, currently only supports minLength and maxLength

Below shows validation with patternProperties using scala repl. I expected validation returns invalid messages but not (refer to web validator)

scala> import com.fasterxml.jackson.databind.node.{ArrayNode, JsonNodeType, ObjectNode}
import com.fasterxml.jackson.databind.node.{ArrayNode, JsonNodeType, ObjectNode}

scala> import com.networknt.schema.{JsonSchemaFactory, SpecVersion, ValidationMessage}
import com.networknt.schema.{JsonSchemaFactory, SpecVersion, ValidationMessage}

scala> import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}

scala> val mapper = new ObjectMapper()
mapper: com.fasterxml.jackson.databind.ObjectMapper = com.fasterxml.jackson.databind.ObjectMapper@42672d9d

scala> val sch = mapper.readTree("""{"type": "object", "propertyNames": {"pattern": "^[A-Za-z-_]*$"}}""")
sch: com.fasterxml.jackson.databind.JsonNode = {"type":"object","propertyNames":{"pattern":"^[A-Za-z-_]*$"}}

scala> val jsonSchemaFactory: JsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)
jsonSchemaFactory: com.networknt.schema.JsonSchemaFactory = com.networknt.schema.JsonSchemaFactory@270f76dc

scala> val schema = jsonSchemaFactory.getSchema(sch)
schema: com.networknt.schema.JsonSchema = "#" : {"type":"object","propertyNames":{"pattern":"^[A-Za-z-_]*$"}}

# This json node should be failed in validation
scala> schema.validate(mapper.readTree("""{"123": 123}"""))
res6: java.util.Set[com.networknt.schema.ValidationMessage] = []

scala> schema.validate(mapper.readTree("""{"asdf": 123}"""))
res7: java.util.Set[com.networknt.schema.ValidationMessage] = []
@stevehu
Copy link
Contributor

stevehu commented Oct 1, 2020

@kimtkyeom I am not aware that patterns are supported for the propertyNames. It might be added in the recent version but I couldn't find any test cases in the official test suite. Could you please point me out the specification? It should be easy to implement in my opinion as we have done that for the patternProperties. Also, I am wondering if you could open an issue to let the specification team fix the test suite. Thanks a lot for raising it up.

@ShaulEngler
Copy link

https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.3.2.5
the spec references propertyNames as any other JSON Schema pattern included, no limitations applied to it.

also can see this discussion:
json-schema-org/json-schema-org.github.io#77
which actively uses pattern as a suggested new example

@stevehu
Copy link
Contributor

stevehu commented Oct 27, 2020

@ShaulEngler Thanks a lot for the links. It makes perfect sense in my opinion. I've marked help wanted and hope some team members can pick it up and get it implemented.

@LeifRilbeATG
Copy link

@stevehu Please review related PR.

stevehu pushed a commit that referenced this issue Feb 4, 2021
* #326: Pattern validation for propertyNames

* #326: Adjust test case descriptions.

Co-authored-by: leif.rilbe <leif.rilbe@ATG68462.hastsportenshus.atg.se>
@stevehu stevehu changed the title pattern validation for "propertyNames" pattern validation for propertyNames Feb 4, 2021
@fdutton
Copy link
Contributor

fdutton commented May 22, 2023

Resolved in #397

@fdutton fdutton closed this as completed May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants