示例#1
0
    def _validate(self, doc, **kwargs):
        lookup = {'act': doc['act'], 'type': doc[ITEM_TYPE]}
        use_headline = kwargs and 'headline' in kwargs
        validators = superdesk.get_resource_service('validators').get(req=None, lookup=lookup)
        for validator in validators:
            v = Validator()
            v.allow_unknown = True
            v.validate(doc['validate'], validator['schema'])
            error_list = v.errors
            response = []
            for e in error_list:
                if error_list[e] == 'required field' or type(error_list[e]) is dict:
                    message = '{} is a required field'.format(e.upper())
                elif 'min length is' in error_list[e]:
                    message = '{} is too short'.format(e.upper())
                elif 'max length is' in error_list[e]:
                    message = '{} is too long'.format(e.upper())
                else:
                    message = '{} {}'.format(e.upper(), error_list[e])

                if use_headline:
                    response.append('{}: {}'.format(doc['validate'].get('headline',
                                                                        doc['validate'].get('_id')), message))
                else:
                    response.append(message)
            return response
        else:
            return ['validator was not found for {}'.format(doc['act'])]
示例#2
0
def test_coerce_not_destructive():
    schema = {
        'amount': {'coerce': int}
    }
    v = Validator(schema)
    doc = {'amount': '1'}
    v.validate(doc)
    assert v.document is not doc
示例#3
0
def test_readonly_field_first_rule():
    # test that readonly rule is checked before any other rule, and blocks.
    # See #63.
    schema = {"a_readonly_number": {"type": "integer", "readonly": True, "max": 1}}
    v = Validator(schema)
    v.validate({"a_readonly_number": 2})
    # it would be a list if there's more than one error; we get a dict
    # instead.
    assert "read-only" in v.errors["a_readonly_number"][0]
示例#4
0
def test_callable_validator():
    """
    Validator instance is callable, functions as a shorthand
    passthrough to validate()
    """
    schema = {"test_field": {"type": "string"}}
    v = Validator(schema)
    assert v.validate({"test_field": "foo"})
    assert v({"test_field": "foo"})
    assert not v.validate({"test_field": 1})
    assert not v({"test_field": 1})
def max_min_length_example():
    """对于字符串项, 可以限制字符串的长度。
    """
    schema = {"password": {"type": "string", "minlength": 8, "maxlength": 20}}
    v = Validator(schema)
    
    print(v.validate({"password": "******"}))
    print(v.errors)

    print(v.validate({"password": "******"}))
    print(v.errors)
def regex_example():
    """对字符串进行正则匹配验证。
    """
    email_regex ="([a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*(@|\sat\s)(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(\.|\sdot\s))+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)" 
    schema = {"email": {"type": "string", "regex": email_regex}}
    v = Validator(schema)
    
    print(v.validate({"email": "*****@*****.**"}))
    
    print(v.validate({"email": "123456"}))
    print(v.errors)
def allow_unknown_example():
    """默认需要data中所有的key都要在schema中被预定义。而设置allow_unknown = True可以允许出现
    没有被预定义的key
    """
    schema = {"name": {"type": "string", "maxlength": 10}}
    v = Validator(schema)
    print(v.validate({"name": "john", "sex": "M"}))
    print(v.errors)
    
    v.allow_unknown = True
    print(v.validate({"name": "john", "sex": "M"}))
示例#8
0
def test_callable_validator():
    """
    Validator instance is callable, functions as a shorthand
    passthrough to validate()
    """
    schema = {'test_field': {'type': 'string'}}
    v = Validator(schema)
    assert v.validate({'test_field': 'foo'})
    assert v({'test_field': 'foo'})
    assert not v.validate({'test_field': 1})
    assert not v({'test_field': 1})
def max_min_example():
    """对于数值项, 可以限制其最大值和最小值。
    """
    schema = {"value": {"type": "number", "min": 0, "max": 1}}
    v = Validator(schema)
    
    print(v.validate({"value": 1.5}))
    
    print(v.validate({"value": -0.5}))
    print(v.errors)
    
    print(v.validate({"value": 1.5}))
    print(v.errors)
def function_based_custom_validators():
    """以函数的方式自定义你的validator
    """
    def validate_oddity(field, value, error):
        if not bool(value & 1):
            error(field, "Must be an odd number")
            
    schema = {"oddity": {"validator": validate_oddity}}
    v = Validator(schema)
    print(v.validate({"oddity": 10}))
    print(v.errors)
    
    print(v.validate({"oddity": 9}))
def allowed_example():
    """对list中的值的取值进行限制。
    
    我们可以通过设定::
    
        "allowed": [item1, item2, ...]
        
    使得list中的值必须是allowed中的元素
    """
    schema = {"role": {"type": "list", "allowed": ["agent", "client", "supplier"]}}
    v = Validator(schema)
    
    print(v.validate({"role": ["agent", "supplier"]}))
    print(v.validate({"role": ["agent", "boss"]}))
    print(v.errors)
