def test_additionalItems_pass(self): data = [12482, "Yes, more strings", False, ["I'm"], {"also": "allowed!"}] try: validictory.validate(data, self.schema2) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def test_format_utcmillisec_pass(self): data = 1294915735 try: validictory.validate(data, self.schema_utcmillisec) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def validate(value, **kwds): """ Validate a python value against json schema: validate(value, schemaPath) validate(value, schemaDict) value: python object to validate against the schema The json schema may be specified either as a path of the file containing the json schema or as a python dictionary using one of the following keywords as arguments: schemaPath: Path of file containing the json schema object. schemaDict: Python dictionary containing the json schema object Returns: nothing Raises: ValidationError when value fails json validation """ assert len(kwds.keys()) >= 1 assert 'schemaPath' in kwds or 'schemaDict' in kwds schemaDict = None if 'schemaPath' in kwds: schemaPath = kwds.pop('schemaPath') schemaDict = loadJsonValueFromFile(schemaPath) elif 'schemaDict' in kwds: schemaDict = kwds.pop('schemaDict') try: validictory.validate(value, schemaDict, **kwds) except validictory.ValidationError as e: raise ValidationError(e)
def test_uniqueitems_pass_not_an_array(self): data = 13 # it's pretty unique try: validictory.validate(data, self.schema) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def test_pattern_pass_nonstring(self): data = 123 try: validictory.validate(data, self.schema) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def validate(dictionary, schema_id): """ Validate a dictionary using a schema. !! DEPRECATED !! Always return True. It just log call stack to var/log/schema.log :param dictionary: Dictionary to validate. :type dictionary: dict :param schema_id: Schema identifier (value of _id field in Mongo document). :type schema_id: str :returns: True if the validation succeed, False otherwise. WARNING: disabled, always returns True. """ call_stack = ''.join(traceback.format_stack()) schema_logger.critical( 'call to canopsis.schema.__init__.validate: {}\n'.format(call_stack)) # FIXIT: Look at the code under this one: we just avoid useless computation # while we completely remove calls to this function. return True schema = get(schema_id) try: validictory.validate(dictionary, schema, required_by_default=False) return True except validictory.ValidationError: return True
def update_schedule(username, sid, mongodb): ''' Input: json schedule document with updates Output: Status 204 No Content, and Location with the schedule id Checks for valid user session. If the session checks out, the schedule is updated with the new values in the request. If the session is not valid, status 401 Unauthorized is returned. ''' # Check session cookie. Returns username if matched; otherwise, None. session_user = request.get_cookie('session', secret=secret_key) if session_user: # Do your thing, man. user = mongodb.users.find_one({'username': username}, {'_id': 1}) if user: if session_user in [username, 'admin']: try: # Validate json data from request. validictory.validate(request.json, schedule_schema) # Update schedule if 'courses' not in request.json.keys(): # Clears all courses from schedule document if courses is # not in the json object in the request. request.json['courses'] = [] mongodb.schedules.update({'_id': ObjectId(sid)}, {'$set': request.json}) except ValueError, error: # Return 400 status and error from validation. return HTTPResponse(status=400, output=error) response.status = 204 response.headers['location'] = '/api/users/%s/schedules/%s' % (username, sid) return else: return HTTPResponse(status=401, output="You do not have permission.") else: return HTTPResponse(status=401, output="Not a valid user.")
def test_uniqueitems_pass(self): data = [1,2,3] try: validictory.validate(data, self.schema) except ValueError, e: self.fail("Unexpected failure: %s" % e)
def querystr_tuple_to_query(querystr, qtype, dsetname, engine, prev_qsid=None): """ Takes the query given in the parameters and converts it to a dictionary. Arguments: querystr: query string as sent by the frontend. qtype: query type. This value determines whether querystr corresponds to just the string in the query or if it also encodes more information. dsetname: dataset name engine: engine name prev_qsid: previous query id. Only used when refining a previous query. Returns: The query in dictionary form. It will rise a ValueError exception if the dictionary structure does not fit json_schema.query_schema. """ query = {'qtype': qtype, 'dsetname': dsetname} if qtype == models.opts.Qtypes.text: query['qdef'] = querystr else: query['qdef'] = decode_image_querystr(querystr) if engine: query['engine'] = engine # store optional previous query session id param if it was provided if prev_qsid: query['prev_qsid'] = prev_qsid # validate converted input just in case... validictory.validate(query, models.json_schema.query_schema) return query
def test_items_descriptive_fail(self): data = [1294] try: validictory.validate(data, self.schema1) except ValueError as e: # warning should mention list item, not _data assert 'list item' in str(e)
def test_additionalItems_false_no_additional_items_pass(self): data = [12482, "Yes, more strings", False] try: validictory.validate(data, self.schema1) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def validate_config(config_instance, config_schema=None): """ Validates a configuration dict against a JSON schema. Args: config_instance (str, unicode): The configuration dictionary instance. config_schema (dict, optional): The configuration JSON schema in the form of a `dict`. Defaults to `None` in which case the `config_schema_default` is used. Returns: bool: `True` if `config_instance` validates against the schema. """ # Use `config_schema_default` if no schema was provided. if config_schema is None: config_schema = config_schema_default # Perform the validation of the provided configuration against the schema # and raise an exception if it fails. try: # Perform the validation. validictory.validate( data=config_instance, schema=config_schema, required_by_default=False, blank_by_default=True ) # catch any exception resulting from the validation and wrap it in the # custom `excs.ConfigFileInvalid` exception. except Exception as exc: raise excs.ConfigFileInvalid(exc.message) return True
def build_run_centro_command(centro): """ Construye un comando docker para la ejecución del container apache asociado al centro """ try: validate(centro, centro_schema) envvars = centro['envvars'] command = [] command.append("sudo docker run -d ") command.append("-p :80 ") command.append("--name " + centro['nombre'] + " ") command.append("--link " + conf.fpm_container_name + ":" + conf.fpm_container_name + " ") for var in envvars: command.append("-e " + var + "=" + envvars[var] + " ") command.append(conf.apache_container_image) command.append(" || sudo docker start " + centro['nombre']) command_str = ''.join(command) return command_str except validator.ValidationError, e: print "Alguno de los centros del fichero '" + conf.centrosfile + \ "no está bien definido" print e sys.exit(0)
def test_optional_pass(self): x = {"prop01": "test", "prop03": 1, "prop04": False} try: validictory.validate(x, self.schema) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def validate_geojson(test_geojson): geojson_types = { 'Point': point, 'MultiPoint': multipoint, 'LineString': linestring, 'MultiLineString': multilinestring, 'Polygon': polygon, 'MultiPolygon': multipolygon, 'GeometryCollection': geometrycollection, 'Feature': feature, 'FeatureCollection': featurecollection, } if not test_geojson['type'] in geojson_types: raise GeoJSONValidationException('"%s" is not a valid GeoJSON type.' % test_geojson['type']) if test_geojson['type'] in ('Feature', 'FeatureCollection', 'GeometryCollection'): # # These are special cases that every JSON schema library # I've tried doesn't seem to handle properly. # _validate_special_case(test_geojson) else: try: validictory.validate(test_geojson, geojson_types[test_geojson['type']]) except validictory.validator.ValidationError as error: raise GeoJSONValidationException(str(error)) if test_geojson['type'] == 'Polygon': # First and last coordinates must be coincident _validate_polygon(test_geojson) return
def post(self, *m, **kw): try: if self.request.path == '/settings/profile': data = { 'displayname': self.get_argument('displayname', default=''), 'avatar': self.get_argument('avatar', default='') } validate(data, self.profile_schema) self.current_user.displayname = data['displayname'] self.current_user.avatar = data['avatar'] self.session.add(self.current_user) self.session.commit() self.json_write('402') return elif self.request.path == '/settings/account': if not util.validpwd(self.get_argument('curpwd', default=''), self.current_user.pwd): self.json_write('401') return data = {'pwd': util.makepwd(self.get_argument('newpwd'))} self.current_user.pwd = data['pwd'] self.session.add(self.current_user) self.session.commit() self.json_write('400') return elif self.request.path == '/settings/message': self.um.set_meta(self.current_user.uid, 'message-accept', self.get_argument('msg', default='all')) self.json_write('404') return elif self.request.path == '/settings/email': return except ValueError: self.json_write('000')
def test_format_datetime_with_microseconds_pass(self): data = "2011-01-13T10:56:53.0438Z" try: validictory.validate(data, self.schema_datetime) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def post(self, *m, **kw): try: data = { 'target': 'wish', #默认为wish 'toid': int(self.get_argument('toid')), 'fuid': self.current_user.uid, 'op': self.get_argument('op') } wish = self.session.query(models.Wish).get(data['toid']) validate(data, self.schema) getattr(self.wg, data['op'])(data['fuid'], data['toid']) #仅在祝福时发送一条notice 并更新feed evt if (data['op'] == 'bless' and self.current_user.uid != wish.uid): self.noti.add_notice( wish.uid, { 'type': data['op'], 'fuid': self.current_user.uid, 'toid': wish.wid }) self.feed.add_to_feed_evt(uid=self.current_user.uid, content={ 'type': 'bless_w', 'actorid': self.current_user.uid, 'targetid': wish.wid }) self.json_write( code='002', data=getattr(self.wg, 'get_' + data['op'].replace('un', '') + '_count')(wish.wid)) except ValueError, e: self.json_write(code='000')
def step_args_validator(node, value): if value: try: validictory.validate(value.split(), _STEP_ARGS_VALIDICTORY_SCHEMA) except Exception as err: raise colander.Invalid(node, _(u'Got Error: ${err}', mapping=dict(err=err)))
def Process(self, doc): try: validictory.validate(doc, self.schema, required_by_default=False) except: raise default = -99999 for key in self.names_lookup.values(): if isinstance(getattr(self.my_struct, key), (float, int)): setattr(self.my_struct, key, default) # not defined, ie. default elif isinstance(getattr(self.my_struct, key), (str)): setattr(self.my_struct, key, str(-9999)) # not defined, ie. default for key, val in self.objwalk(doc): if key in self.names_lookup.keys(): setattr(self.my_struct, self.names_lookup[key], val) # check if list if len(key) > 1 and isinstance(key[-1], int) and key[0:-1] in self.names_lookup.keys(): temp = getattr(self.my_struct, self.names_lookup[key[0:-1]]) temp[key[-1]] = val setattr(self.my_struct, self.names_lookup[key[0:-1]], temp) self.t.Fill()
def test_format_ip_pass(self): valids = ["0.0.0.0", "255.255.255.255"] for ip in valids: try: validictory.validate(ip, self.schema_ip) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def test_deep_error(): schema = { 'type': 'object', 'properties': { 'foo': { 'type': 'object', 'properties': { 'bar': { 'type': 'array', 'items': { 'type': 'object', 'properties': { 'baz': { 'type': 'string', 'enum': ['a', 'b'] } } } } } } } } data = {'foo': {'bar': [{'baz': 'a'}, {'baz': 'x'}]}} try: validictory.validate(data, schema) except validictory.FieldValidationError as e: estr = str(e) assert 'baz' in estr assert 'foo' in estr assert 'bar' in estr assert '1' in estr
def test_uniqueitems_false_pass(self): data = [1, 1, 1] try: validictory.validate(data, self.schema_false) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def test_format_date_pass(self): data = "2011-01-13" try: validictory.validate(data, self.schema_date) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def test_remove_unknown_properties_pass(self): extra_data = deepcopy(self.data_simple) extra_data["sex"] = "male" validictory.validate(extra_data, self.schema_simple, remove_unknown_properties=True) self.assertEqual(extra_data, self.data_simple)
def test_additionalItems_false_no_additional_items_pass(self): data = [12482, "Yes, more strings", False] try: validictory.validate(data, self.schema1) except ValueError, e: self.fail("Unexpected failure: %s" % e)
def test_remove_unknown_properties_complex_pass(self): try: validictory.validate(self.data_complex, self.schema_complex, remove_unknown_properties=True) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def test_patternproperties_nonmatch(self): data = { 'a': True, 'd': 'foo' } try: validictory.validate(data, self.schema) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def check_local_config_schema(build): log.info("Verifying your configuration settings...") # leave this import here: might not be on sys.path in some situations import validictory local_conf_filename = build.tool_config.get('general.local_config') if local_conf_filename is not None: # explicit conf file defined if not path.isfile(local_conf_filename): raise ConfigurationError( "{file} does not exist!".format(file=local_conf_filename)) else: local_conf_filename = 'local_config.json' if not path.isfile(local_conf_filename): log.warning( "Local configuration file '{file}' does not exist!".format( file=local_conf_filename)) with open(path.join(path_to_lib(), "local_config_schema.json")) as local_conf_schema_file: local_conf_schema = json.load(local_conf_schema_file) try: validictory.validate(build.tool_config.all_config(), local_conf_schema) except validictory.validator.UnexpectedPropertyError as e: log.warning( 'Unexpected setting: "{error}". This will be ignored'.format( file=local_conf_filename, error=e)) log.info("Configuration settings check complete")
def post(): data = request.get_json() schema = {'type': 'object', 'properties': {'education_id': {'type': 'integer'}, 'name': {'type': 'string'}, 'description': {'type': 'string'}}} try: validictory.validate(data, schema) except Exception: return make_api_response(400, 'Data does not correspond to scheme.') if Education.query\ .filter(Education.id == data['education_id']).count() == 0: return make_api_response(400, 'No object has been associated with ' 'the education ID that has been ' 'specified.') if Course.query.filter(Course.name == data['name']).count() > 0: return make_api_response(400, 'There is already an object with ' 'the name that has been specified.') course = Course(data['name'], data['description']) db.session.add(course) db.session.commit() return course.to_dict(), '201 The object has been created.'
def test_pattern_pass(self): data = "*****@*****.**" try: validictory.validate(data, self.schema) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def validate_wrap(*args, **kwargs): ''' :param args: function to accept an arbitrary number of arguments. :param kwargs: function to accept an arbitrary number of keyword arguments. ''' params = {} request = None for arg in args: if type(arg) == pyramid.request.Request or type(arg) == pyramid.testing.DummyRequest: try: request = arg params = Validator.fix_types_for_schema(schema.get('properties'), arg.GET, True) if getattr(arg, 'method', 'GET') == 'POST' and len(arg.json_body) > 0: # parse request params in POST params.update(arg.json_body) except ValueError: raise EdApiHTTPPreconditionFailed('Payload cannot be parsed') except Exception as e: raise EdApiHTTPPreconditionFailed('Payload cannot be parsed') # validate params against schema try: validictory.validate(params, schema) def validated(request): ''' Return validated parameters for this request ''' return params request.set_property(validated, 'validated_params', reify=True) except Exception as e: raise EdApiHTTPPreconditionFailed(e) return request_handler(*args, **kwargs)
def validate(self, value, model_instance): super(JSONField, self).validate(value, model_instance) if self.schema: try: validictory.validate(value, self.schema) except ValueError as e: raise self.ValidationError(e)
def test_blank_true(self): try: validictory.validate("", {"blank": True}, blank_by_default=False) validictory.validate("test", {"blank": True}, blank_by_default=False) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def validate(json_object, orderly_object): """Validates the JSON object with an Orderly definition""" if type(orderly_object) in (str, unicode): orderly_object = parse(orderly_object) if type(json_object) in (str, unicode): json_object = json.loads(json_object) validictory.validate(json_object, orderly_object)
def WebSection_setObject(self, id, ob, **kw): """ Make any change related to the file uploaded. """ portal = self.getPortalObject() data = self.REQUEST.get('BODY') schema = self.WebSite_getJSONSchema() structure = json.loads(data) # 0 elementh in structure is json in json # 1 elementh is just signature structure = [json.loads(structure[0]), structure[1]] validictory.validate(structure, schema) file_name = structure[0].get('file', None) expiration_date = structure[0].get('expiration_date', None) data_set = portal.portal_catalog.getResultValue(portal_type='Data Set', reference=id) if data_set is None: data_set = portal.data_set_module.newContent(portal_type='Data Set', reference=id) data_set.publish() reference = hashlib.sha512(data).hexdigest() ob.setFilename(file_name) ob.setFollowUp(data_set.getRelativeUrl()) ob.setContentType('application/json') ob.setReference(reference) if expiration_date is not None: ob.setExpirationDate(expiration_date) ob.publish() return ob
def test_format_time_pass(self): data = "10:56:53" try: validictory.validate(data, self.schema_time) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def testComposeModelCommandResultForDeleteFail(self, repoMock, *_args): """ Make sure we can compose a model command result message for publishing a failed "deleteModel" on the AMQP exchange """ repoMock.getMetric.side_effect = Exception("getMetric should not have been called here") service = anomaly_service.AnomalyService() modelID = "123456abcdef" result = anomaly_service.ModelCommandResult( commandID="123", method="deleteModel", status=1, errorMessage="bad, bad, bad" ) msg = service._composeModelCommandResultMessage(modelID=modelID, cmdResult=result) # Validate the message against its JSON schema schemaStream = pkg_resources.resource_stream( "htmengine.runtime.json_schema", "model_command_result_amqp_message.json" ) jsonSchema = json.load(schemaStream) validictory.validate(msg, jsonSchema) self.assertEqual(msg.pop("method"), result.method) self.assertEqual(msg.pop("modelId"), modelID) self.assertEqual(msg.pop("commandId"), result.commandID) self.assertEqual(msg.pop("status"), result.status) self.assertEqual(msg.pop("errorMessage"), result.errorMessage) self.assertFalse(msg)
def test_with_properties(self): schema = { "properties": { "prop1": {"type":"integer"}, "prop2": {"type":"string"} }, "additionalProperties":{"type":["string", "number"]} } for x in [1, "test", 48, "ok", 4.9, 42]: try: data = { "prop1":123, "prop2":"this is prop2", "prop3": x } validictory.validate(data, schema) except ValueError as e: self.fail("Unexpected failure: %s" % e) #failures for x in [{"test":"blah"}, [32, 49], None, True]: data = { "prop1":123, "prop2":"this is prop2", "prop3": x } self.assertRaises(ValueError, validictory.validate, data, schema)
def test_uniqueitems_pass_string(self): data = ['1', '2', '3'] try: validictory.validate(data, self.schema) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def test_uniqueitems_pass_different_types(self): data = [1, "1"] try: validictory.validate(data, self.schema) except ValueError as e: self.fail("Unexpected failure: %s" % e)
def test_items_descriptive_fail(self): data = [1294] try: validictory.validate(data, self.schema1) except ValueError, e: # warning should mention list item, not _data assert 'list item' in str(e)
def read_json(file): ''' Open and read the full content of a json file ''' assert file is not None, "pre-condition assert" try: with open(file) as f: f = f.read() data = json.loads(f) #schema = {"type": "object", "properties":{"stocks":{["name":"string"]}}} #schema = {"type":"object", "properties":{"array", "minItems":1}} schema = {"type":"object"} #s = open('schema.json') #schema = {"type":"object","properties":{"stocks":{"id":"stocks","type":"array","additionalItems":"false"}},"additionalProperties":"false"} schema = {"type":"object","properties":{"stocks":{"type":"array" }}} #schema = {"type":"object","properties":{"stocks":{"type":["type":"object"] }}} #schema = {"type":"object","properties":{"stocks":{"id":"stocks","type":"array","items":{"id":"4","type":"object","properties":{"spp global":{"type":"string"}}}}}} #schema = {"type":"object","properties":{"stocks":{"id":"stocks","type":"array","items":{"id":"3","type":"object","properties":{"spp global":{"id":"spp global","type":"string"}}}}}} # http://jsonschema.net/#/ #schema = {"type":"object","properties":{"stocks":{"id":"stocks","type":"array","items":{"id":"auto-generated-schema-594", "type":"object"}}}} schema = {"type":"object","properties":{"stocks":{"id":"stocks","type":"array","items":{"type":"object"}}}} # http://www.alexconrad.org/2011/10/json-validation.html validictory.validate(data, schema) return data except FileNotFoundError as e: raise DataError("Can not load json file: {}".format(e)) except ValueError as e: raise DataError("Wrong format in json file: {}".format(e))
def __init__(self, geojson_string): filtered_geojson = {'type': 'FeatureCollection', 'features': []} try: test_geojson = json.loads(geojson_string) if not isinstance(test_geojson, dict): raise GeoJSONParserException('Content was not a JSON object.') except (ValueError, TypeError): raise GeoJSONParserException('Content was not JSON serializeable.') if not 'type' in test_geojson: raise GeoJSONParserException('The "type" member is requried and was not found.') if test_geojson['type'] != 'FeatureCollection': raise GeoJSONParserException('GeoJSON object must be of type FeatureCollection. The passed type was {0}.'.format(test_geojson['type'])) elif not ('features' in test_geojson and isinstance(test_geojson['features'], (list, tuple))): raise GeoJSONParserException('GeoJSON object must have member named "features" as an array of Features.') for feature in test_geojson['features']: if not ('type' in feature and feature['type'] == 'Feature' and 'properties' in feature and 'geometry' in feature): raise GeoJSONParserException('GeoJSON Features must have a type of "Feature" and "properties" and "geometry" members.') if feature['geometry'] is None: # null geometries are valid. Move along. continue if feature['geometry']['type'] not in geojson_types: raise GeoJSONParserException('{0} is not a valid GeoJSON geometry.'.format(feature['geometry']['type'])) try: validictory.validate(feature['geometry'], geojson_types[feature['geometry']['type']]) except validictory.validator.ValidationError as e: raise GeoJSONParserException('GeoJSON validation error. Message: {0}.'.format(str(e))) filtered_geojson['features'].append(feature) # Everything's good self.features = filtered_geojson['features']
def check_local_config_schema(build): log.info("Verifying your configuration settings...") # leave this import here: might not be on sys.path in some situations import validictory local_conf_filename = build.tool_config.get('general.local_config') if local_conf_filename is not None: # explicit conf file defined if not path.isfile(local_conf_filename): raise ConfigurationError("{file} does not exist!".format(file=local_conf_filename)) else: local_conf_filename = 'local_config.json' if not path.isfile(local_conf_filename): log.warning("Local configuration file '{file}' does not exist!".format(file=local_conf_filename)) with open(local_conf_filename) as local_conf_file: local_conf = json.load(local_conf_file) from forge.remote import Remote from forge import build_config remote = Remote(build_config.load()) local_conf_schema = remote._api_get('platform/{platform_version}/local_config_schema'.format( platform_version=build.config['platform_version'])) try: validictory.validate(local_conf, local_conf_schema) except validictory.validator.UnexpectedPropertyError as e: log.warning('Unexpected setting: "{error}" in "{file}". This will be ignored.'.format( file=local_conf_filename, error=e) ) log.info("Configuration settings check complete")
def test_disallow_unknown_properties_pass(self): try: validictory.validate(self.data_simple, self.schema_simple, disallow_unknown_properties=True) except ValueError as e: self.fail("Unexpected failure: %s" % e)