import os
from jsonschema import validators, Draft4Validator
schema = {
'type': 'file'
}
checker = Draft4Validator.TYPE_CHECKER.redefine("file", lambda _, value: os.path.exist(value))
Validator = validators.extend(Draft4Validator, type_checker=checker)
Validator.check_schema(schema)
Traceback (most recent call last):
File "test_schema.py", line 24, in test_combination
Validator.check_schema(schema)
File "C:\python\lib\site-packages\jsonschema\validators.py", line 235, in check_schema
raise SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: 'file' is not valid under any of the given schemas
Failed validating 'anyOf' in metaschema['properties']['type']:
{'anyOf': [{'$ref': '#/definitions/simpleTypes'},
{'items': {'$ref': '#/definitions/simpleTypes'},
'minItems': 1,
'type': 'array',
'uniqueItems': True}]}
--
You received this message because you are subscribed to the Google Groups "jsonschema - An implementation of JSON Schema for Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema+...@googlegroups.com.
To post to this group, send email to jsons...@googlegroups.com.
from os.path import exists
from jsonschema import validators, Draft4Validator
schema = {'type': 'file'}
Draft4Validator.META_SCHEMA['definitions']['simpleTypes']['enum'].append('file')
checker = Draft4Validator.TYPE_CHECKER.redefine("file", lambda _, value: exists(value))
Validator = validators.extend(Draft4Validator, type_checker=checker)
Validator.check_schema(schema)
validator = Validator(schema=schema)
validator.validate('real.txt')
validator.validate('fake.txt')
Traceback (most recent call last):
File "test_schema.py", line 21, in test_combination
validator.validate('fake.txt')
File "C:\python\lib\site-packages\jsonschema\validators.py", line 282, in validate
raise error
jsonschema.exceptions.ValidationError: 'fake.txt' is not of type 'file'
Failed validating 'type' in schema:
{'type': 'file'}
On instance:
'fake.txt'
from os.path import existsimport jsonschema
def f(checker, instance): return exists(instance)jsonschema.Draft6Validator.META_SCHEMA['definitions']['simpleTypes']['enum'].append('path')checker = jsonschema.Draft6Validator.TYPE_CHECKER.redefine("path", f)
myValidator = validators.extend(jsonschema.Draft6Validator, type_checker=checker)pvalidator = myValidator(schema={'type': 'path'})schema = _read_schema("/path/to/schema.yaml") jsonschema.validate(p2, schema, cls= pvalidator)
TypeError: 'Validator' object is not callable