Ejemplo n.º 1
0
 def best_match(self, errors):
     errors = list(errors)
     best = exceptions.best_match(errors)
     reversed_best = exceptions.best_match(reversed(errors))
     msg = "Didn't return a consistent best match!\nGot: {0}\n\nThen: {1}"
     self.assertEqual(
         best, reversed_best, msg=msg.format(best, reversed_best),
     )
     return best
Ejemplo n.º 2
0
def is_valid_json(instance_json, schema_json):
    """
    Make a validation check whether the instance_json given parameter is 
    valid by the defined constraints of schema_json. Return True if the first 
    parameter is schema based, or return False if that is invalid.
    
    Parameters:
    @param instance_json: json object which will be checked
    @param schema_json: the schema json object  
    """
    try:
        validate(instance_json, schema_json)
    except ValueError as value_error:
        logging.error("Json file is not valid \n", value_error)
        return False
    except ValidationError:
        logging.error("A problem has occurred during the validation, here: " +
                      best_match(Draft4Validator(schema_json)
                                 .iter_errors(instance_json)).message)
        return False
    except SchemaError:
        logging.error("The provided schema is malformed.")
        return False
    
    if instance_json["MinSize"] > instance_json["MaxSize"]:
        logging.error("Maxsize is lower than minsize. Change the " +
                      "config.json file")
        return False
    return True
Ejemplo n.º 3
0
def validate_schema(file_path, file_type):
    """Check if the specified config file has a valid schema

    :param file_path: path to file to validate
    :param file_type: what schema type should we validate against
    """
    schema = get_schema(file_type)
    if (schema is None):
        print '%s: %s' % (SCHEMA_NOT_FOUND, file_path)
        return
    validator = Draft4Validator(schema, format_checker=FormatChecker())
    basename = os.path.basename(file_path)
    extension = os.path.splitext(basename)[1]
    try:
        config_file = get_file_contents(file_path)
    except IOError:
        print '%s: %s' % (FAILED_READING_FILE, file_path)
        return False
    if extension == '.yaml':
        config_file_object = yaml.load(config_file)
    elif extension == '.json':
        config_file_object = json.loads(config_file)
    else:
        config_file_object = config_file
    try:
        validator.validate(config_file_object)
    except ValidationError:
        print '%s: %s' % (SCHEMA_INVALID, file_path)

        errors = validator.iter_errors(config_file_object)
        print '  Validation Message: %s' % exceptions.best_match(errors).message
    else:
        print '%s: %s' % (SCHEMA_VALID, basename)
        return True
Ejemplo n.º 4
0
def validate(data, schema=None):
    if schema is None:
        schema = generate()
        Validator.check_schema(schema)
    validator = Validator(schema)

    errors = list(validator.iter_errors(data))
    if not errors:
        counter = Counter([p['name'] for p in data.get('policies')])
        dupes = []
        for k, v in counter.items():
            if v > 1:
                dupes.append(k)
        if dupes:
            return [ValueError(
                "Only one policy with a given name allowed, duplicates: %s" % (
                    ", ".join(dupes)))]
        return []
    try:
        resp = specific_error(errors[0])
        name = isinstance(errors[0].instance, dict) and errors[0].instance.get('name', 'unknown') or 'unknown'
        return [resp, name]
    except Exception:
        logging.exception(
            "specific_error failed, traceback, followed by fallback")

    return filter(None, [
        errors[0],
        best_match(validator.iter_errors(data)),
    ])
Ejemplo n.º 5
0
 def test_one_error(self):
     validator = Draft4Validator({"minProperties": 2})
     error, = validator.iter_errors({})
     self.assertEqual(
         exceptions.best_match(validator.iter_errors({})).validator,
         "minProperties",
     )
Ejemplo n.º 6
0
def validate(data, schema=None):
    if schema is None:
        schema = generate()
        Validator.check_schema(schema)

    validator = Validator(schema)
    errors = list(validator.iter_errors(data))
    if not errors:
        return check_unique(data) or []
    try:
        resp = policy_error_scope(specific_error(errors[0]), data)
        name = isinstance(
            errors[0].instance,
            dict) and errors[0].instance.get(
            'name',
            'unknown') or 'unknown'
        return [resp, name]
    except Exception:
        logging.exception(
            "specific_error failed, traceback, followed by fallback")

    return list(filter(None, [
        errors[0],
        best_match(validator.iter_errors(data)),
    ]))
Ejemplo n.º 7
0
def default_and_validate(model, schema):
    instance = model.to_dict()
    apply_defaults(instance, schema)
    errors = list(Draft4Validator(schema).iter_errors(instance))
    if len(errors) > 0:
        raise DartValidationException(str(best_match(errors)))
    return model.from_dict(instance)
Ejemplo n.º 8
0
def is_valid(instance):
    """
    Validates a data.json entry against the project open data's JSON schema. Log a warning message on validation error
    """
    error = best_match(validator.iter_errors(instance))
    if error:
        logger.warn("Validation failed, best guess of error = %s", error)
        return False
    return True
Ejemplo n.º 9
0
 def is_valid(self, instance):
     """
     Validates a data.json entry against the project open data's JSON schema.
     Log a warning message on validation error
     """
     error = best_match(draft4validator.iter_errors(instance))
     if error:
         logger.warn("===================================================\r\n"+
                     "Validation failed, best guess of error:\r\n %s\r\nFor this dataset:\r\n", error)
         return False
     return True