示例#12
0
def test_readonly_field_first_rule():
    # test that readonly rule is checked before any other rule, and blocks.
    # See #63.
    schema = {
        'a_readonly_number': {
            'type': 'integer',
            'readonly': True,
            'max': 1
        }
    }
    v = Validator(schema)
    v.validate({'a_readonly_number': 2})
    # it would be a list if there's more than one error; we get a dict
    # instead.
    assert 'read-only' in v.errors['a_readonly_number'][0]
    def validate(self):

        """
        Validates the action_block based on the cerberus schema
        example:
        action_block :: sample ::
        - name: manipulate_inventory
          type: shell
          path: /tmp/shellscripts
          actions:
            - thisisshell.sh
        """

        schema = {
            'name': {'type': 'string', 'required': True},
            'type': {
                'type': 'string',
                'allowed': ['shell', 'subprocess']
            },
            'path': {'type': 'string', 'required': False},
            'context': {'type': 'boolean', 'required': False},
            'actions': {
                'type': 'list',
                'schema': {'type': 'string'},
                'required': True
            }
        }

        v = Validator(schema)
        status = v.validate(self.action_data)

        if not status:
            raise HookError("Invalid Syntax: {0}".format(str(v.errors)))
        else:
            return status
示例#14
0
文件: grupos.py 项目: VTacius/justine
class EsquemaGrupo():
    def __init__(self, *claves_requeridas):
        self.cadena = {'type': 'string'}
        self.correo = {'type': 'string', 'regex': "(?i)^[A-Z0-9._%!#$%&'*+-/=?^_`{|}~()]+@[A-Z0-9]+([.-][A-Z0-9]+)*\.[A-Z]{2,22}$"}
        self.cadena_enteros = {'type': 'string', 'regex': "[0-9]+"}
    
        esquema = {}
        esquema["cn"] = self.cadena.copy()
        esquema["type"] = self.cadena_enteros.copy()
        esquema["description"] = self.cadena.copy()
        esquema["mail"] = self.correo.copy()
        esquema["notes"] = self.cadena.copy()
        esquema = self._requeridor(esquema, claves_requeridas) 

        self.validador = Validator(esquema)

    def _requeridor(self, esquema, claves_requeridas):
        for clave in claves_requeridas: 
            esquema[clave]['required'] = True

        return esquema
    
    def validacion(self, contenido):
        if self.validador.validate(contenido):
            return contenido
        else:
            raise DatosException(self.validador.errors)
示例#15
0
    def __init__(self, environment_name, nova_descriptor_file=None):
        self._nova_descriptor_file = nova_descriptor_file or 'nova.yml'
        self._environment_name = environment_name
        self._environment = None
        self._codedeploy_app = None

        self.templates_used = dict()
        yaml.add_constructor("!include", yaml_include)

        with open(os.path.join(spec.__path__[0], 'nova_service_schema.yml'), 'r') as schemaYaml:
            schema = yaml.load(schemaYaml)

        v = Validator(schema)
        try:
            with open(self._nova_descriptor_file, 'r') as novaYaml:
                self.service_spec = yaml.safe_load(novaYaml)

            # Validate loaded dictionary
            valid = v.validate(self.service_spec)
            if not valid:
                raise NovaError("Invalid nova service descriptor file '%s': %s" % (self._nova_descriptor_file, v.errors))
            else:
                self.service = Service.load(self.service_spec)
                self.service_name = self.service.name
                self.service_port = self.service.port
                self.service_healthcheck_url = self.service.healthcheck_url
        except IOError:
            raise NovaError("No nova service descriptor found at '%s'" % self._nova_descriptor_file)
示例#16
0
文件: auth.py 项目: clayman74/wallet
async def registration(request: web.Request) -> Dict:
    payload = await base.get_payload(request)

    validator = Validator(schema=users.schema)
    if not validator.validate(payload):
        raise ValidationError(validator.errors)

    async with request.app['engine'].acquire() as conn:
        count = await conn.scalar(
            sqlalchemy.select([sqlalchemy.func.count()])
                .select_from(users.table)
                .where(users.table.c.login == payload['login'])
        )

        if count:
            raise ValidationError({'login': '******'})

        user = {
            'login': payload['login'],
            'password': users.encrypt_password(payload['password']),
            'created_on': datetime.now()
        }
        user = await create_instance(user, users.table, conn)

    return base.json_response({
        'id': user['id'],
        'login': user['login']
    }, status=201)
    def validate(self):

        """
        Validates the action_block based on the cerberus schema
        """

        schema = {
            'name': {'type': 'string', 'required': True},
            'type': {'type': 'string', 'allowed': ['ruby']},
            'path': {'type': 'string', 'required': False},
            'context': {'type': 'boolean', 'required': False},
            'actions': {
                'type': 'list',
                'schema': {'type': 'string'},
                'required': True
            }
        }

        v = Validator(schema)
        status = v.validate(self.action_data)

        if not status:
            raise HookError("Invalid syntax: {0}".format(str((v.errors))))
        else:
            return status
