Exemple #1
0
    def __init__(self,
                 schema,
                 types=(),
                 resolver=None,
                 format_checker=None,
                 skip_meta_validation=False):
        super(PandelVisitor, self).__init__(schema, types, resolver,
                                            format_checker)

        self._types.update({
            "number": (
                numbers.Number, np.number
            ),  ## type(np.nan) == builtins.float! FIXME, are numpy-numbers --> json-types OK??
            "integer": (int, np.integer),
            "boolean": (bool, np.bool_),  #, np.bool8),
            "array": (list, tuple, np.ndarray),
            "object": (dict, pd.DataFrame, pd.Series)
        })

        ## Setup Draft4/3 validation
        #
        # Meta-validate schema
        #    with original validators (and not self)
        #    because this class inherits an empty (schema/rules) validator.
        validator_class = jsonschema.validators.validator_for(
            schema)  ## Falls back to 'Draft4' if no `$schema` exists.
        self.VALIDATORS = validator_class.VALIDATORS.copy()
        self.META_SCHEMA = validator_class.META_SCHEMA
        self.VALIDATORS.update({
            'items':
            PandelVisitor._rule_items,
            'additionalProperties':
            PandelVisitor._rule_additionalProperties,
            'additionalItems':
            PandelVisitor._rule_additionalItems,
        })
        if validator_class == Draft3Validator:
            self.VALIDATORS.update({
                'properties':
                PandelVisitor._rule_properties_draft3,
            })
        else:
            self.VALIDATORS.update({
                'properties':
                PandelVisitor._rule_properties_draft4,
                'required':
                PandelVisitor._rule_required_draft4,
            })

        self.old_scopes = []

        ## Cannot use ``validator_class.check_schema()`` because
        #    need to relay my args to ``validator_class.__init__()``.
        # Even better use myself, that i'm fatser (kind of...).
        if not skip_meta_validation:
            for error in self.iter_errors(schema, validator_class.META_SCHEMA):
                raise SchemaError.create_from(error)
Exemple #2
0
    def __init__(self, schema, types=(), resolver=None, format_checker=None, skip_meta_validation=False):
        super(PandelVisitor, self).__init__(
            schema, types, resolver, format_checker)

        self._types.update({
            # type(np.nan) == builtins.float! FIXME, are numpy-numbers -->
            # json-types OK??
            "number":   (numbers.Number, np.number),
            "integer":  (int, np.integer),
            "boolean":  (bool, np.bool_),  # , np.bool8),
            "array":    (list, tuple, np.ndarray),
            "object":  (dict, pd.DataFrame, pd.Series)
        })

        # Setup Draft4/3 validation
        #
        # Meta-validate schema
        #    with original validators (and not self)
        #    because this class inherits an empty (schema/rules) validator.
        # Falls back to 'Draft4' if no `$schema` exists.
        validator_class = jsonschema.validators.validator_for(schema)
        self.VALIDATORS = validator_class.VALIDATORS.copy()
        self.META_SCHEMA = validator_class.META_SCHEMA
        self.VALIDATORS.update({
            'items':                PandelVisitor._rule_items,
            'additionalProperties': PandelVisitor._rule_additionalProperties,
            'additionalItems':      PandelVisitor._rule_additionalItems,
        })
        if validator_class == Draft3Validator:
            self.VALIDATORS.update({
                'properties':           PandelVisitor._rule_properties_draft3,
            })
        else:
            self.VALIDATORS.update({
                'properties':           PandelVisitor._rule_properties_draft4,
                'required':             PandelVisitor._rule_required_draft4,
            })

        self.old_scopes = []

        # Cannot use ``validator_class.check_schema()`` because
        #    need to relay my args to ``validator_class.__init__()``.
        # Even better use myself, that i'm fatser (kind of...).
        if not skip_meta_validation:
            for error in self.iter_errors(schema, validator_class.META_SCHEMA):
                raise SchemaError.create_from(error)
Exemple #3
0
 def check_schema(cls, schema):
     for error in cls(cls.META_SCHEMA).iter_errors(schema):
         raise SchemaError.create_from(error)
 def check_schema(cls, schema):
     for error in cls(cls.META_SCHEMA).iter_errors(schema):
         raise SchemaError.create_from(error)
Exemple #5
0
 def check_schema(self, schema):
     from jsonschema.exceptions import SchemaError
     for error in default_meta_validator.iter_errors(schema):
         raise SchemaError.create_from(error)
Exemple #6
0
    def validate_against(self, instance, schemauris=[], strict=False):
        """
        validate the instance against each of the schemas identified by the 
        list of schemauris.  For the instance to be considered valid, it 
        must validate against each of the named schemas.  $extensionSchema
        properties within the instance are ignored.  

        :argument instance:  a parsed JSON document to be validated.
        :argument list schemauris:  a list of URIs of the schemas to validate
                                    against.  

        :return list: a list of encountered errors in the form of exceptions; 
                      otherwise, an empty list if the instance is valid against
                      all schemas.
        """
        if isinstance(schemauris, (str, unicode)):
            schemauris = [schemauris]
        schema = None
        out = []
        for uri in schemauris:
            val = self._validators.get(uri)
            if not val:
                (urib, frag) = self._spliturifrag(uri)
                schema = self._schemaStore.get(urib)
                if not schema:
                    try:
                        schema = self._loader(urib)
                    except KeyError as e:
                        ex = MissingSchemaDocument(
                            "Unable to find schema document for " + urib)
                        if strict:
                            out.append(ex)
                        continue

                resolver = jsch.RefResolver(uri,
                                            schema,
                                            self._schemaStore,
                                            handlers=self._handler)

                if frag:
                    try:
                        schema = resolver.resolve_fragment(schema, frag)
                    except RefResolutionError as ex:
                        exc = RefResolutionError(
                            "Unable to resolve fragment, {0} from schema, {1} ({2})"
                            .format(frag, urib, str(ex)))
                        out.append(exc)
                        continue

                cls = jsch.validator_for(schema)

                # check the schema for errors
                scherrs = [ SchemaError.create_from(err) \
                            for err in cls(cls.META_SCHEMA).iter_errors(schema) ]
                if len(scherrs) > 0:
                    out.extend(scherrs)
                    continue

                val = cls(schema, resolver=resolver)

            out.extend([err for err in val.iter_errors(instance)])

            self._validators[uri] = val
            self._schemaStore.update(val.resolver.store)

        return out