Example #1
0
    def test_validate_schema_property_exception(self):
        """Test validate_schema validation exception handling."""
        invalid_property_schema = PropertySchema()
        invalid_property_schema.type = 'UNKNOWN'

        self.maxDiff = None
        self.assertRaisesRegexp(
            ValidationException,
            r"""The value "UNKNOWN" for "type" not in enumeration \[.*\].""",
            meta_type.validate_property_schema, invalid_property_schema)

        value_errors = meta_type.validate_property_schema(
            invalid_property_schema, raise_validation_exception=False)
        self.assertEqual(1, len(value_errors))
        self.assertTrue(value_errors[0].startswith(
            'The value "UNKNOWN" for "type" not in enumeration'))
Example #2
0
    def test_validate_schema_property_exception(self):
        """Test validate_schema validation exception handling."""
        invalid_property_schema = PropertySchema()
        invalid_property_schema.type = 'UNKNOWN'

        self.maxDiff = None
        self.assertRaisesRegexp(
            ValidationException,
            r"""The value "UNKNOWN" for "type" not in enumeration \[.*\].""",
            meta_type.validate_property_schema, invalid_property_schema)

        value_errors = meta_type.validate_property_schema(
            invalid_property_schema,
            raise_validation_exception=False)
        self.assertEqual(1, len(value_errors))
        self.assertTrue(value_errors[0].startswith(
            'The value "UNKNOWN" for "type" not in enumeration'))
Example #3
0
def validate_schema(candidate_schema, raise_validation_exception=True):
    """Validate a given :class:`SchemaType`.

    This method will iterate through all of the
    :class:`ontic.meta_type.PropertySchema` and validate that each definition
    is valid.  The method will collect all of the errors and return those as
    a list of strings or raise a
    :class:`ontic.validation_exception.ValidationException`. The switch in
    behavior is determined by the *raise_validation_exception*

    :param candidate_schema: The schema to be validated.
    :type candidate_schema: :class:`SchemaType`
    :param raise_validation_exception: If True, then *validate_schema* will
        throw a *ValidationException* upon validation failure. If False, then a
        list of validation errors is returned. Defaults to True.
    :type raise_validation_exception: bool
    :return: If no validation errors are found, then *None* is
        returned. If validation fails, then a list of the errors is returned,
        if the *raise_value_error* is not set to True.
    :rtype: list<str>, None
    :raises ValueError: *candidate_schema* is None, or not of type
        :class:`SchemaType`.
    :raises ValidationException: A property of *candidate_schema* does not
        meet schema requirements.
    """
    if candidate_schema is None:
        raise ValueError('"candidate_schema" must be provided.')
    if not isinstance(candidate_schema, SchemaType):
        raise ValueError('"candidate_schema" must be of SchemaType.')

    value_errors = []
    for candidate_property_schema in candidate_schema.values():
        value_errors.extend(
            meta_type.validate_property_schema(candidate_property_schema,
                                               False))

    if value_errors and raise_validation_exception:
        raise ValidationException(value_errors)

    return value_errors
Example #4
0
def validate_schema(candidate_schema, raise_validation_exception=True):
    """Validate a given :class:`SchemaType`.

    This method will iterate through all of the
    :class:`ontic.meta_type.PropertySchema` and validate that each definition
    is valid.  The method will collect all of the errors and return those as
    a list of strings or raise a
    :class:`ontic.validation_exception.ValidationException`. The switch in
    behavior is determined by the *raise_validation_exception*

    :param candidate_schema: The schema to be validated.
    :type candidate_schema: :class:`SchemaType`
    :param raise_validation_exception: If True, then *validate_schema* will
        throw a *ValidationException* upon validation failure. If False, then a
        list of validation errors is returned. Defaults to True.
    :type raise_validation_exception: bool
    :return: If no validation errors are found, then *None* is
        returned. If validation fails, then a list of the errors is returned,
        if the *raise_value_error* is not set to True.
    :rtype: list<str>, None
    :raises ValueError: *candidate_schema* is None, or not of type
        :class:`SchemaType`.
    :raises ValidationException: A property of *candidate_schema* does not
        meet schema requirements.
    """
    if candidate_schema is None:
        raise ValueError('"candidate_schema" must be provided.')
    if not isinstance(candidate_schema, SchemaType):
        raise ValueError('"candidate_schema" must be of SchemaType.')

    value_errors = []
    for candidate_property_schema in candidate_schema.values():
        value_errors.extend(
            meta_type.validate_property_schema(
                candidate_property_schema, False))

    if value_errors and raise_validation_exception:
        raise ValidationException(value_errors)

    return value_errors