def allowed_for_single_value():
    """对于值进行限制, 只能使预定义的几个值中的一个。
    """
    schema = {"label": {"type": "integer", "allowed": [1, 2, 3]}}
    v = Validator(schema)
    
    print(v.validate({"label": 1}))
示例#19
0
文件: admin.py 项目: Msms-NJ/zual
 def validate(self):
     # pdb.set_trace()
     v = Validator(self.schema)
     status = v.validate(self.result)
     if not status:
         return v.errors
     return None
示例#20
0
def add_friend_to_group(id):
    user = current_user._get_current_object()  
    form = request.get_json()
    schema = {
                'id':{'type':'integer','empty':False},
            } 
    v = Validator(schema)   
    if v.validate(form) is False:
        return api_validation_error(v.errors)  
    #first see if this is a valid group
    group = FriendsGroup.query.filter(and_(FriendsGroup.user_id == user.id,FriendsGroup.id == id)).first()

    if group:
        #lets see if this is a confirmed friend of yours
        friend = Friends.query.filter(and_(Friends.user_id == user.id,Friends.friend_id == form['id'])).first()

        if friend:
            #lets see if this friend is not in this group
            if friend.group_id == group.id:
                return api_error("This friend is already in this group")
            else:
                friend.group_id = group.id
                meeple.db.session.commit()
                return api_package()
        else:
            return api_error("Friend does not exist")
    else:
        return api_error("This group does not exist.")
示例#21
0
def validate_cred(cred, f):
    v = Validator()
    valid = v.validate(cred, schema)
    for e in v.errors:
        logger.error("[validate_cred] Validation Error: %s, %s - %s" % (f, e, v.errors[e]))

    return valid
示例#22
0
文件: etl.py 项目: kianho/bxrec
def check_bx_users(fn):
    """Check the consistentcy of the BX-Users.csv file.

    """

    tab = etl.fromcsv(fn, delimiter=DELIMITER,
            quoting=QUOTE_ALL, encoding=ENCODING)

    v = Validator({
            "User-ID" : {
                "type" : "string",
                "regex" : USER_ID_PAT,
                "required" : True
            },
            "Location" : {
                "type" : "string",
                "required" : True
            },
            "Age" : {
                "type" : "string",
                "validator" : validate_age,
                "required" : True
            }
        })

    for row_num, r in enumerate(tab.dicts(), 1):
        is_valid = v.validate(r)
        if not is_valid:
            print "row %d -> %r, %r" % (row_num, v.errors, r)

    return
示例#23
0
 def _validate_page(self, page):
     schema = {'page': {'type': 'integer', 'min': 1}}
     v = Validator(schema)
     try:
         return v.validate({'page': int(page)})
     except Exception:
         return False
示例#24
0
文件: etl.py 项目: kianho/bxrec
def check_bx_book_ratings(fn):
    """Check the consistency of the BX-Book-Ratings.csv file.

    """

    tab = etl.fromcsv(fn, delimiter=DELIMITER,
            quoting=QUOTE_ALL, encoding=ENCODING)

    v = Validator({
            "User-ID" : {
                "type" : "string",
                "required" : True
            },
            "ISBN" : {
                "type" : "string",
                "required" : True
            },
            "Book-Rating" : {
                "type" : "string",
                "validator" : validate_rating,
                "required" : True
            }
        })

    for row_num, r in enumerate(tab.dicts(), 1):
        is_valid = v.validate(r)
        if not is_valid:
            print "row %d -> %r, %r" % (row_num, v.errors, r)

    return
示例#25
0
def confirm_friend():
    user = current_user._get_current_object()
    form = request.get_json()
    schema = {
        'id':{'type':'integer','empty':False}
    }
    v = Validator(schema)
    if v.validate(form) is False:
        return api_validation_error(v.errors)

    friend_request = FriendRequests.query.filter(FriendRequests.id == form['id']).first()
    if friend_request:

        #we are confirming this, so create friendships that last both ways.
        my_friend = Friends(user_id = user.id,friend_id = friend_request.friend_id)
        their_friend = Friends(user_id = friend_request.friend_id,friend_id = user.id)

        meeple.db.session.add(my_friend)
        meeple.db.session.add(their_friend)
        meeple.db.session.delete(friend_request) #remove this friend request
        meeple.db.session.commit()

        #now return who this friend is back to them.
        return api_package(data=my_friend.as_dict())
    else:
        return api_error("This request does not exist")
示例#26
0
文件: utils.py 项目: idserge7/eve
    def validate_filter(filter):
        for key, value in filter.items():
            if '*' not in allowed and key not in allowed:
                return "filter on '%s' not allowed" % key

            if key in ('$or', '$and', '$nor'):
                if not isinstance(value, list):
                    return "operator '%s' expects a list of sub-queries" % key
                for v in value:
                    if not isinstance(v, dict):
                        return "operator '%s' expects a list of sub-queries" \
                            % key
                    r = validate_filter(v)
                    if r:
                        return r
            else:
                if config.VALIDATE_FILTERS:
                    res_schema = config.DOMAIN[resource]['schema']
                    if key not in res_schema:
                        return "filter on '%s' is invalid"
                    else:
                        field_schema = res_schema.get(key)
                        v = Validator({key: field_schema})
                        if not v.validate({key: value}):
                            return "filter on '%s' is invalid"
                        else:
                            return None
