Commit 526dbada authored by Paul Warren's avatar Paul Warren

Add support for patterns.

parent 6aa3f7bb
......@@ -38,7 +38,7 @@ class Parser:
while True:
self.eat_whitespace()
if self.get_token("\}"):
if self.get_token(r'\}'):
return
name = None
......@@ -61,22 +61,22 @@ class Parser:
c.extends(b.group(1))
self.eat_whitespace()
if self.get_token('\?'):
if self.get_token(r'\?'):
x.make_optional(name)
self.eat_whitespace()
if self.get_token(':'):
if self.get_token(r':'):
self.parse_type_definition(c)
elif self.get_token('\.\.\.'):
elif self.get_token(r'\.\.\.'):
x.allow_additional_properties()
else:
raise ParseError("Unexpected input: " + self.s[self.p:])
self.eat_whitespace()
if self.get_token("\}"):
if self.get_token(r'\}'):
return
elif self.get_token(","):
pass
......@@ -86,23 +86,28 @@ class Parser:
def parse_type_definition(self, c):
while True:
self.eat_whitespace()
m = self.get_token('\$\(([-a-z]+)\)')
m = self.get_token(r'\$\(([-a-z]+)\)')
if m is not None:
c.add_type("string", m.group(1));
elif self.get_token('\$'):
c.add_type("string");
elif self.get_token('\#'):
c.add_type("number");
elif self.get_token('B'):
c.add_type("boolean");
elif self.get_token('\~'):
c.add_type("null");
elif self.get_token('\{'):
self.parse_object(c)
elif self.get_token('\['):
self.parse_array(c)
else:
break
m = self.get_token(r'\$\/((?:\\.|[^\/])*)/')
if m is not None:
print("Is a pattern")
c.add_type("string", pattern = m.group(1))
elif self.get_token(r'\$'):
c.add_type("string");
elif self.get_token(r'\#'):
c.add_type("number");
elif self.get_token(r'B'):
c.add_type("boolean");
elif self.get_token(r'\~'):
c.add_type("null");
elif self.get_token(r'\{'):
self.parse_object(c)
elif self.get_token(r'\['):
self.parse_array(c)
else:
break
def parse_array(self, x):
......@@ -113,7 +118,7 @@ class Parser:
self.parse_type_definition(c)
self.eat_whitespace()
if self.get_token('\]'):
if self.get_token(r'\]'):
return
raise ParseError("Unexpected input: " + self.s[self.p:])
......@@ -126,12 +131,17 @@ class SchemaComponent:
self.items = None
self.base_type = None
self.format = None
self.pattern = None
self.additional_properties = False
def add_type(self, t, f = None):
def add_type(self, t, format = None, pattern = None):
self.types.append(t)
# XXX format is applied to all possible types for the component
self.format = f
# XXX format and pattern are applied to all possible types for the
# component
if format is not None:
self.format = format
if pattern is not None:
self.pattern = pattern
def add_property(self, n, c):
self.properties[n] = c
......@@ -156,6 +166,9 @@ class SchemaComponent:
schema["type"] = self.types
if self.format is not None:
schema["format"] = self.format
if self.pattern is not None:
schema["pattern"] = self.pattern
required = []
for n, c in self.properties.items():
if n[0] != '/':
......
......@@ -286,6 +286,36 @@ def test_short_schema():
{},
]
},
# Patterns
{
"input": """
{
"p":$/f.o/#,
}
""",
"output": {
"type": "object",
"properties": {
"p": {
"type": ["string", "number"],
"pattern": "f.o"
},
},
"required": [ "p" ],
"additionalProperties": False
},
"valid_instances": [
{ "p": "foo" },
{ "p": "fxo" },
{ "p": 37 },
],
"invalid_instances": [
{
"p": "bar"
},
]
},
]
for t in tests:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment