-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_schema.py
More file actions
55 lines (48 loc) · 1.92 KB
/
json_schema.py
File metadata and controls
55 lines (48 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from jsonschema import validate, ValidationError
from content_api.util import get
from dateutil.parser import parse as parse_date
def validate_schema(instance, schema):
try:
validate(instance=instance, schema=schema)
return None
except ValidationError as schema_error:
return schema_error
def coerce_value(value, value_schema):
value_type = get(value_schema, 'type')
if not value_type or value is None:
return value
try:
if value_type == 'integer' or (value_type == 'number' and '.' not in value):
return int(value)
elif value_type == 'number' and '.' in value:
return float(value)
elif value_type == 'boolean':
return value not in ['0', 'false', 'FALSE', 'f']
elif value_type == 'string' and get(value_schema, 'format') == 'date-time':
return parse_date(value)
else:
return value
except:
return value
def coerce_values(values, schema):
return {k: coerce_value(v, get(schema, f'properties.{k}')) for k, v in values.items()}
def is_writable(property):
return get(property, 'x-meta.writable') != False
def writable_schema(schema):
if not schema or not 'properties' in schema:
return schema
properties = {key: property for key, property in schema['properties'].items() if is_writable(property)}
get_required = lambda required: [key for key in required if key in properties]
return {
**schema,
'properties': properties,
'required': (schema['required'] and get_required(schema['required']))
}
def writable_doc(schema, doc):
if not doc or not schema or not 'properties' in schema:
return doc
unwritable_keys = [k for k, v in schema['properties'].items() if not is_writable(v)]
return {k: v for k, v in doc.items() if k not in unwritable_keys}
def schema_error_response(schema_error):
body = {'error': {'message': schema_error.message, 'path': list(schema_error.path), 'schema': schema_error.schema}}
return {'status': 400, 'body': body}