Ejemplo n.º 10
0
def _validate_body(body: dict, schema: dict) -> None:
    """ Raise an InvalidBody exception if non-compliant with the spec """

    errors = Draft4Validator(schema).iter_errors(body)
    error = best_match(errors)
    if error:
        exc = InvalidBody(error.message)
        exc.link = error.schema['uri']
        exc.meta = {'spec': error.schema['description']}
        exc.source = {'pointer': '/' + '/'.join(error.absolute_path)}
        raise exc
Ejemplo n.º 11
0
    def validate(self, data):
        """
        Validate `data` according to the current schema.

        :param data: The data to be validated
        :return: valid data
        :raises ValidationError: if the data is invalid
        """
        # Take a copy to ensure we don't modify what we were passed.
        appstruct = copy.deepcopy(data)
        error = best_match(self.validator.iter_errors(appstruct))
        if error is not None:
            raise ValidationError(_format_jsonschema_error(error))
        return appstruct
def is_valid_json(instance_json, schema_json):
    """
    Make a validation check whether the instance_json given parameter is 
    valid by the defined constraints of schema_json. Return True if the first 
    parameter is schema based, or return False if that is invalid.
    
    Parameters:
    @param instance_json: json object which will be checked
    @param schema_json: the schema json object  
    """
    try:
        validate(instance_json,schema_json)
    except ValueError as value_error:
        print("Json file is not valid \n", value_error)
        return False
    except ValidationError as validation_error:
        print("A problem has occurred during the validation, here:")
        # determine the error's first place
        print(best_match(Draft4Validator(schema_json)\
                         .iter_errors(instance_json)).message)
        return False
        #rint(best_match(Draft4Validator(schema_json).iter_errors(instance_json)).message)
        #print(ErrorTree(Draft4Validator(schema_json).iter_errors(instance_json)))
    except SchemaError:
        print("The provided schema is malformed.")
        return False
    
    if (instance_json["minSize"] > instance_json["maxSize"]):
        print("Maxsize is lower than minsize. Change the "\
              "config.json file")
        return False
    
    # store the actual working directory to change back later
    current_path = os.getcwd()
    # set the working directory to this module's path
    handler.set_working_directory();
    # instance_json["workspacePath"] must be checked relatively
    handler.set_working_directory("../../../")
    if (os.path.exists(instance_json["workspacePath"]) == False):
        print(instance_json["workspacePath"] +\
              "is not an existing directory.")
        handler.set_working_directory(current_path);
        return False
    elif(os.path.isdir(instance_json["workspacePath"]) == False):
        print(instance_json["workspacePath"] + " is not a directory!")
        handler.set_working_directory(current_path);
        return False
    handler.set_working_directory(current_path);
    return True
Ejemplo n.º 13
0
def validate(data):
    schema = generate()
    Validator.check_schema(schema)
    validator = Validator(schema)

    errors = list(validator.iter_errors(data))
    if not errors:
        return []
    try:
        return [specific_error(errors[0])]
    except Exception:
        logging.exception(
            "specific_error failed, traceback, followed by fallback")

    return filter(None, [
        errors[0],
        best_match(validator.iter_errors(data)),
    ])
Ejemplo n.º 14
0
def load_config(path: str) -> Dict:
    """
    Loads a config file from the given path
    :param path: path as str
    :return: configuration as dictionary
    """
    with open(path) as file:
        conf = json.load(file)
    if 'internals' not in conf:
        conf['internals'] = {}
    logger.info('Validating configuration ...')
    try:
        validate(conf, CONF_SCHEMA)
        return conf
    except ValidationError as exception:
        logger.fatal('Invalid configuration. See config.json.example. Reason: %s', exception)
        raise ValidationError(
            best_match(Draft4Validator(CONF_SCHEMA).iter_errors(conf)).message
        )
Ejemplo n.º 15
0
def default_and_validate(model, schema):
    instance = model.to_dict()
    apply_defaults(instance, schema)
    errors = list(Draft4Validator(schema).iter_errors(instance))
    if len(errors) > 0:
        raise DartValidationException(str(best_match(errors)))

    user_id = "anonymous"
    if current_user and hasattr(current_user, 'email'):
        user_id = current_user.email
    else:
        _logger.debug("No current_user found. using anonymous user id instead.")

    if instance and ('data' in instance) and ("user_id" in instance["data"]):
        instance['data']['user_id'] = user_id
    else:
        _logger.debug("instance['data']['user_id'] do not exist. Skipping setting a user_id.")

    return model.from_dict(instance)
Ejemplo n.º 16
0
 def put(self, store_id):
     store = Store.objects.filter(external_id=store_id, live=True).first()
     if not store:
         return jsonify({}), 404
     store_json = request.json
     error = best_match(Draft4Validator(schema).iter_errors(store_json))
     if error:
         return jsonify({"error": error.message}), 400
     else:
         store.neighborhood = store_json.get('neighborhood')
         store.street_address = store_json.get('street_address')
         store.city = store_json.get('city')
         store.state = store_json.get('state')
         store.zip = store_json.get('zip')
         store.phone = store_json.get('phone')
         store.save()
         response = {
             "result": "ok",
             "store": store_obj(store)
         }
         return jsonify(response), 200
Ejemplo n.º 17
0
def validate_config_schema(conf: Dict[str, Any]) -> Dict[str, Any]:
    """
    Validate the configuration follow the Config Schema
    :param conf: Config in JSON format
    :return: Returns the config if valid, otherwise throw an exception
    """
    conf_schema = deepcopy(constants.CONF_SCHEMA)
    if conf.get('runmode', RunMode.OTHER) in (RunMode.DRY_RUN, RunMode.LIVE):
        conf_schema['required'] = constants.SCHEMA_TRADE_REQUIRED
    elif conf.get('runmode',
                  RunMode.OTHER) in (RunMode.BACKTEST, RunMode.HYPEROPT):
        conf_schema['required'] = constants.SCHEMA_BACKTEST_REQUIRED
    else:
        conf_schema['required'] = constants.SCHEMA_MINIMAL_REQUIRED
    try:
        FreqtradeValidator(conf_schema).validate(conf)
        return conf
    except ValidationError as e:
        logger.critical(f"Invalid configuration. Reason: {e}")
        raise ValidationError(
            best_match(Draft4Validator(conf_schema).iter_errors(conf)).message)
