def test_3(self): """ The schema is not altered if the required condition is only client side. """ schema = Schema({'type': 'object', 'properties': {'normal': Schema({'type': 'string'})}}) schema.apply_func(self.validator_conditional.alter_schema) assert_not_in('required', schema)
def test_0(self): """ When applied to an existing schema, an empty schema is returned. """ schema = Schema({'type': 'object', 'properties': {'normal': Schema({'type': 'string'})}, 'required': ['normal']}) schema.apply_func(self.validator.alter_schema) assert_equal(len(schema), 0)
def test(self): """ Modify the type of the leaf object. """ schema = Schema( {"type": "object", "properties": {"normal": Schema({"type": "string"})}, "required": ["normal"]} ) schema.apply_func(self.validator.alter_schema) assert_equal("integer", schema["properties"]["normal"]["type"])
def test_2(self): """ Alter the schema by adding a required field to the schema for each named properties. """ schema = Schema({'type': 'object', 'properties': {'normal': Schema({'type': 'string'})}}) schema.apply_func(self.validator_default.alter_schema) assert_in('required', schema) assert_equal(schema['required'], ['normal'])
def test_4(self): """ The value of the pattern in the schema is not modified. If a schema is altered by the validator, only the leafs ared affected by the modifications. """ schema = Schema({'type': 'object', 'properties': {'normal': Schema({'type': 'string'})}, 'required': ['normal']}) schema.apply_func(self.validator.alter_schema) assert_in('[A-Z]{4}', schema['properties']['normal']['pattern'])
def test_3(self): """ Insert a pattern to the leaf objects. If a schema is altered by the validator, only the leafs ared affected by the modifications. """ schema = Schema({'type': 'object', 'properties': {'normal': Schema({'type': 'string'})}, 'required': ['normal']}) schema.apply_func(self.validator.alter_schema) assert_in('pattern', schema['properties']['normal'])
def __init__(self, bind, name=None, label=None, description=None, html_attributes=None, modifiers=None, **kwargs): # increment the instance counter self.instance_counter = Widget.instance_counter Widget.instance_counter += 1 self.html_attributes = HTMLAttrs() self.schema = Schema() self.modifiers = [] # default properties self.bind = bind # is an HTML attribute (see properties) self.name = name # is an HTML attribute (see properties) self.label = label self.description = description if self.atomic_schema is not None: self.schema[bind] = copy.deepcopy(self.atomic_schema) self._apply_customize(**kwargs) # final user's stuff override everything else if modifiers is not None: self.modifiers += modifiers if html_attributes is not None: self.html_attributes.update(html_attributes) self.apply_modifiers()
def _init_schema(self, schema, fqn): """ Combine the information from the `schema` attribute of the class and from `bind` property of the `Widget` instance to compute a validating JSON schema. """ schema.update(Schema({'type': 'object', 'properties': {}})) # convert a nested object to nested schemas. parent = schema for child in self.bind.split('.')[:-1]: new = Schema({'type': 'object', 'properties': {}}) parent['properties'][child] = new parent = new last = self.bind.split('.')[-1] parent['properties'][last] = Schema(self.__class__.schema) return True
def _schema(self): """ On the fly schema computation """ schema = Schema() schema.apply_func(self._init_schema) # Apply modifiers for modifier in self.modifiers: if hasattr(modifier, 'alter_schema'): schema.apply_func(modifier.alter_schema) return schema.compile()
class TestPatternProperty(object): """ Tests are done with a schema having a variable with 'parent.child' as FQN; """ def setup(self): self.schema = Schema({'type': 'object', 'properties': {'parent': Schema({'type': 'object', 'properties': {'child': Schema({'type': 'string'})}})}}) def test_0(self): """PatternProperty does not set attributes.""" assert_equal(PatternProperty().attributes, {}) def test_1(self): """ The FQN redefinition can be partial and will not raise an exception. """ validator = PatternProperty(r'^[A-Z]{3}') self.schema.apply_func(validator.alter_schema) def test_2(self): """ The FQN redefinition add an asPatternProperty field to the object if the argument does not match the FQN. """ validator = PatternProperty(r'^[A-Z]{3}') self.schema.apply_func(validator.alter_schema) assert_in('asPatternProperty', self.schema['properties']['parent']) def test_3(self): """ The FQN redefinition set the asPatternProperty field value to the value of the argument if it does not match the FQN. """ validator = PatternProperty(r'^[A-Z]{3}') self.schema.apply_func(validator.alter_schema) assert_equal(self.schema['properties']['parent']['asPatternProperty'], r'^[A-Z]{3}') def test_4(self): """ If the FQN is not redefined, no modification is done. """ validator = PatternProperty('parent') self.schema.apply_func(validator.alter_schema) assert_not_in('asPatternProperty', self.schema['properties']['parent'])
def alter_schema(self, schema, fqn): if schema[u'type'] != u'object': # some properties must be moved from the array object to the item # object : items = Schema(schema) # clean some uneeded value in items for k in items.keys(): if k in ['asPatternProperty', 'required']: del items[k] # delete migrated values for k in schema.keys(): if k in items: del schema[k] schema[u'type'] = u'array' schema[u'items'] = items if self.min_items is not None: schema[u'minItems'] = self.min_items if self.max_items is not None: schema[u'maxItems'] = self.max_items
def schema_merger(*schemas): """ Merge multiple schemas in a new-one. """ res = Schema(description='Generated with Flask-Triangle') for schema in schemas: for k, v in schema: if k is not None and k not in res and isinstance(v, Object): res[k] = Object() elif not isinstance(v, Object): res[k] = copy.deepcopy(v) if hasattr(v, 'required') and k is not None: res[k].required += [prop for prop in v.required if prop not in res[k].required] elif hasattr(v, 'required'): res.required += [prop for prop in v.required if prop not in res.required] return res
def setup(self): self.schema = Schema({'type': 'object', 'properties': {'parent': Schema({'type': 'object', 'properties': {'child': Schema({'type': 'string'})}})}})