def anyof_example():
    """定义: 值需要满足以下几个条件中的任意一个
    除了anyof, 类似的还有: allof, noneof, oneof
    """
    schema = {"value": {"type": "number", "anyof": [{"min": 0, "max": 10}, {"min": 100, "max": 1000}]}}
    v = Validator(schema)
    
    document = {"value": 1}
    print(v.validate(document, schema))
    
    document = {"value": 111}
    print(v.validate(document, schema))
    
    document = {"value": 11}
    print(v.validate(document, schema))
    print(v.errors)
示例#28
0
def validate_info():
    v = Validator()
    schema = {
        'title': {'required': True, 'type': 'string'},
        'version': {'required': True, 'type': 'string'},
        'description': {'type': 'string'},
        'termsOfService': {'type': 'string'},
        'contact': {
            'type': 'dict',
            'schema': {
                'name': {'type': 'string'},
                'url': {'type': 'string', 'validator': _validate_url},
                'email': {
                    'type': 'string',
                    'regex':
                    '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
                }
            }
        },
        'license': {
            'type': 'dict',
            'schema': {
                'name': {'type': 'string', 'required': True},
                'url': {'type': 'string', 'validator': _validate_url}
            }
        },
    }
    if eve_swagger.INFO not in app.config:
        raise ConfigException('%s setting is required in Eve configuration.' %
                              eve_swagger.INFO)

    if not v.validate(app.config[eve_swagger.INFO], schema):
        raise ConfigException('%s is misconfigured: %s' % (
            eve_swagger.INFO, v.errors))
示例#29
0
    def put(self, request, *args, **kwargs):
        team_info = json.loads(request.body.decode('utf-8'))

        validator = Validator(team_schema)
        if not validator.validate(team_info):
            return JsonResponse({'error': validator.errors})

        try:
            team = Team.objects.get(id=int(self.kwargs['team_id']))
        except ObjectDoesNotExist:
            return JsonResponse({'error': [{"Team ID": "Team not found for ID in request"}]})

        check_scope(request, team)

        report = team.report_set.first()
        if 'send_time' in team_info:
            report.survey_send_time = parser.parse(team_info['send_time']).replace(second=0, microsecond=0)
        if 'summary_time' in team_info:
            report.summary_send_time = parser.parse(team_info['summary_time']).replace(second=0, microsecond=0)
        if 'days_of_week' in team_info:
            rule = recurrence.Rule(recurrence.WEEKLY, byday=team_info['days_of_week'])
            rec = recurrence.Recurrence(rrules=[rule])
            report.recurrences = rec
        if 'name' in team_info:
            team.name = team_info['name']
        report.save()

        try:
            team.save()
        except IntegrityError:
            return JsonResponse({'error': {"name": _("team with this name already exists")}})

        team_dict = model_to_dict(team, exclude=['users'])
        team_dict['report'] = self.report_dict(team)
        return JsonResponse({'team': team_dict})
示例#30
0
def new_place():
    if flask.request.method == 'POST':
        v = Validator({
            'name': {'type': 'string', 'minlength': 3},
            'description': {'type': 'string', 'required': True},
            'location': {'type': 'string', 'required': True},
        })

        form = dict(flask.request.form.items())

        try:
            del form['g-recaptcha-response']
        except KeyError:
            pass

        if check_recaptcha() and v.validate(form):
            author = make_author()
            place = Place(v.document['name'], v.document['description'],
                          v.document['location'], author)
            place.scale = PlaceScale()
            flask.g.sql_session.add(place)
            flask.g.sql_session.commit()
            return flask.redirect(flask.url_for('.place', slug=place.slug))
        else:
            return flask.render_template('place/new.html', errors=v.errors)
    return flask.render_template('place/new.html')
示例#31
0
    def validate_add(self, report_id, data):
        """
            Validate add type

            :report_id: the report id we are modifying
            :data: the data we want to add to the report
        """
        validator = Validator(Schemas().add, allow_unknown=True)
        validation = validator.validate(data)
        if not validation:
            raise ValidationError(validator.errors)
        if data["type"] == "test":
            data = self.validate_step_add(report_id, data)
        elif data["type"] == "scenario":
            data = self.validate_scenario_add(report_id, data)
        return data
示例#32
0
        def wrapped_view(**kwargs):
            if "application/json" not in request.headers["Content-Type"]:
                return jsonify({
                    "message":
                    "Content-Type must be `application/json`."
                }), Status.BAD_REQUEST

            v = Validator(schema)
            g.data = {key: request.json[key] for key in schema.keys()}

            if not v.validate(g.data):
                return jsonify({
                    "message": f"Errors in request data: {v.errors}"
                }), Status.BAD_REQUEST

            return view(**kwargs)