Ejemplo n.º 18
0
    def put(cls, product_id):
        """
        update a specific product

        Endpoint: /product/88517737685737189039085760589870132011

        Example Body:
            {
                "title": "Kitty Litter",
                "product_type": "Pets",
                "vendor": "Pet Co"
            }
        """
        store = Store.objects.filter(app_id=request.headers.get('APP-ID'),
                                     deleted_at=None).first()

        product = Product.objects.filter(product_id=product_id,
                                         deleted_at=None,
                                         store=store).first()
        if not product:
            return jsonify({}), 404

        product_json = request.json
        error = best_match(Draft4Validator(schema).iter_errors(product_json))
        if error:
            return jsonify({"error": error.message}), 400

        product.title = product_json.get("title")
        product.product_type = product_json.get("product_type")
        product.vendor = product_json.get("vendor")
        product.sale_price_in_cents = product_json.get("sale_price_in_cents")

        if product_json.get("description") is not None:
            product.description = product_json.get("description")

        product.updated_at = datetime.now()
        product.save()

        response = {"result": "ok", "product": product_obj(product)}
        return jsonify(response), 201
Ejemplo n.º 19
0
    def post(self):        
        """
        Register a new user
        ---
        tags:
          - registration
        """
        req = request.json

        # ensure correct schema
        error = best_match(Draft4Validator(schema_register).iter_errors(req))
        if error:
            return jsonify({"error": error.message}), 400
        
        # ensure that 'username' field is unique
        if self._db_session.query(exists().where(UserMaster.username == req.get("username"))).scalar():
            return jsonify({"error": "username already exists"}), 400 
        
        # ensure that the saving amount is an accepted value
        saving_amount = req.get('savingAmount')
        if saving_amount not in accepted_saving_amounts:
            return jsonify({"error": "saving amount not in {0}".format(accepted_saving_amounts)}), 400 

        # ensure that the loan amount is an accepted value
        loan_amount = req.get('loanAmount')
        if loan_amount not in accepted_loan_amounts:
            return jsonify({"error": "loan amount not in {0}".format(accepted_loan_amounts)}), 400

        # register new user & its profile
        user = UserMaster(
            username = req.get('username'),
            password = hash_password(req.get('password')),
            saving_amount = saving_amount,
            loan_amount = loan_amount,
            access = Access(generate_token(), get_expiry_date())
        )
        self._db_session.add(user)
        self._db_session.commit()

        return jsonify({"profile": get_user_profile(saving_amount, loan_amount)}), 201
Ejemplo n.º 20
0
    def _validate_data(self, data: dict):
        """
        Validates data against provider schema. Raises :class:`~notifiers.exceptions.BadArguments` is relevant
        
        Arguments:
            data {dict} -- Data to validate

        :raises: :class:`~notifiers.exceptions.BadArguments`
        """
        log.debug("validating provided data")

        e = best_match(self.validator.iter_errors(data))

        if e:
            custom_error_key = f"error_{e.validator}"

            msg = (e.schema[custom_error_key]
                   if e.schema.get(custom_error_key) else e.message)

            raise BadArguments(validation_error=msg,
                               provider=self.name,
                               data=data)
Ejemplo n.º 21
0
def default_and_validate(model, schema):
    instance = model.to_dict()
    apply_defaults(instance, schema)
    errors = list(Draft4Validator(schema).iter_errors(instance))
    if len(errors) > 0:
        raise DartValidationException(str(best_match(errors)))

    user_id = "anonymous"
    if current_user and hasattr(current_user, 'email'):
        user_id = current_user.email
    else:
        _logger.debug(
            "No current_user found. using anonymous user id instead.")

    if instance and ('data' in instance) and ("user_id" in instance["data"]):
        instance['data']['user_id'] = user_id
    else:
        _logger.debug(
            "instance['data']['user_id'] do not exist. Skipping setting a user_id."
        )

    return model.from_dict(instance)
Ejemplo n.º 22
0
    def post(self):
        course_json = request.json

        error = best_match(Draft4Validator(schema).iter_errors(course_json))

        if error:
            return jsonify({"error": error.message}), 400
        else:
            starting_date = datetime.datetime.strptime(
                course_json.get('starting_date'), "%Y-%m-%dT%H:%M:%SZ")

            ending_date = datetime.datetime.strptime(
                course_json.get('ending_date'), "%Y-%m-%dT%H:%M:%SZ")

            external_id = str(uuid.uuid4())

            params = {
                'title': course_json.get('title'),
                'description': course_json.get('description'),
                'duration': course_json.get('duration'),
                'starting_date': starting_date,
                'ending_date': ending_date,
                'user_id': course_json.get('user_id'),
                'display_picture': course_json.get('display_picture'),
                'requirements': course_json.get('requirements'),
                'what_you_will_learn': course_json.get('what_you_will_learn'),
                'curriculum': course_json.get('curriculum'),
                'target_audience': course_json.get('target_audience'),
                'external_id': external_id
            }

            try:
                course = Course(**params).save()
            except Exception as e:
                return jsonify({"error": e}), 400

            response = {'result': 'ok', 'course': course_obj(course)}

            return jsonify(response)
