Commit 5c186232 authored by Paul Warren's avatar Paul Warren

Support for formats.

parent c79e011e
......@@ -101,6 +101,8 @@ e.g.
}
```
### Scalar types
Other simple types:
```
......@@ -111,7 +113,7 @@ Other simple types:
}
```
Objects:
### Objects
```
{
......@@ -123,14 +125,22 @@ Objects:
}
```
Arrays:
### Arrays
```
{
arrayOfNumbers: [#]
arrayOfNumbers: [#],
arrayOfStringsOrNumbers:[$#]
}
```
### Formats
```
{
dateProperty:$(date)
}
## Defaults
In Short JSON Schema, properties are required by default, and additional
......
......@@ -88,7 +88,7 @@ class Parser:
self.eat_whitespace()
m = self.get_token('\$\(([-a-z]+)\)')
if m is not None:
c.add_type(m.group(1));
c.add_type("string", m.group(1));
elif self.get_token('\$'):
c.add_type("string");
elif self.get_token('\#'):
......@@ -125,10 +125,13 @@ class SchemaComponent:
self.optional = []
self.items = None
self.base_type = None
self.format = None
self.additional_properties = False
def add_type(self, t):
def add_type(self, t, f = None):
self.types.append(t)
# XXX format is applied to all possible types for the component
self.format = f
def add_property(self, n, c):
self.properties[n] = c
......@@ -151,6 +154,8 @@ class SchemaComponent:
schema["type"] = self.types[0]
elif len(self.types) > 1:
schema["type"] = self.types
if self.format is not None:
schema["format"] = self.format
required = []
for n, c in self.properties.items():
if n[0] != '/':
......
......@@ -228,7 +228,7 @@ def test_short_schema():
{
nullThing?:~, -- nullable
booleanThing?:B, -- boolean
-- emailThing?:$(email)
dateThing?:$(date)
}
""",
"output": {
......@@ -240,18 +240,20 @@ def test_short_schema():
"booleanThing": {
"type": "boolean",
},
# "emailThing": {
# "type": "email",
# }
"dateThing": {
"type": "string",
"format": "date"
}
},
"additionalProperties": False
},
"valid_instances": [
{ "nullThing": None, "booleanThing": True },
{ "nullThing": None, "booleanThing": True, "dateThing": "2019-01-01" },
],
"invalid_instances": [
{ "nullThing": False, "booleanThing": True },
{ "nullThing": False, "booleanThing": None },
{ "nullThing": None, "booleanThing": True, "dateThing": "abc" },
]
},
......@@ -294,7 +296,7 @@ def test_short_schema():
except jsonschema.SchemaError:
pytest.fail("Schema error parsing" + repr(s))
sv = jsonschema.Draft4Validator(s)
sv = jsonschema.Draft4Validator(s, format_checker = jsonschema.FormatChecker())
for i in t.get("valid_instances",{}):
try:
sv.validate(i)
......
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