Skip to content

Commit ed007b9

Browse files
committed
Add patternProperties to Schema
Based on JSON schema 2021 [1]. Where the value is expected to be a map where the key is a regex (to match a string property name) and value is a Schema object. Like the implementation of pattern in this codebase this doesn't have validation that pattern is a regex, which could be added. [1]: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-10.3.1.1
1 parent 123b76b commit ed007b9

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

json-schema-for-3.1.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ items: array of schema - in 3.0
8686
contains: schema - done
8787

8888
properties: object, each value json schema - in 3.0
89-
patternProperties: object each value JSON schema
89+
patternProperties: object each value JSON schema key regex - done
9090
additionalProperties: single json schema
9191

9292
unevaluatedItems - single schema
@@ -110,6 +110,7 @@ Little things:
110110
- schema integer fields generally are required to be non-negative
111111
- quite common for arrays to be invalid if not unique (required, type)
112112
- probably want a quick way to get coverage of the methods on nodes
113+
- could validate that pattern and patternProperties contain regexs
113114

114115
JSON Schema specs:
115116

lib/openapi3_parser/node/schema/v3_1.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def prefix_items
8989
def contains
9090
self["contains"]
9191
end
92+
93+
# @return [Node::Map<String, Schema>]
94+
def pattern_properties
95+
self["patternProperties"]
96+
end
9297
end
9398
end
9499
end

lib/openapi3_parser/node_factory/schema/v3_1.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class V3_1 < NodeFactory::Object # rubocop:disable Naming/ClassAndModuleCamelCas
3636
field "else", factory: :referenceable_schema
3737
field "prefixItems", factory: :prefix_items_factory
3838
field "contains", factory: :referenceable_schema
39+
field "patternProperties", factory: :pattern_properties_factory
3940

4041
def build_node(data, node_context)
4142
Node::Schema::V3_1.new(data, node_context)
@@ -88,6 +89,13 @@ def prefix_items_factory(context)
8889
value_factory: NodeFactory::Schema.factory(context)
8990
)
9091
end
92+
93+
def pattern_properties_factory(context)
94+
NodeFactory::Map.new(
95+
context,
96+
value_factory: NodeFactory::Schema.factory(context)
97+
)
98+
end
9199
end
92100
end
93101
end

spec/support/examples/v3.1/changes.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ components:
107107
type: string
108108
unevaluatedItems:
109109
type: string
110-
# Add object
110+
Object:
111+
type: object
112+
patternProperties:
113+
/^test/:
114+
type: string
111115
# Add content types
112116
# Add $ref usage (plain, merged, defs)
113117
# Add compound things: anyOf, oneOf, not, if, then, else

0 commit comments

Comments
 (0)