Ejemplo n.º 23
0
 def post(self):
     user_json = request.json
     error = best_match(
         Draft4Validator(UserDB.getSchema()).iter_errors(user_json))
     if error:
         return jsonify({"error": error.message}), 400
     else:
         user = User(external_id=str(uuid.uuid4()),
                     country=user_json.get("country"),
                     state=user_json.get("state"),
                     city=user_json.get("city"),
                     lang=user_json.get("lang"),
                     name=user_json.get("name"),
                     surname=user_json.get("surname"),
                     username=user_json.get("username"),
                     email=user_json.get("email"),
                     password=user_json.get("password"),
                     bio=user_json.ger("bio"),
                     live=True,
                     created=datetime.utcnow()).save()
         response = {"result": "ok", "user": user_obj(user)}
         return jsonify(response), 201
Ejemplo n.º 24
0
    def post(scan_id: ScanID, task_key: str) -> Any:
        """Add new Label for given scan.

        This endpoint needs a multipart/form-data content where there is one mandatory section called "label".
        Such section will contain a JSON payload with representation of a Label. If such Label needs additional
        information like images (binary mask), please attach them as a separate part.

        Here is an example CURL command that sends Label with Brush Element:

            $> curl -v
                    -H "Content-Type:multipart/form-data"
                    -H "Authorization: Bearer MEDTAGGER_API_TOKEN"
                    -F "SLICE_1=@"/Users/jakubpowierza/Desktop/test.png""
                    -F "label={"elements": [{"width": 1, "height": 1, "image_key": "SLICE_1",
                               "slice_index": 1, "tag": "LEFT_KIDNEY", "tool": "BRUSH"}],
                               "labeling_time": 0.1};type=application/json"
                     http://localhost:51000/api/v1/scans/c5102707-cb36-4869-8041-f00421c03fa1/MARK_KIDNEYS/label
        """
        is_predefined = (request.args.get('is_predefined', 'false') == 'true')
        if is_predefined:
            require_one_of_roles({'doctor', 'admin'})

        files = {name: file_data.read() for name, file_data in request.files.items()}
        label = json.loads(request.form['label'])
        elements = label['elements']
        try:
            validate(elements, elements_schema)
        except ValidationError:
            validator = Draft4Validator(elements_schema)
            errors = validator.iter_errors(elements)
            best_error = best_match(errors)
            raise InvalidArgumentsException(best_error.message)

        business.validate_label_payload(label, task_key, files)

        labeling_time = label['labeling_time']
        comment = label.get('comment')
        label = business.add_label(scan_id, task_key, elements, files, labeling_time, comment, is_predefined)
        return label, 201
def validate(schema_name, src_path):
    schema = load_schema(schema_name)
    check_action = schema_name in ['cfr']

    with open(src_path, "r") as f:
        items = json.loads(f.read())
        for item in items:
            try:
                print("Validate schema for {}".format(item["id"]))
                jsonschema.validate(instance=item, schema=schema)
                # If it's an experiment we want to evaluate the branches
                if "arguments" in item:
                    validate_experiment(item)
            except ValidationError as err:
                match = best_match([err])
                print("Validation error: {}".format(match.message))
                sys.exit(1)
            validate_item_targeting(item)
            if check_action:
                validate_all_actions(item, schema_name)

    print("Passed!")
Ejemplo n.º 26
0
 def put(self, person_id):
     person = Person.find_by_id(person_id)
     print(person)
     if not person:
         response = {}
         return make_response(response=response, code=404)
     # print(person_id)
     person_json = request.json
     error = best_match(Draft7Validator(schema).iter_errors(person_json))
     if error:
         response = {'error': error.message}
         return make_response(response=response, code=400)
     # print(person_id)
     if person_json.get('birthdate'):
         try:
             birthdate = datetime.datetime.strptime(
                 person_json.get('birthdate'), "%Y-%m-%d")
             person_json['birthdate'] = birthdate
         except:
             response = {'error': "INVALID_BIRTHDATE"}
             return make_response(response=response, code=400)
     # print(person_id)
     if person_json.get("children"):
         children = Person.query.filter(
             Person.person_id.in_(person_json.get("children"))).all()
         person_json['children'] = children
     if person_json.get("parents"):
         parents = Person.query.filter(
             Person.person_id.in_(person_json.get("parents"))).all()
         person_json['parents'] = parents
     print(person_id)
     person.patch(person_json)
     print(person_id)
     person.save_to_db()
     print(person_id)
     response = {"result": "ok", "data": [person_obj(person)]}
     print(response)
     return make_response(response=response, code=200)
Ejemplo n.º 27
0
def validate(data, schema=None, resource_types=()):
    if schema is None:
        schema = generate(resource_types)
        JsonSchemaValidator.check_schema(schema)

    validator = JsonSchemaValidator(schema)
    errors = list(validator.iter_errors(data))
    if not errors:
        return check_unique(data) or []
    try:
        resp = policy_error_scope(specific_error(errors[0]), data)
        name = isinstance(errors[0].instance, dict) and errors[0].instance.get(
            'name', 'unknown') or 'unknown'
        return [resp, name]
    except Exception:
        logging.exception(
            "specific_error failed, traceback, followed by fallback")

    return list(
        filter(None, [
            errors[0],
            best_match(validator.iter_errors(data)),
        ]))
