def test_nested_array(): s = { 'type': 'object', 'properties': { 'stuff': { 'type': 'array', 'items': { 'type': 'array', 'items': [ { 'type': 'integer' }, { 'type': 'string' }, { 'type': 'number' }, ], 'minItems': 3, 'maxItems': 3 } } } } good = dict(stuff=[[1, 'hello', 2], [4, 'world', 9.7]]) schema.validate(good, schema=s) bads = [ dict(stuff=[[1, 2, 3]]), dict(stuff=[12,'dldl']), dict(stuff=[[12, 'dldl']]), dict(stuff=[[1, 'hello', 2], [4, 5]]), dict(stuff=[[1, 'hello', 2], [4, 5, 6]]) ] for b in bads: with pytest.raises(ValidationError): schema.validate(b, schema=s)
def _validate(self): instance = yamlutil.custom_tree_to_tagged_tree( self._instance, self._ctx._asdf) try: previously_invalid = self._ctx._has_invalid_values except AttributeError: previously_invalid = False try: schema.validate(instance, schema=self._schema) self._ctx._has_invalid_values = False except jsonschema.ValidationError: self._ctx._has_invalid_values = True if previously_invalid or self._ctx._pass_invalid_values: pass else: raise
def callback(schema, path, combiner, ctx, recurse): result = None if 'fits_keyword' in schema: fits_keyword = schema['fits_keyword'] result = _fits_keyword_loader( hdulist, fits_keyword, schema, ctx.get('hdu_index'), known_keywords) if result is not None: temp_schema = { '$schema': 'http://stsci.edu/schemas/asdf-schema/0.1.0/asdf-schema'} temp_schema.update(schema) try: asdf_schema.validate(result, schema=temp_schema) except jsonschema.ValidationError: if validate: raise else: msgfmt = "'{0}' is not valid in keyword '{1}'" log.warning(msgfmt.format(result, fits_keyword)) if pass_invalid_values: properties.put_value(path, result, tree) else: properties.put_value(path, result, tree) elif 'fits_hdu' in schema and ( 'max_ndim' in schema or 'ndim' in schema or 'datatype' in schema): result = _fits_array_loader( hdulist, schema, ctx.get('hdu_index'), known_datas) if result is not None: temp_schema = { '$schema': 'http://stsci.edu/schemas/asdf-schema/0.1.0/asdf-schema'} temp_schema.update(schema) asdf_schema.validate(result, schema=temp_schema) properties.put_value(path, result, tree) if schema.get('type') == 'array': has_fits_hdu = _schema_has_fits_hdu(schema) if has_fits_hdu: for i in range(len(hdulist)): recurse(schema['items'], path + [i], combiner, {'hdu_index': i}) return True
def test_nested_array(): s = { 'type': 'object', 'properties': { 'stuff': { 'type': 'array', 'items': { 'type': 'array', 'items': [ { 'type': 'integer' }, { 'type': 'string' }, { 'type': 'number' }, ], 'minItems': 3, 'maxItems': 3 } } } } good = dict(stuff=[[1, 'hello', 2], [4, 'world', 9.7]]) schema.validate(good, schema=s) bads = [ dict(stuff=[[1, 2, 3]]), dict(stuff=[12, 'dldl']), dict(stuff=[[12, 'dldl']]), dict(stuff=[[1, 'hello', 2], [4, 5]]), dict(stuff=[[1, 'hello', 2], [4, 5, 6]]) ] for b in bads: with pytest.raises(ValidationError): schema.validate(b, schema=s)
def test_time_tag(): schema = asdf_schema.load_schema( 'http://stsci.edu/schemas/asdf/time/time-1.1.0', resolve_references=True) schema = _flatten_combiners(schema) date = time.Time(datetime.datetime.now()) tree = {'date': date} asdf = AsdfFile(tree=tree) instance = yamlutil.custom_tree_to_tagged_tree(tree['date'], asdf) asdf_schema.validate(instance, schema=schema) tag = 'tag:stsci.edu:asdf/time/time-1.1.0' date = tagged.tag_object(tag, date) tree = {'date': date} asdf = AsdfFile(tree=tree) instance = yamlutil.custom_tree_to_tagged_tree(tree['date'], asdf) asdf_schema.validate(instance, schema=schema)
def test_nested_array_yaml(tmpdir): schema_def = """ %YAML 1.1 --- type: object properties: stuff: type: array items: type: array items: - type: integer - type: string - type: number minItems: 3 maxItems: 3 ... """ schema_path = tmpdir.join('nested.yaml') schema_path.write(schema_def.encode()) schema_tree = schema.load_schema(str(schema_path)) schema.check_schema(schema_tree) good = dict(stuff=[[1, 'hello', 2], [4, 'world', 9.7]]) schema.validate(good, schema=schema_tree) bads = [ dict(stuff=[[1, 2, 3]]), dict(stuff=[12,'dldl']), dict(stuff=[[12, 'dldl']]), dict(stuff=[[1, 'hello', 2], [4, 5]]), dict(stuff=[[1, 'hello', 2], [4, 5, 6]]) ] for b in bads: with pytest.raises(ValidationError): schema.validate(b, schema=schema_tree)
def test_nested_array_yaml(tmpdir): schema_def = """ %YAML 1.1 --- type: object properties: stuff: type: array items: type: array items: - type: integer - type: string - type: number minItems: 3 maxItems: 3 ... """ schema_path = tmpdir.join('nested.yaml') schema_path.write(schema_def.encode()) schema_tree = schema.load_schema(str(schema_path)) schema.check_schema(schema_tree) good = dict(stuff=[[1, 'hello', 2], [4, 'world', 9.7]]) schema.validate(good, schema=schema_tree) bads = [ dict(stuff=[[1, 2, 3]]), dict(stuff=[12, 'dldl']), dict(stuff=[[12, 'dldl']]), dict(stuff=[[1, 'hello', 2], [4, 5]]), dict(stuff=[[1, 'hello', 2], [4, 5, 6]]) ] for b in bads: with pytest.raises(ValidationError): schema.validate(b, schema=schema_tree)
def test_load_schema_with_asdf_uri_scheme(): subschema_content = """%YAML 1.1 --- $schema: http://stsci.edu/schemas/asdf/asdf-schema-1.0.0 id: asdf://somewhere.org/schemas/bar bar: type: string ... """ content = """%YAML 1.1 --- $schema: http://stsci.edu/schemas/asdf/asdf-schema-1.0.0 id: asdf://somewhere.org/schemas/foo definitions: local_bar: type: string type: object properties: bar: $ref: asdf://somewhere.org/schemas/bar#/bar local_bar: $ref: '#/definitions/local_bar' ... """ with asdf.config_context() as config: config.add_resource_mapping( {"asdf://somewhere.org/schemas/foo": content}) config.add_resource_mapping( {"asdf://somewhere.org/schemas/bar": subschema_content}) schema_tree = schema.load_schema("asdf://somewhere.org/schemas/foo") instance = {"bar": "baz", "local_bar": "foz"} schema.validate(instance, schema=schema_tree) with pytest.raises(ValidationError): schema.validate({"bar": 12}, schema=schema_tree)
def test_tag_validator(): content = """%YAML 1.1 --- $schema: http://stsci.edu/schemas/asdf/asdf-schema-1.0.0 id: asdf://somewhere.org/schemas/foo tag: asdf://somewhere.org/tags/foo ... """ with asdf.config_context() as config: config.add_resource_mapping( {"asdf://somewhere.org/schemas/foo": content}) schema_tree = schema.load_schema("asdf://somewhere.org/schemas/foo") instance = tagged.TaggedDict(tag="asdf://somewhere.org/tags/foo") schema.validate(instance, schema=schema_tree) with pytest.raises(ValidationError): schema.validate( tagged.TaggedDict(tag="asdf://somewhere.org/tags/bar"), schema=schema_tree) content = """%YAML 1.1 --- $schema: http://stsci.edu/schemas/asdf/asdf-schema-1.0.0 id: asdf://somewhere.org/schemas/bar tag: asdf://somewhere.org/tags/bar-* ... """ with asdf.config_context() as config: config.add_resource_mapping( {"asdf://somewhere.org/schemas/bar": content}) schema_tree = schema.load_schema("asdf://somewhere.org/schemas/bar") instance = tagged.TaggedDict(tag="asdf://somewhere.org/tags/bar-2.5") schema.validate(instance, schema=schema_tree) with pytest.raises(ValidationError): schema.validate( tagged.TaggedDict(tag="asdf://somewhere.org/tags/foo-1.0"), schema=schema_tree)
def _validate(self): instance = yamlutil.custom_tree_to_tagged_tree(self._instance, self._ctx._asdf) schema.validate(instance, schema=self._schema)
def _validate(self): instance = yamlutil.custom_tree_to_tagged_tree( self._instance, self._ctx._asdf) schema.validate( instance, schema=self._schema)