本文整理汇总了Python中jsonschema.Draft3Validator类的典型用法代码示例。如果您正苦于以下问题:Python Draft3Validator类的具体用法?Python Draft3Validator怎么用?Python Draft3Validator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Draft3Validator类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate
def validate(self):
try:
Draft3Validator.check_schema(self.schema)
except SchemaError as err:
return 'schema is invalid: {}'.format(err)
return None
示例2: test_schema_valid
def test_schema_valid(self):
"""
The schema itself is a valid Draft 3 schema
"""
Draft3Validator.check_schema(rest_schemas.create_group_request)
示例3: test_0
def test_0(self):
"""The schema is valid"""
Draft3Validator.check_schema(self.widget.schema)
示例4: open
# Loading example files
client_example_file = open(WORKSPACE_DIR + "schema/communication/examples/client-message-example.json")
server_example_file = open(WORKSPACE_DIR + "schema/communication/examples/server-message-example.json")
client_configuration_example_file = open(WORKSPACE_DIR + "schema/configuration/examples/client-configuration-example.json")
client_logging_example_file = open(WORKSPACE_DIR + "schema/communication/examples/client-logging-example.json")
# Loading into JSON
client_schema = json.load(client_schema_file)
server_schema = json.load(server_schema_file)
client_configuration_schema = json.load(client_configuration_schema_file)
client_logging_schema = json.load(client_logging_schema_file)
client_example = json.load(client_example_file)
server_example = json.load(server_example_file)
client_configuration_example = json.load(client_configuration_example_file)
client_logging_example = json.load(client_logging_example_file)
# Running verification
logging.info("Testing schemes for compliance against the JSON Schema format")
Draft3Validator.check_schema(client_schema)
Draft3Validator.check_schema(server_schema)
Draft3Validator.check_schema(client_configuration_schema)
Draft3Validator.check_schema(client_logging_schema)
logging.info("Testing example files for compliance against respective schemes")
validate(client_example, client_schema)
validate(server_example, server_schema)
validate(client_configuration_example, client_configuration_schema)
validate(client_logging_example, client_logging_schema)
示例5:
# How do I validate a JSON Schema schema, in Python?
from jsonschema import Draft3Validator
my_schema = json.loads(my_text_file) #or however else you end up with a dict of the schema
Draft3Validator.check_schema(schema)
示例6: ValueError
msg = " check effect %s ... " % filename
try:
effect = json.loads(f.read())
script = path.basename(effect['script'])
if not path.exists(jsonFiles+'/'+script):
raise ValueError('script file: '+script+' not found.')
schema = path.splitext(script)[0]+'.schema.json'
if not path.exists(jsonFiles+'/schema/'+schema):
raise ValueError('schema file: '+schema+' not found.')
schema = jsonFiles+'/schema/'+schema
# validate against schema
with open(schema) as s:
effectSchema = json.loads(s.read())
Draft3Validator.check_schema(effectSchema)
validator = Draft3Validator(effectSchema)
baseValidator.validate(effect)
validator.validate(effect['args'])
#print(msg + "ok")
except Exception as e:
print(msg + 'error ('+str(e)+')')
errors += 1
retval = 1
print(" checked effect files: %s success: %s errors: %s" % (total,(total-errors),errors))
sys.exit(retval)
示例7: validate
def validate(nbjson):
"""Checks whether the given notebook JSON conforms to the current
notebook format schema, and returns the list of errors.
"""
# load the schema file
with open(schema_path, 'r') as fh:
schema_json = json.load(fh)
# resolve internal references
schema = resolve_ref(schema_json)
schema = jsonpointer.resolve_pointer(schema, '/notebook')
# count how many errors there are
v = Validator(schema)
errors = list(v.iter_errors(nbjson))
return errors
示例8: __init__
def __init__(self, name, mode="pass", schema=''):
Actor.__init__(self, name)
self.name=name
self.mode=mode
self.schema=schema
if mode == "decode":
self.convert = self.__loads
elif mode == "encode":
self.convert = self.__dumps
elif mode == "pass":
self.convert = self.__pass
else:
raise Exception ("mode should be either 'encode' or 'decode'.")
if schema != "":
self.logging.debug("Validation schema defined. Doing validation.")
schema_data = self.__loadValidationSchema(schema)
self.validate = self.__validate
self.validator=Validator(schema_data)
else:
self.logging.debug("No validation schema defined. No validation.")
self.validate = self.__noValidate
示例9: JSON
class JSON(Actor):
'''**A Wishbone module which converts and validates JSON.**
This module has 2 main modes:
- Validate JSON data and convert into a Python data structure.
- Convert a Python data structure into a JSON string.
Parameters:
- name (str): The instance name when initiated.
- mode (str): Determines whether the input has to be encoded, decoded or
passed through.
Can have 3 values: "encode", "decode", "pass"
Default: pass
- schema (str): The filename of the JSON validation schema to load. When no
schema is defined no validation is done.
Default: ''
Queues:
- inbox: Incoming events.
- outbox: Outgoing events.
Data which cannot be converted or which fails the validation is purged.
The schema should be in valid JSON syntax notation. JSON validation can
only be done on Python objects so you will have to convert your any JSON
data to a Python object first.
'''
def __init__(self, name, mode="pass", schema=''):
Actor.__init__(self, name)
self.name=name
self.mode=mode
self.schema=schema
if mode == "decode":
self.convert = self.__loads
elif mode == "encode":
self.convert = self.__dumps
elif mode == "pass":
self.convert = self.__pass
else:
raise Exception ("mode should be either 'encode' or 'decode'.")
if schema != "":
self.logging.debug("Validation schema defined. Doing validation.")
schema_data = self.__loadValidationSchema(schema)
self.validate = self.__validate
self.validator=Validator(schema_data)
else:
self.logging.debug("No validation schema defined. No validation.")
self.validate = self.__noValidate
def consume(self, event):
try:
event["data"] = self.convert(event["data"])
except Exception as err:
self.logging.warn("Unable to convert incoming data. Purged. Reason: %s"%(err))
return
try:
self.validate(event["data"])
except ValidationError as err:
self.logging.warn("JSON data does not pass the validation schema. Purged. Reason: %s"%(str(err).replace("\n"," > ")))
return
try:
self.queuepool.outbox.put(event)
except QueueLocked:
self.queuepool.inbox.rescue(event)
self.queuepool.outbox.waitUntilPutAllowed()
def __loadValidationSchema(self, path):
with open(path,'r') as schema:
data = ''.join(schema.readlines())
print loads(data)
return loads(data)
def __loads(self, data):
return loads(data)
def __dumps(self, data):
return dumps(data)
def __pass(self, data):
return data
def __validate(self, data):
return self.validator.validate(data)
def __noValidate(self, data):
#.........这里部分代码省略.........