Ejemplo n.º 28
0
 def put(self, user_id):
     user = User.objects.filter(external_id=user_id, live=True).first()
     if not user:
         return jsonify({}), 404
     user_json = request.json
     error = best_match(Draft4Validator(UserSchema.schema).iter_errors(user_json))
     if error:
         return jsonify({"error": error.message}), 400
     else:
         user.country = user_json.get("country")
         user.state = user_json.get("state")
         user.city = user_json.get("city")
         user.name = user_json.get("name")
         user.surname = user_json.get("surname")
         user.username = user_json.get("username")
         user.e_mail = user_json.get("e_mail")
         user.password = user_json.get("password")
         user.save()
         response = {
             "result": "ok",
             "user": user_obj(user)
         }
         return jsonify(response), 200
Ejemplo n.º 29
0
    def validate_configuration(self, schema, configuration):

        validator = Draft4Validator(schema)
        try:
            configuration_json = json.loads(self.configuration)
        except:
            raise ValidationError(
                "The given configuration file is not valid JSON.")

        error = best_match(validator.iter_errors(configuration_json))

        if error:
            raise ValidationError(error.message)

        for key, field in configuration_json["fields"].iteritems():
            self.validate_field(key, field)

        index = 0
        fieldset_entries = []

        for fieldset in configuration_json["fieldsets"]:
            for field in fieldset["fields"]:
                if field not in fieldset_entries:
                    fieldset_entries.append(field)
                else:
                    raise ValidationError(
                        "Fieldset at index {} repeats field key '{}'.".format(
                            index, field))

                if field not in configuration_json["fields"]:
                    raise ValidationError(
                        "The key '{}'' is not defined as a field in fieldset at index {}"
                        .format(field, index))

            index += 1

        return configuration_json
Ejemplo n.º 30
0
    def put(self, course_id):

        course = Course.objects.filter(external_id=course_id,
                                       is_live=True).first()

        if not course:
            return jsonify({}), 404

        course_json = request.json

        error = best_match(Draft4Validator(schema).iter_errors(course_json))

        if error:
            return jsonify({"error": error.message}), 400
        else:
            starting_date = datetime.datetime.strptime(
                course_json.get('starting_date'), "%Y-%m-%dT%H:%M:%SZ")

            ending_date = datetime.datetime.strptime(
                course_json.get('ending_date'), "%Y-%m-%dT%H:%M:%SZ")

            course.title = course_json.get('title')
            course.description = course_json.get('description')
            course.duration = course_json.get('duration')
            course.starting_date = starting_date
            course.ending_date = ending_date
            course.user_id = course_json.get('user_id')
            course.display_picture = course_json.get('display_picture')
            course.requirements = course_json.get('requirements')
            course.what_you_will_learn = course_json.get('what_you_will_learn')
            course.curriculum = course_json.get('curriculum')
            course.target_audience = course_json.get('target_audience')

            course.save()

            response = {"result": "ok", "pet": course_obj(course)}
            return jsonify(response), 200
Ejemplo n.º 31
0
def validate_experiment(item):
    # Load all the schemas to validate experiment messages
    load_all_schemas()

    validated = 0
    for branch in item.get("arguments").get("branches"):
        branch_message = get_branch_message(branch.get("value"))
        if branch_message is None:
            print("\tSkip branch {} because it's empty".format(
                branch.get("slug")))
            validated += 1
            continue
        # Try all of the available message schemas
        for schema_path in ALL_SCHEMAS.keys():
            try:
                branch_message_list = branch_message if isinstance(
                    branch_message, list) else [branch_message]
                for message in branch_message_list:
                    jsonschema.validate(instance=message,
                                        schema=ALL_SCHEMAS.get(schema_path))
                validated += 1
                print("\tValidated {} with {}".format(branch.get("slug"),
                                                      schema_path))
                break
            except ValidationError as err:
                match = best_match([err])
                print("\tValidation error: {}".format(match.message))
                print("\tTried to validate {} with {}\n".format(
                    branch.get("slug"), schema_path))

        # Validate the targeting JEXL if any
        validate_item_targeting(branch_message, True)

    if validated != len(item.get("arguments").get("branches")):
        print("\tBranches did not validate for {}".format(item.get("id")))
        sys.exit(1)
Ejemplo n.º 32
0
    def post(self):
        person_json = request.json
        error = best_match(Draft7Validator(schema).iter_errors(person_json))
        if error:
            response = {'error': error.message}
            return make_response(response=response, code=400)
        try:
            birthdate = datetime.datetime.strptime(
                person_json.get('birthdate'), "%Y-%m-%d")
        except:
            response = {'error': "INVALID_BIRTHDATE"}
            return make_response(response=response, code=400)
        if person_json.get("children"):
            children = Person.query.filter(
                Person.person_id.in_(person_json.get("children"))).all()
            person_json['children'] = children
        if person_json.get("parents"):
            parents = Person.query.filter(
                Person.person_id.in_(person_json.get("parents"))).all()
            person_json['parents'] = parents

        person = Person(person_id=str(uuid.uuid4()),
                        firstname=person_json.get('firstname'),
                        lastname=person_json.get('lastname'),
                        birthdate=birthdate,
                        email=person_json.get('email'),
                        street_address=person_json.get('street_address'),
                        city=person_json.get('city'),
                        state=person_json.get('state'),
                        zip=person_json.get('zip'),
                        phone=person_json.get('phone'),
                        children=person_json.get('children', []),
                        parents=person_json.get('parents', []))
        person.save_to_db()
        response = {"result": "ok", "data": [person_obj(person)]}
        return make_response(response=response, code=201)
