Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

additional schema keywords #17

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions aptos/parser.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from .primitive import Creator
from .visitor import ResolveVisitor
from collections import MutableMapping
from .primitive import Creator, Component


class Parser:

@staticmethod
def parse(schema):
def parse(schema: MutableMapping) -> Component:
raise NotImplementedError()


class SchemaParser(Parser):

@staticmethod
def parse(schema):
def parse(schema: MutableMapping) -> Component:
if not isinstance(schema, MutableMapping):
raise ValueError('schema is not a mapping type')
component = Creator.create(schema.get('type')).unmarshal(schema)
component.accept(ResolveVisitor(schema))
return component
9 changes: 7 additions & 2 deletions aptos/primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class Primitive(Component):

def __init__(self, enum=None, const=None, type=None, allOf=None,
anyOf=None, oneOf=None, definitions=None, title='',
description='', default=None, examples=None, **kwargs):
description='', default=None, examples=None,
format='', **kwargs):
self.enum = [] if enum is None else list(set(enum))
self.const = const
self.type = type
Expand All @@ -86,6 +87,7 @@ def __init__(self, enum=None, const=None, type=None, allOf=None,
self.description = description
self.default = default
self.examples = [] if examples is None else list(examples)
self.format = format

@classmethod
def unmarshal(cls, schema):
Expand Down Expand Up @@ -157,11 +159,14 @@ def accept(self, visitor, *args):

class String(Primitive):

def __init__(self, maxLength=0, minLength=0, pattern='', **kwargs):
def __init__(self, maxLength=0, minLength=0, pattern='',
contentEncoding='', contentMediaType='', **kwargs):
super().__init__(**kwargs)
self.maxLength = maxLength
self.minLength = minLength
self.pattern = pattern
self.contentEncoding = contentEncoding
self.contentMediaType = contentMediaType

def accept(self, visitor, *args):
return visitor.visit_string(self, *args)
Expand Down
13 changes: 13 additions & 0 deletions aptos/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def visit_one_of(self, one_of, *args):
errors = handler(one_of, *args)
assert len(errors) == 1, ', '.join(errors)

def visit_format(self, primitive, *args):
"""The `format`_ keyword does not require an implementation

.. _format:
http://json-schema.org/latest/json-schema-validation.html#format
"""
raise NotImplementedError()

def visit_primitive(self, primitive, *args):
instance = self.instance
if primitive.const is not None:
Expand All @@ -60,6 +68,11 @@ def visit_primitive(self, primitive, *args):
primitive.allOf.accept(self, *args)
primitive.anyOf.accept(self, *args)
primitive.oneOf.accept(self, *args)
try:
if primitive.format != '':
self.visit_format(primitive, *args)
except NotImplementedError:
pass

def visit_boolean(self, boolean, *args):
self.visit_primitive(boolean, *args)
Expand Down