示例#33
0
def validate_document(validation_schema, document):
    """
    Validate the format of a document, depending on the schema supplied in the class constructor.
    Args:
        validation_schema (dict): The schema the document has to follow.
        document (dict): The format specification.

    """
    validator = Validator(validation_schema)
    # Check if the document is valid.
    if not validator.validate(document):
        raise ApiUnprocessableEntity(
            u"The submitted document doesn't have the right format.",
            api_error_code=u"WRONG_DOCUMENT_FORMAT",
            payload=validator.errors)
    return None
示例#34
0
def validate_yml_file(yml_file, yml_file_dir):
    v = Validator(schema)
    if not v.validate(yml_file):
        print(v.errors)
        exit(
            'Please follow schema definition as declared in schema.py. Exiting.'
        )

    hasErrors = False
    for path in yml_file['paths']:
        if not _maps_to_folder(os.path.join(yml_file_dir, path)):
            hasErrors = True
            print("Provided path: " +
                  os.path.abspath(os.path.join(yml_file_dir, path)))
    if hasErrors:
        exit('No directory found for previous paths. Exiting.')
示例#35
0
class LoginValidator:
    def __init__(self):
        self.__schema = {
            'email': {
                'type': 'string',
                'required': True
            },
            'password': {
                'type': 'string',
                'required': True
            },
        }
        self.__validator = Validator()

    def validate_login(self, login: dict):
        return self.__validator.validate(login, self.__schema)
示例#36
0
    def put(self):
        data = request.get_json()
        validator = Validator(self.login_schema)
        is_valid = validator.validate(data)
        if not is_valid:
            return self.json_response(validator.errors, HTTPStatus.BAD_REQUEST)

        clean_data = validator.normalized(data)
        user = self.logged_user
        if 'fullname' in clean_data:
            user.fullname = clean_data['fullname']
        user.set_password(clean_data['password'])
        db.session.add(user)
        db.session.commit()

        return self.json_response({'id': user.id}, HTTPStatus.ACCEPTED)
示例#37
0
 def validateData(cls, data, _request):
     schema = {
         'category_name': {
             'required': True,
             'empty': False,
             'type': 'string'
         },
         'logo': {
             'required': True,
             'empty': False,
             'type': 'string'
         }
     }
     v = Validator(schema)
     v.allow_unknown = True
     return True if (v.validate(data)) else v.errors
示例#38
0
 def validate(self, schema, payload):
     try:
         check_data = Validator(schema)
         validate = check_data.validate(payload)
         status = None
         message = None
         if (validate == True):
             status = "success"
             message = payload
         else:
             status = "error"
             error_data = {i for i in check_data.errors}
             message = f"""Invalid Paramerters found for: '{error_data}'."""
         return {"status": status, "message": message}
     except BaseException as error:
         return {"status": "error", "message": error}
def test_list_all_links_not_empty_body(purge_all_links, create_shortcut_link):
    response = requests.get(url='http://localhost:8888/admin/all_links')

    v = Validator({
        'links': {
            'type': 'dict',
            'schema': {
                str(create_shortcut_link.json()['id']): {
                    'type': 'string',
                    'allowed':
                    ['https://github.com/Yurasb/url_shortener_testing']
                }
            }
        }
    })
    assert v.validate(response.json()), v.errors
示例#40
0
    def delete(self):
        schema = {'name': {'type': 'string', 'empty': False, 'required': True}}
        v = Validator(schema, allow_unknown=True)
        if not v.validate(self.__dict__):
            raise NonRecoverableError("Object schema not valid")

        response = self.fm_client.delete_path(name=self.name)
        ctx.logger.debug("Path {} {} response: {} {}".format(
            'DELETE', self.name, response['status_code'], response['content']))
        if response['status_code'] != 200:
            raise NonRecoverableError(
                "Delete Path {0} failed with response {1} {2}".format(
                    self.name, response['status_code'], response['content']))
        else:
            ctx.logger.info("Delete Path {} response successful: {} {}".format(
                self.name, response['status_code'], response['content']))
示例#41
0
 def create(cls, postId=None):
     request_data = return_request_data()
     coll = db[cls.collection]
     v = Validator(cls.schema)
     if not v.validate(request_data):
         return make_error(status=400, description=v.errors)
     else:
         if cls.__name__ == "User":
             if users.find_one({"username": request_data.get('username')}) is not None:
                 return make_error(400, description="Username is exist, please choose another.")
             request_data["birthday"] = datetime.strptime(
                 request_data.get("birthday"), format_str)
         result = coll.insert_one(request_data)
         post = coll.find_one({'_id': result.inserted_id})
         logger.warn('Update %r', post)
         return Utils.return_jsonify(post)