Ejemplo n.º 33
0
def validate(data, schema=None):
    if schema is None:
        schema = generate()
        Validator.check_schema(schema)

    validator = Validator(schema)

    errors = list(validator.iter_errors(data))

    if not errors:
        counter = Counter([p['name'] for p in data.get('policies')])
        dupes = []
        for k, v in counter.items():
            if v > 1:
                dupes.append(k)
        if dupes:
            return [
                ValueError(
                    "Only one policy with a given name allowed, duplicates: %s"
                    % (", ".join(dupes))), dupes[0]
            ]
        return []
    try:
        resp = specific_error(errors[0])
        name = isinstance(errors[0].instance, dict) and errors[0].instance.get(
            'name', 'unknown') or 'unknown'
        return [resp, name]
    except Exception:
        logging.exception(
            "specific_error failed, traceback, followed by fallback")

    return list(
        filter(None, [
            errors[0],
            best_match(validator.iter_errors(data)),
        ]))
Ejemplo n.º 34
0
    def post(self):
        """
        creates a new gift card.
        """
        error = best_match(
            Draft4Validator(create_gift_cart_schema).iter_errors(request.json))
        if error:
            return jsonify({"error": error.message}), 400

        store = Store.objects.filter(app_id=request.headers.get('APP-ID'),
                                     deleted_at=None).first()

        gifter_customer = Customer.objects.filter(
            customer_id=request.json.get("gifter_customer_id"),
            store_id=store,
            deleted_at=None).first()
        if gifter_customer is None:
            return jsonify({"error": CUSTOMER_NOT_FOUND}), 404

        recipient_customer = Customer.objects.filter(
            customer_id=request.json.get("recipient_customer_id"),
            store_id=store,
            deleted_at=None).first()
        if recipient_customer is None:
            return jsonify({"error": CUSTOMER_NOT_FOUND}), 404

        gift_card = GiftCard(
            gift_card_id=str(uuid.uuid4().int),
            gifter_customer=gifter_customer,
            recipient_customer=recipient_customer,
            original_balance_in_cents=request.json.get("original_amount"),
            current_balance_in_cents=request.json.get(
                "original_amount")).save()

        response = {"result": "ok", "gift_card": gift_card_obj(gift_card)}
        return jsonify(response), 201
Ejemplo n.º 35
0
 def test_no_errors(self):
     validator = Draft4Validator({})
     self.assertIsNone(exceptions.best_match(validator.iter_errors({})))
Ejemplo n.º 36
0
def validate(obj):
    errors = best_match(_validator.iter_errors(obj))
    if errors:
        return False, str(errors)
    return True, None
Ejemplo n.º 37
0
    def validate():
        result = best_match(
            Draft4Validator(Verification.conSchema,
                            format_checker=FormatChecker()).iter_errors(
                                Verification.constants))

        if result:
            index_range = len(list(result.schema_path))

            if index_range > 4:
                print("FAILURE: " + list(result.schema_path)[4],
                      result.message,
                      sep=", ")
                return False

            elif index_range == 4:
                print("FAILURE: " + list(result.schema_path)[3],
                      result.message,
                      sep=", ")
                return False

            elif index_range == 3:
                print("FAILURE: " + list(result.schema_path)[2],
                      result.message,
                      sep=", ")
                return False

            elif index_range == 2:
                print("FAILURE: " + list(result.schema_path)[1],
                      result.message,
                      sep=", ")
                return False

            else:
                print("FAILURE: " + list(result.schema_path)[0],
                      result.message,
                      sep=", ")
                return False
        else:
            servo_lists = [
                value["servo_list"]
                for (key,
                     value) in Verification.constants['components'].items()
            ]
            motor_lists = [
                value["motor_list"]
                for (key,
                     value) in Verification.constants['components'].items()
            ]
            servo_pins = [
                value["pin"] for group in servo_lists
                for (key, value) in group.items()
            ]
            motor_pins = [
                value["pin"] for group in motor_lists
                for (key, value) in group.items()
            ]

            if len(servo_pins + motor_pins) == len(set(servo_pins +
                                                       motor_pins)):
                for (key,
                     value) in Verification.constants['components'].items():
                    servo_schemes = [
                        value["color_scheme"]
                        for (key, value) in value["servo_list"].items()
                    ]
                    motor_schemes = [
                        value["color_scheme"]
                        for (key, value) in value["motor_list"].items()
                    ]

                    if not len(servo_schemes + motor_schemes) == len(
                            set(servo_schemes + motor_schemes)):
                        print(
                            "FAILURE: Multiple use of a color-scheme in one component."
                        )
                        return False

                print("Validation of 'constants.json' was successful.")
                return True

            else:
                print("FAILURE: Pins are double used.")
                return False
Ejemplo n.º 38
0
 def findError(self, data, validator):
     e = best_match(validator.iter_errors(data))
     ex = specific_error(list(validator.iter_errors(data))[0])
     return e, ex
Ejemplo n.º 39
0
def validate(instance, schema=SCHEMA):
    v = Draft4Validator(schema)

    if not v.is_valid(instance):
        raise best_match(v.iter_errors(instance))
Ejemplo n.º 40
0
def validate(instance, schema, cls=OAS30Validator, *args, **kwargs):
    cls.check_schema(schema)
    validator = cls(schema, *args, **kwargs)
    error = best_match(validator.iter_errors(instance))
    if error is not None:
        raise error
Ejemplo n.º 41
0
 def test_no_errors(self):
     validator = Draft4Validator({})
     self.assertIsNone(exceptions.best_match(validator.iter_errors({})))
Ejemplo n.º 42
0
def validate(instance, schema):
    v = Draft7Validator(schema)
    if not v.is_valid(instance):
        raise best_match(v.iter_errors(instance))
