示例#1
0
    def validate(self, document, schema=None, update=False, context=None):
        """
        Main validation method which simply tries to validate against cerberus
        schema and if it does not fail, repeats the same against mongoengine
        validation machinery.
        """

        # call default eve's validator
        if not Validator.validate(self, document, schema, update, context):
            return False

        # validate using mongoengine field validators
        if self.resource:
            model_cls = app.data.models[self.resource]
            doc = model_cls(**document)
            # rewind all file-like's
            for attr, field in iteritems(model_cls._fields):
                if isinstance(field, FileField) and attr in document:
                    document[attr].stream.seek(0)
            try:
                doc.validate()
            except ValidationError as e:
                for field_name, error in e.errors.items():
                    self._error(field_name, str(e))
                return False

        return True
示例#2
0
    def validate(self, document, schema=None, update=False, context=None):
        """
        Main validation method which simply tries to validate against cerberus
        schema and if it does not fail, repeats the same against mongoengine
        validation machinery.
        """
        # call default eve's validator
        if not Validator.validate(self, document, schema, update, context):
            return False

        # validate using mongoengine field validators
        if self.resource and context is None:
            model_cls = app.data.models[self.resource]

            # We must translate any database field names to their corresponding
            # MongoEngine names before attempting to validate them.
            translate = lambda x: model_cls._reverse_db_field_map.get(x, x)
            document = {translate(k): document[k] for k in document}

            doc = model_cls(**document)
            # rewind all file-like's
            for attr, field in iteritems(model_cls._fields):
                if isinstance(field, FileField) and attr in document:
                    document[attr].stream.seek(0)
            try:
                doc.validate()
            except ValidationError as e:
                for field_name, error in e.errors.items():
                    self._error(field_name, str(e))
                return False

        return True
示例#3
0
    def validate(self, document, schema=None, update=False, context=None):
        """
        Main validation method which simply tries to validate against cerberus
        schema and if it does not fail, repeats the same against mongoengine
        validation machinery.
        """
        # call default eve's validator
        if not Validator.validate(self, document, schema, update, context):
            return False

        # validate using mongoengine field validators
        if self.resource and context is None:
            model_cls = app.data.models[self.resource]

            # We must translate any database field names to their corresponding
            # MongoEngine names before attempting to validate them.
            translate = lambda x: model_cls._reverse_db_field_map.get(x, x)
            document = {translate(k): document[k] for k in document}

            doc = model_cls(**document)
            # rewind all file-like's
            for attr, field in iteritems(model_cls._fields):
                if isinstance(field, FileField) and attr in document:
                    document[attr].stream.seek(0)
            try:
                doc.validate()
            except ValidationError as e:
                for field_name, error in e.errors.items():
                    self._error(field_name, str(e))
                return False

        return True
示例#4
0
def LiteralSchemaInput(name, schema, type_=None):
    validator = Validator({"value": schema})

    def validate_schema(self, value):
        return validator.validate({"value": value})

    params = {
        "validate_schema": validate_schema,
        "type": type_,
    }
    return type(name + "InputWidget", (LiteralSchemaInputBase, ), params)
示例#5
0
    def match_iter(self):

        # short circuit on empty
        if self.match_df is None:
            return

        # create validator.
        v = Validator(self.match_schema)

        # names cols.
        cis = zip(range(len(self.match_df.columns)),
                  list(self.match_df.columns))

        # strip out underscore fields.
        tmp = []
        for a, b in cis:
            if b[0] == "_" and b != "_id_y":
                continue
            tmp.append((a, b))
        cis = tmp

        # loop over each entry.
        for row in self.match_df.itertuples():

            # create dictionary.
            tmp = {}
            for i, key in cis:

                # extract value.
                val = row[i + 1]

                # convert nan
                if pd.isnull(val):
                    val = None

                # convert special key.
                if key == "_id_y":
                    key = "GENOMIC_ID"

                # save it to dict.
                tmp[key] = val

            # yield the match
            yield tmp
示例#6
0
    def validate(self, document, schema=None, update=False, context=None):
        """
        Main validation method which simply tries to validate against cerberus
        schema and if it does not fail, repeats the same against mongoengine
        validation machinery.
        """

        # fix timestamp
        if app.config["DATE_CREATED"] in list(self.schema.keys()):
            now = get_utc_time()
            if not update:
                document[app.config["DATE_CREATED"]] = document.get(
                    app.config["DATE_CREATED"], now)
            document[app.config["LAST_UPDATED"]] = now

        # call default eve's validator
        if not Validator.validate(self, document, schema, update, context):
            return False

        # validate using mongoengine field validators
        if self.resource:
            model_cls = app.data.models[self.resource]
            translate = lambda x: model_cls._reverse_db_field_map.get(x)
            new_document = dict()
            for field, value in document.items():
                field_copy = field.split(".")
                field_copy[0] = translate(field_copy[0])
                if None not in field_copy:
                    new_document[".".join(field_copy)] = value
            document = new_document
            doc = model_cls(**document)
            # rewind all file-like's
            for attr, field in iteritems(model_cls._fields):
                if isinstance(field, FileField) and attr in document:
                    document[attr].stream.seek(0)
            try:
                doc.validate()
            except ValidationError as e:
                for field_name, error in e.errors.items():
                    self._error(field_name, str(e))
                return False

        return True
示例#7
0
 def validate(self, document, schema=None, update=False):
     """
     Main validation method which simply tries to validate against cerberus
     schema and if it does not fail, repeats the same against mongoengine
     validation machinery.
     """
     # call default eve's validator
     if not Validator.validate(self, document, schema, update):
         return False
     # validate using mongoengine field validators
     model_cls = app.data.models[self.resource]
     doc = model_cls(**document)
     # rewind all file-like's
     for attr, field in iteritems(model_cls._fields):
         if isinstance(field, FileField) and attr in document:
             document[attr].stream.seek(0)
     try:
         doc.validate()
     except ValidationError as e:
         for field_name, error in e.errors.items():
             self._error(field_name, str(e))
         return False
     return True