pattern validation for propertyNames
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] = []
@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.
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: https://github.com/json-schema-org/json-schema-org.github.io/issues/77 which actively uses pattern as a suggested new example
@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.
@stevehu Please review related PR.
Resolved in #397