Ejemplo n.º 43
0
def validate(instance, schema, cls=None, *args, **kwargs):
    """
    Validate an instance under the given schema.

        >>> validate([2, 3, 4], {"maxItems": 2})
        Traceback (most recent call last):
            ...
        ValidationError: [2, 3, 4] is too long

    :func:`validate` will first verify that the provided schema is itself
    valid, since not doing so can lead to less obvious error messages and fail
    in less obvious or consistent ways. If you know you have a valid schema
    already or don't care, you might prefer using the
    `IValidator.validate` method directly on a specific validator
    (e.g. ``Draft7Validator.validate``).


    Arguments:

        instance:

            The instance to validate

        schema:

            The schema to validate with

        cls (IValidator):

            The class that will be used to validate the instance.

    If the ``cls`` argument is not provided, two things will happen in
    accordance with the specification. First, if the schema has a
    :validator:`$schema` property containing a known meta-schema [#]_ then the
    proper validator will be used.  The specification recommends that all
    schemas contain :validator:`$schema` properties for this reason. If no
    :validator:`$schema` property is found, the default validator class is
    the latest released draft.

    Any other provided positional and keyword arguments will be passed on when
    instantiating the ``cls``.

    Raises:

        `jsonschema.exceptions.ValidationError` if the instance
            is invalid

        `jsonschema.exceptions.SchemaError` if the schema itself
            is invalid

    .. rubric:: Footnotes
    .. [#] known by a validator registered with
        `jsonschema.validators.validates`
    """
    if cls is None:
        cls = validator_for(schema)

    cls.check_schema(schema)
    validator = cls(schema, *args, **kwargs)
    error = exceptions.best_match(validator.iter_errors(instance))
    if error is not None:
        raise error
Ejemplo n.º 44
0
def validate(instance, schema, cls=None, *args, **kwargs):
    """
    Validate an instance under the given schema.

        >>> validate([2, 3, 4], {"maxItems": 2})
        Traceback (most recent call last):
            ...
        ValidationError: [2, 3, 4] is too long

    :func:`validate` will first verify that the provided schema is itself
    valid, since not doing so can lead to less obvious error messages and fail
    in less obvious or consistent ways.

    If you know you have a valid schema already, especially if you
    intend to validate multiple instances with the same schema, you
    likely would prefer using the `IValidator.validate` method directly
    on a specific validator (e.g. ``Draft7Validator.validate``).


    Arguments:

        instance:

            The instance to validate

        schema:

            The schema to validate with

        cls (IValidator):

            The class that will be used to validate the instance.

    If the ``cls`` argument is not provided, two things will happen in
    accordance with the specification. First, if the schema has a
    :validator:`$schema` property containing a known meta-schema [#]_ then the
    proper validator will be used.  The specification recommends that all
    schemas contain :validator:`$schema` properties for this reason. If no
    :validator:`$schema` property is found, the default validator class is
    the latest released draft.

    Any other provided positional and keyword arguments will be passed on when
    instantiating the ``cls``.

    Raises:

        `jsonschema.exceptions.ValidationError` if the instance
            is invalid

        `jsonschema.exceptions.SchemaError` if the schema itself
            is invalid

    .. rubric:: Footnotes
    .. [#] known by a validator registered with
        `jsonschema.validators.validates`
    """
    if cls is None:
        cls = validator_for(schema)

    cls.check_schema(schema)
    validator = cls(schema, *args, **kwargs)
    error = exceptions.best_match(validator.iter_errors(instance))
    if error is not None:
        raise error