示例#42
0
def validate_initialisation_request(parameters):
    """Validate parameters for initialisation request

    Args:
        parameters (dict): The request parameters in the initialisation request

    Returns:
        boolean: Returns True if the validation is a success
    """
    logger.info("Validating initialisation parameters")
    initialisation_validator = Validator(initialisation_parameters)
    result = initialisation_validator.validate(parameters)
    if result:
        return True
    logger.error("Validation Failed: {0}".format(initialisation_validator.errors))
    raise BadRequest
示例#43
0
def validate_file(filename, dump=False):
    """
    Auxiliary function, validate file

    :return dict representing the YAML document. NOTE that the
    returned dict hasn't removed any 'additional' key from the original
    file.
    """
    validated = False  # reflect whether the overall process suceeded
    cyan("Validating " + str(filename) + "...")
    doc = None
    try:
        with open(click.format_filename(filename), 'r') as stream:
            try:
                doc = yaml.load(stream, Loader=yaml.FullLoader)
            except yaml.YAMLError as exception:
                raise exception
    except FileNotFoundError:
        red("File " + str(filename) + " not found")

    v = Validator(SCHEMA, allow_unknown=True)  # allow unknown values
    if doc:
        # print(MyNormalizer().normalized(doc, SCHEMA))
        if not v.validate(doc, SCHEMA):
            # print(v.errors)
            for key in v.errors.keys():
                print("\t" + str(key) + ": ", end='')
                red("not valid", end='')
                print(': ' + str(v.errors[key]))
        else:
            # print(v.validated(doc))
            # valid_documents = [x for x in v.validated(doc)]
            # for document in valid_documents:
            #     print("\t" + str(document) + ": ", end='')
            #     green("valid")
            green("Validated successfully!")
            validated = True
    # else:
    #     red("file to validate not processed correctly!")

    if dump:
        print(json.dumps(v.document, indent=4, default=default))
        # flaw = Flaw(v.document)
        # print the final document after validations and normalizations
        # print(flaw)
        # print(flaw.yml())
    return validated, v.document
示例#44
0
def validate(raw):
    schema = {
        "version": {
            "type": "string",
            "allowed": ["1.0"]
        },
        "athletes": {
            "type": "list",
            "minlength": 1,
            "schema": {
                "type": "dict",
                "schema": {
                    "born": {
                        "type": "integer",
                        "min": 1900,
                        "max": 2100
                    },
                    "club": {
                        "type": "string",
                        "minlength": 4,
                        "maxlength": 4,
                        "required": False
                    },
                    "id": {
                        "type": "integer",
                        "required": False
                    },
                    "name": {
                        "type": "string"
                    },
                    "sex": {
                        "type": "string",
                        "allowed": ["f", "m"]
                    },
                    "surname": {
                        "type": "string"
                    }
                }
            }
        }
    }

    v = Validator(schema, require_all=True)
    if not v.validate(raw):
        print(v.errors)
        raise Exception("Athletes file does not validate")
    return v.document
def patch_site_settings_values(**kwargs):
    body = kwargs.get('body')
    email = kwargs.get('email')

    settings = {
        'site_visibility': body.get('site_visibility', ''),
        'title': body.get('title', ''),
        'fontsize': body.get('fontsize', ''),
        'coloraccent': body.get('coloraccent', ''),
        'isdarkmode': body.get('isdarkmode', ''),
        'description': body.get('description', ''),
        'copyright': body.get('copyright', ''),
        'websiteurl': body.get('websiteurl', ''),
        'brandmail': body.get('brandmail', ''),
        'brandlogourl': body.get('brandlogourl', ''),
        'faviconurl': body.get('faviconurl', ''),
        'appiconurl': body.get('appiconurl', '')
    }

    schema_validation = Validator(PATCH_ADMIN_SETTINGS_SCHEMA)

    remove_empty_values_of_dict(settings)

    resp = None
    if schema_validation.validate(settings):

        query = dynamic_update_query(
            settings, 'settings.site_settings', '', email)

        print(query)

        msg = {
            'body': {
                'action': 'run',
                'queries': [query]
            }
        }

        method, result = db_handler(msg)

        print(result)

        if method == 'ok':
            resp = settings
    else:
        return bad_request(schema_validation.errors)
    return ok(resp)
示例#46
0
    async def _handle_open_stream(self, message):
        log.info("Handling %s", message)
        route = message['route']
        data = message['message']
        stream = message['stream']
        session_key = message.get('session')
        log.info("Opening stream %s", stream)

        # Make sure requested route exists
        if route not in self.routes:
            await self.send_error(stream, 404, 'Route not found')
            return

        # Make sure user doesn't have too many streams open
        if len(self.generators) >= self.MAX_STREAMS:
            await self.send_error(stream, 400, 'Too many open streams')
            return

        # Check message schema
        if self.routes[route].schema:
            validator = Validator(schema=self.routes[route].schema)
            if not validator.validate({"message": data}):
                log.error("Invalid message received: %s", validator.errors, extra={'body': message})
                await self.send_error(stream, 400, "Bad request", error_fields=validator.errors)
                return

        # Get a DB connection and a new session.
        async with self.db.acquire() as db_connection, self.sessions.acquire(session_key) as session:
            req = Request(
                db=db_connection,
                session=session,
                message=data,
                stream=stream,
                route=route)

            response = Response(
                protocol=self,
                stream=stream)

            try:
                fn = self.routes[route].handler
                async with db_connection.transaction():
                    await fn(req, response)
                    await response.close()
            except Exception as e:
                log.exception("Handler failed: %s", str(e), extra={'body': message})
                await self.send_error(stream, 500, 'Internal server error')