Ejemplo n.º 45
0
    def post(self):
        """
        Creates a new customer

        Endpoint: /customer/

        Example Post Body:
        {
            "currency": "USD",
            "email": "*****@*****.**",
            "first_name": "John",
            "last_name": "Smith4",
            "addresses":
              [
                    {
                          "street": "1236 Main Street",
                          "city": "townsville",
                          "zip": "1234",
                          "state": "CA",
                          "country": "USA",
                          "is_primary": "true"
                    },
                    {
                          "street": "1215 Main Street",
                          "city": "townsville",
                          "zip": "500",
                          "state": "CA",
                          "country": "USA"
                    },
                    {
                          "street": "1216 Main Street",
                          "city": "townsville",
                          "zip": "500",
                          "state": "CA",
                          "country": "USA"
                    }
              ]
        }

        {
             "customer": {
                 "addresses": [
                     {
                         "address_id": "224473682041851492125327501325163956867",
                         "city": "townsville",
                         "created_at": "Thu, 10 Jan 2019 03:56:26 GMT",
                         "deleted_at": null,
                         "is_primary": false,
                         "state": "CA",
                         "street": "1215 Main Street",
                         "updated_at": "Thu, 10 Jan 2019 03:56:26 GMT",
                         "zip": "500"
                     },
                     {
                         "address_id": "245608141370371202915656949519861248348",
                         "city": "townsville",
                         "created_at": "Thu, 10 Jan 2019 03:56:26 GMT",
                         "deleted_at": null,
                         "is_primary": true,
                         "state": "CA",
                         "street": "1236 Main Street",
                         "updated_at": "Thu, 10 Jan 2019 03:56:26 GMT",
                         "zip": "1234"
                     },
                     {
                         "address_id": "274242069278329272621665758252140893540",
                         "city": "townsville",
                         "created_at": "Thu, 10 Jan 2019 03:56:26 GMT",
                         "deleted_at": null,
                         "is_primary": false,
                         "state": "CA",
                         "street": "1216 Main Street",
                         "updated_at": "Thu, 10 Jan 2019 03:56:26 GMT",
                         "zip": "500"
                     }
                 ],
                 "created_at": "Thu, 10 Jan 2019 03:56:26 GMT",
                 "currency": "USD",
                 "customer_id": "204987158183621343381078484949153439747",
                 "deleted_at": null,
                 "email": "*****@*****.**",
                 "first_name": "John",
                 "last_name": "Smith4",
                 "last_order_date": null,
                 "links": [
                     {
                         "href": "/customer/204987158183621343381078484949153439747",
                         "rel": "self"
                     }
                 ],
                 "total_spent": 0,
                 "updated_at": "Thu, 10 Jan 2019 03:56:26 GMT"
             },
        "result": "ok"
        }
        """
        customer_json = request.json
        error = best_match(Draft4Validator(schema).iter_errors(customer_json))
        if error:
            return jsonify({"error": error.message}), 400

        store = Store.objects.filter(app_id=request.headers.get('APP-ID'),
                                     deleted_at=None).first()
        existing_customer = Customer.objects.filter(
            email=customer_json.get("email"), store_id=store).first()

        if existing_customer:
            return jsonify({"error": "CUSTOMER_ALREADY_EXISTS"}), 400

        count_is_primary = 0
        if customer_json.get("addresses"):
            for address in customer_json.get("addresses"):
                if address.get("is_primary"):
                    if address.get("is_primary") == "true":
                        count_is_primary += 1
                        if count_is_primary > 1:
                            return jsonify({
                                "error":
                                "MULTIPLE_PRIMARY_ADDRESSES_SUPPLIED"
                            }), 400

        customer = Customer(currency=customer_json.get("currency"),
                            email=customer_json.get("email"),
                            first_name=customer_json.get("first_name"),
                            last_name=customer_json.get("last_name"),
                            customer_id=str(uuid.uuid4().int),
                            store_id=store).save()

        customer.set_password(customer_json.get("password"))

        if customer_json.get("addresses"):
            addresses = []
            for address in customer_json.get("addresses"):
                is_primary = False
                if address.get("is_primary"):
                    is_primary = True if address.get(
                        "is_primary").lower() == "true" else False
                address = Address(street=address.get("street"),
                                  city=address.get("city"),
                                  zip=address.get("zip"),
                                  state=address.get("state"),
                                  country=address.get("country"),
                                  is_primary=is_primary,
                                  customer_id=customer,
                                  address_id=str(uuid.uuid4().int))
                addresses.append(address)

            for add in addresses:
                Address.objects.insert(add)

        response = {"result": "ok", "customer": customer_obj(customer)}
        return jsonify(response), 201
Ejemplo n.º 46
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from jsonschema import Draft4Validator
from jsonschema.exceptions import best_match

schema = {
    "type": "array",
    "items": {
        "type": "string",
    },
    "minItems": 3,
}

instance = [11]

v = Draft4Validator(schema)

errors = list(v.iter_errors(instance))

print best_match(errors).message

print "*" * 50

for error in errors:
    print error.path, error.message
Ejemplo n.º 47
0
 def findError(self, data, validator):
     e = best_match(validator.iter_errors(data))
     ex = specific_error(list(validator.iter_errors(data))[0])
     return e, ex
Ejemplo n.º 48
0
def get_config(configuration_filename,
               schema_filename='config.yml',
               lower_keys=True):
    """
    Gets default config and overwrite it with the content of configuration_filename.
    If the file does not exist, it creates it.
    Default config is generated by applying get_defaults() to local file named configuration.yaml .
    Content of configuration_filename by assuming the content is formatted in YAML.
    Args:
        configuration_filename: name of the YAML configuration file
        schema_filename: name of the JSONSchema file
        lower_keys: transform keys to uppercase
    Returns:
        dict: configuration statements
    """

    base_dir = os.path.dirname(os.path.realpath(__file__))
    with open(os.path.join(base_dir, schema_filename)) as stream:
        try:
            configschema = yaml.load(stream,
                                     Loader=yamlordereddictloader.Loader)
        except (yaml.scanner.ScannerError) as error:
            raise SyntaxError('Error while parsing configuration file: %s' %
                              error)

    if os.path.exists(configuration_filename):
        with open(configuration_filename, 'r') as stream:
            defaults = get_defaults(configschema)
            config = yaml.load(stream, Loader=yaml.FullLoader)
            config = updatedict(defaults, config)
            if lower_keys:
                config = keys_to_lower(config)
    else:
        # Read defaults and include descriptions
        config = get_defaults(configschema, with_description=True)
        content = yaml.dump(config,
                            default_flow_style=False,
                            sort_keys=False,
                            width=9999)

        # Transform key preceding description lines to "# "
        content = content.splitlines()
        for _ in range(len(content)):
            content[_] = re.sub(r"- __description_\S+: '(.*)'", r"  # \1",
                                content[_])
            content[_] = re.sub(r"__description_\S+: '(.*)'", r"# \1",
                                content[_])
            content[_] = re.sub(r"- __description_\S+: ", "  # ", content[_])
            content[_] = re.sub(r"__description_\S+: ", "# ", content[_])
        content = '\n'.join(content) + '\n'

        # Dump config to file
        try:
            with open(configuration_filename, 'w') as stream:
                stream.write(content)
                print('Created configuration file: %s' %
                      configuration_filename)
        except IOError:
            raise IOError('Unable to create configuration file: %s' %
                          configuration_filename)

        # Reload defaults without descriptions
        config = get_defaults(configschema, with_description=False)

    error = best_match(
        DefaultValidatingDraft4Validator(configschema).iter_errors(config))
    if error:
        if error.path:
            path = '/'.join(error.relative_path)
            raise SyntaxError(
                'Error while parsing configuration file, not a valid value for: %s'
                % path)
        raise SyntaxError('Error while parsing configuration file: %s' %
                          error.message)

    return config