示例#47
0
                    def recursive_validate_filter(key, value, schema):
                        if key not in schema:
                            base_key, _, sub_keys = key.partition('.')
                            if sub_keys and base_key in schema:
                                # key is the composition of base field and
                                # sub-fields
                                sub_schemas = get_sub_schemas(schema[base_key])
                                for sub_schema in sub_schemas:
                                    if recursive_validate_filter(
                                            sub_keys, value, sub_schema):
                                        return True

                            return False
                        else:
                            field_schema = schema.get(key)
                            v = Validator({key: field_schema})
                            return v.validate({key: value})
示例#48
0
def cli():
    """Validate input data and call the appropriate subcommand with necessary arguments"""
    args = parse_args(sys.argv[1:])
    logger.setLevel(args.pop('log') * 10)
    cmd = args.pop('command')

    # read in configuration file and destination folder
    config = json.load(open(args.get('config')))
    dest_folder = args.get('dest')

    # create destination folder if necessary
    if not op.isdir(dest_folder):
        makedirs(dest_folder)

    # validate configuration file
    v = Validator(schema)
    valid = v.validate(config)
    if not valid:
        raise Exception(v.errors)

    # custom validation for top level keys
    # require either: country & bounding_box or geojson
    if 'geojson' not in config.keys() and not (
            'country' in config.keys() and 'bounding_box' in config.keys()):
        raise Exception(
            'either "geojson" or "country" and "bounding_box" must be present in the configuration JSON'
        )

    # for geojson, overwrite other config keys to correct labeling
    if 'geojson' in config.keys():
        config['country'] = op.splitext(op.basename(config.get('geojson')))[0]
        config['bounding_box'] = get_bounds(
            json.load(open(config.get('geojson'), 'r')))

    if cmd == 'download':
        download_mbtiles(dest_folder=dest_folder, **config)
    elif cmd == 'labels':
        sparse = args.get('sparse', False)
        make_labels(dest_folder=dest_folder, sparse=sparse, **config)
    elif cmd == 'preview':
        number = args.get('number')
        preview(dest_folder=dest_folder, number=number, **config)
    elif cmd == 'images':
        download_images(dest_folder=dest_folder, **config)
    elif cmd == 'package':
        package_directory(dest_folder=dest_folder, **config)
示例#49
0
def run(schema_filename: str, config_filename: str) -> None:
    schema = yaml.load(read_yaml(schema_filename), Loader=yaml.FullLoader)
    config_data = yaml.load(read_yaml(config_filename), Loader=yaml.FullLoader)

    v = Validator(schema)
    result = v.validate(config_data)

    if not result:
        print(v.errors)
        sys.exit(1)

    config = ConfigClassDict(config_data)
    print(config)
    print(config.version)
    print(config.jobs.build.docker)
    print(config.jobs.build.docker.image)
    print(config.workflows.build_and_test.jobs)
示例#50
0
 def get_config(self):
     """
     Reads the configuration from the config.yaml file and verifies that is compilant with the validation schema
     :return:
     Dictionary with the configuration or None if the config file is invalid
     """
     data = Config.read_yaml(self.config_file)
     schema = Config.read_yaml(self.validation_schema)
     try:
         validator = Validator(schema)
         if validator.validate(data):
             return data
         else:
             return None
     except Exception as e:
         print("ERROR: Exception trying to validate configuration file:" +
               str(e))
示例#51
0
def update_transaction(code):
    validator = Validator(Transaction.schema)

    payload = request.get_json()

    if not validator.validate(payload):
        return make_response(jsonify(validator.errors), 400)

    transaction = Transaction.get(code)

    transaction = Transaction.from_json(payload, transaction)

    session.add(transaction)
    session.commit()
    session.flush()

    return make_response(jsonify(transaction.to_json()), 200)
示例#52
0
def validate_api_get_user_info(data_input):
    """
    :param data_input:
    :return:
    """
    schema = {
        'email': {
            'required': True,
            'type': 'string',
            'regex': '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
        },
    }
    v = Validator(schema)
    if v.validate(data_input):
        return True, data_input
    else:
        return False, v.errors
示例#53
0
 def edit_book_validation(self, dict_data):
     '''edit book data validation function using CERBERUS'''
     schema = {
         'title': {
             'type': 'string',
             'required': False,
             'empty': True,
             'regex': '^[a-zA-Z0-9 ]+$',
             'maxlength': 25,
             'minlength': 4},
         'author': {
             'type': 'string',
             'required': False,
             'regex': '^[a-zA-Z0-9 ]+$',
             'empty': True,
             'maxlength': 25,
             'minlength': 4},
         'genre': {
             'type': 'string',
             'required': False,
             'regex': '^[a-zA-Z0-9 ]+$',
             'empty': True,
             'maxlength': 20,
             'minlength': 4},
         'copies': {
             'type': 'string',
             'required': False,
             'regex': '^[0-9]+$',
             'empty': False,
             'maxlength': 1,
             'minlength': 3},
         'isbn': {
             'type': 'string',
             'required': False,
             'regex': '^[0-9]+$',
             'empty': False,
             'maxlength': 13,
             'minlength': 13},
         'description': {
             'type': 'string',
             'required': False,
             'maxlength': 200,
             'minlength': 4}}
     v = Validator(schema)
     v.allow_unknown = True
     return v.validate(dict_data)
示例#54
0
def validate_verification(_in):
    schema = {
        "verification": {
            "type": "list",
            "maxlength": 1,
            "minlength": 1,
            "nullable": False,
            "required": True,
            "items": [{
                "type": "string"
            }],
        },
    }

    v = Validator(schema)
    is_valid = v.validate(_in)
    return is_valid, v.errors
示例#55
0
def validator(request, schema, target):
    """
    Run validation using Cerberus and a given schema and target dictionary;
    raises an http exception with details when validation fails.
    """
    validator = Validator(schema)
    if not validator.validate(target):
        raise BadRequest(
            response={
                "notification": {
                    "body":
                    "Some of the provided information is invalid. Please correct the errors and try again.",
                    "type": "popup_notifications",
                    "delay": 5,
                },
            })
    return request
示例#56
0
 def _validate_and_sanitize(self, method, params):
     """
     This internal method is used to validate and sanitize the parameters
     expected by the given method.  These parameters are validated and
     sanitized according to a schema provided by a method  following the
     naming convention: '_validator_{method_name}'.
     :param method:
     :param params:
     :return:
     """
     method_name = method.__name__
     schema = self._get_schema_for_method(method_name)
     v = Validator(schema, purge_unknown=True)
     if v.validate(params):
         return v.document
     _logger.error("BadRequest %s", v.errors)
     raise UserError(_('Invalid Form'))
示例#57
0
def test_stats_redirected_body(purge_all_links, create_shortcut_link,
                               redirect_by_id):
    response = requests.post(url='http://localhost:8888/stats',
                             data=json.dumps(
                                 {'id': create_shortcut_link.json()['id']}))

    v = Validator({
        'last_redirected': {
            'nullable': False,
            'type': 'float'
        },
        'redirects_count': {
            'type': 'integer',
            'allowed': [1]
        }
    })
    assert v.validate(response.json()), v.errors
示例#58
0
def create():
  # Assume comment belongs to the temporary test user
  user = get_temp_test_user()
  if user:
    # Validate comment attributes
    v = Validator(comment_schema)
    if v.validate(request.get_json()):
      attrs = v.document
      # Insert valid comment in DB
      if comments_collection.insert_one(attrs).inserted_id:
        return jsonify({}), HTTP_201_CREATED
      else:
        return jsonify({}), HTTP_500_INTERNAL_SERVER_ERROR
    else:
      return jsonify(v.errors), HTTP_422_UNPROCESSABLE_ENTITY
  else:
    return jsonify({'error': 'Test user doesn\'t exist'}), HTTP_422_UNPROCESSABLE_ENTITY
示例#59
0
def completed():
    out = request.json
    schema = {
        "timer_id": {"type": "integer", "required": True},
        "current_time": {"type": "integer", "required": True},
    }
    validator = Validator(schema)
    if not validator.validate(out):
        return {"MSG": "INVALID REQUEST"}
    timestamp = datetime.datetime.fromtimestamp(out["current_time"])
    timestamp = timestamp.strftime("%Y-%m-%d %H:%M:%S")
    query = f"UPDATE pomodoro SET stop_time = TIMESTAMP '{timestamp}' WHERE pomodoro_id = {out['timer_id']} RETURNING *;"
    print(query)
    db.execute(query)
    query = f"UPDATE pomodoro SET status = 'completed' WHERE pomodoro_id = {out['timer_id']} RETURNING *;"
    out = next(db.execute(query))
    return {i: j for i, j in out.items()}
示例#60
0
def validate_document(document, schema, **kwargs):
    """
    Validate `document` against provided `schema`
    :param document: document for validation
    :type document: dict
    :param schema: validation schema
    :type schema: dict
    :param kwargs: additional arguments for `Validator`
    :return: normalized and validated document
    :rtype: dict
    :raise: `BadRequest` if `document` is not valid
    """

    validator = Validator(schema, **kwargs)
    if not validator.validate(document):
        raise BadRequest(validator.errors)
    return validator.document