Ejemplo n.º 1
0
def languagetag_validator(node, cstruct):
    """
    This validator validates a languagetag.

    The validator will check if a tag is a valid IANA language tag. The the
    validator is informed that this should be a new language tag, it will also
    check if the tag doesn't already exist.

    :param colander.SchemaNode node: The schema that's being used while validating.
    :param cstruct: The value being validated.
    """
    request = node.bindings['request']
    languages_manager = request.data_managers['languages_manager']
    new = node.bindings['new']
    errors = []
    language_tag = cstruct['id']

    if new:
        languagetag_checkduplicate(node['id'], language_tag, languages_manager,
                                   errors)
    languagetag_isvalid_rule(node['id'], language_tag, errors)

    if len(errors) > 0:
        raise ValidationError('Language could not be validated',
                              [e.asdict() for e in errors])
Ejemplo n.º 2
0
 def _validate_language(self, json_language, new):
     language = LanguageTag(validator=languagetag_validator).bind(
         request=self.request, new=new)
     try:
         return language.deserialize(json_language)
     except colander.Invalid as e:
         raise ValidationError('Language could not be validated',
                               e.asdict())
Ejemplo n.º 3
0
    def _validate_concept(self, json_concept, conceptscheme_id):
        from atramhasis.validators import (Concept as ConceptSchema,
                                           concept_schema_validator)

        concept_schema = ConceptSchema(
            validator=concept_schema_validator).bind(
                request=self.request, conceptscheme_id=conceptscheme_id)
        try:
            return concept_schema.deserialize(json_concept)
        except colander.Invalid as e:
            raise ValidationError('Concept could not be validated', e.asdict())
Ejemplo n.º 4
0
def languagetag_validator(node, cstruct):
    request = node.bindings['request']
    new = node.bindings['new']
    errors = []
    language_tag = cstruct['id']

    if new:
        languagetag_checkduplicate(node['id'], language_tag, request, errors)
    languagetag_isvalid_rule(node['id'], language_tag, errors)

    if len(errors) > 0:
        raise ValidationError('Language could not be validated',
                              [e.asdict() for e in errors])
Ejemplo n.º 5
0
    def _validate_conceptscheme(self, json_conceptscheme):
        from atramhasis.validators import (
            ConceptScheme as ConceptSchemeSchema,
            conceptscheme_schema_validator
        )

        conceptscheme_schema = ConceptSchemeSchema(
            validator=conceptscheme_schema_validator
        ).bind(
            request=self.request
        )
        try:
            return conceptscheme_schema.deserialize(json_conceptscheme)
        except colander.Invalid as e:  # pragma no cover
            # I doubt this code will ever be reached, keeping it here just in case
            raise ValidationError(
                'Conceptscheme could not be validated',
                e.asdict()
            )
Ejemplo n.º 6
0
def conceptscheme_schema_validator(node, cstruct):
    """
    This validator validates the incoming conceptscheme labels

    :param colander.SchemaNode node: The schema that's being used while validating.
    :param cstruct: The conceptscheme being validated.
    """
    request = node.bindings['request']
    skos_manager = request.data_managers['skos_manager']
    languages_manager = request.data_managers['languages_manager']
    errors = []
    min_labels_rule(errors, node, cstruct)
    if 'labels' in cstruct:
        labels = copy.deepcopy(cstruct['labels'])
        label_type_rule(errors, node, skos_manager, labels)
        label_lang_rule(errors, node, languages_manager, labels)
        max_preflabels_rule(errors, node, labels)
    if len(errors) > 0:
        raise ValidationError('ConceptScheme could not be validated',
                              [e.asdict() for e in errors])
Ejemplo n.º 7
0
 def test_validation_error(self):
     error = ValidationError('validation failed', {})
     self.assertIsNotNone(error)
     self.assertEqual("'validation failed'", str(error))
Ejemplo n.º 8
0
def concept_schema_validator(node, cstruct):
    """
    This validator validates an incoming concept or collection

    This validator will run a list of rules against the concept or collection
    to see that there are no validation rules being broken.

    :param colander.SchemaNode node: The schema that's being used while validating.
    :param cstruct: The concept or collection being validated.
    """
    request = node.bindings['request']
    skos_manager = request.data_managers['skos_manager']
    languages_manager = request.data_managers['languages_manager']
    conceptscheme_id = node.bindings['conceptscheme_id']
    concept_type = cstruct['type']
    id = cstruct['id']
    narrower = None
    broader = None
    related = None
    members = None
    member_of = None
    r_validated = False
    n_validated = False
    b_validated = False
    m_validated = False
    o_validated = False
    errors = []
    min_labels_rule(errors, node, cstruct)
    if 'labels' in cstruct:
        labels = copy.deepcopy(cstruct['labels'])
        label_type_rule(errors, node, skos_manager, labels)
        label_lang_rule(errors, node, languages_manager, labels)
        max_preflabels_rule(errors, node, labels)
    if 'related' in cstruct:
        related = copy.deepcopy(cstruct['related'])
        related = [m['id'] for m in related]
        r_validated = semantic_relations_rule(errors, node['related'],
                                              skos_manager, conceptscheme_id,
                                              related, id)
        concept_relations_rule(errors, node['related'], related, concept_type)
    if 'narrower' in cstruct:
        narrower = copy.deepcopy(cstruct['narrower'])
        narrower = [m['id'] for m in narrower]
        n_validated = semantic_relations_rule(errors, node['narrower'],
                                              skos_manager, conceptscheme_id,
                                              narrower, id)
        concept_relations_rule(errors, node['narrower'], narrower,
                               concept_type)
    if 'broader' in cstruct:
        broader = copy.deepcopy(cstruct['broader'])
        broader = [m['id'] for m in broader]
        b_validated = semantic_relations_rule(errors, node['broader'],
                                              skos_manager, conceptscheme_id,
                                              broader, id)
        concept_relations_rule(errors, node['broader'], broader, concept_type)
    if 'members' in cstruct:
        members = copy.deepcopy(cstruct['members'])
        members = [m['id'] for m in members]
        m_validated = semantic_relations_rule(errors, node['members'],
                                              skos_manager, conceptscheme_id,
                                              members, id)
    if 'member_of' in cstruct:
        member_of = copy.deepcopy(cstruct['member_of'])
        member_of = [m['id'] for m in member_of]
        o_validated = semantic_relations_rule(errors, node['member_of'],
                                              skos_manager, conceptscheme_id,
                                              member_of, id)
    if r_validated and n_validated and b_validated:
        concept_type_rule(errors, node['narrower'], skos_manager,
                          conceptscheme_id, narrower)
        narrower_hierarchy_rule(errors, node['narrower'], skos_manager,
                                conceptscheme_id, cstruct)
        concept_type_rule(errors, node['broader'], skos_manager,
                          conceptscheme_id, broader)
        broader_hierarchy_rule(errors, node['broader'], skos_manager,
                               conceptscheme_id, cstruct)
        concept_type_rule(errors, node['related'], skos_manager,
                          conceptscheme_id, related)

    if m_validated and o_validated:
        members_only_in_collection_rule(errors, node['members'], concept_type,
                                        members)
        collection_members_unique_rule(errors, node['members'], members)
        collection_type_rule(errors, node['member_of'], skos_manager,
                             conceptscheme_id, member_of)
        memberof_hierarchy_rule(errors, node['member_of'], skos_manager,
                                conceptscheme_id, cstruct)
        members_hierarchy_rule(errors, node['members'], skos_manager,
                               conceptscheme_id, cstruct)

    if 'matches' in cstruct:
        matches = copy.deepcopy(cstruct['matches'])
        concept_matches_rule(errors, node['matches'], matches, concept_type)
        concept_matches_unique_rule(errors, node['matches'], matches)

    if 'subordinate_arrays' in cstruct:
        subordinate_arrays = copy.deepcopy(cstruct['subordinate_arrays'])
        subordinate_arrays = [m['id'] for m in subordinate_arrays]
        subordinate_arrays_only_in_concept_rule(errors,
                                                node['subordinate_arrays'],
                                                concept_type,
                                                subordinate_arrays)
        subordinate_arrays_type_rule(errors, node['subordinate_arrays'],
                                     skos_manager, conceptscheme_id,
                                     subordinate_arrays)
        subordinate_arrays_hierarchy_rule(errors, node['subordinate_arrays'],
                                          skos_manager, conceptscheme_id,
                                          cstruct)

    if 'superordinates' in cstruct:
        superordinates = copy.deepcopy(cstruct['superordinates'])
        superordinates = [m['id'] for m in superordinates]
        superordinates_only_in_concept_rule(errors, node['superordinates'],
                                            concept_type, superordinates)
        superordinates_type_rule(errors, node['superordinates'], skos_manager,
                                 conceptscheme_id, superordinates)
        superordinates_hierarchy_rule(errors, node['superordinates'],
                                      skos_manager, conceptscheme_id, cstruct)

    if len(errors) > 0:
        raise ValidationError('Concept could not be validated',
                              [e.asdict() for e in errors])
Ejemplo n.º 9
0
def concept_schema_validator(node, cstruct):
    request = node.bindings['request']
    conceptscheme_id = node.bindings['conceptscheme_id']
    concept_type = cstruct['type']
    narrower = None
    broader = None
    related = None
    members = None
    member_of = None
    r_validated = False
    n_validated = False
    b_validated = False
    m_validated = False
    o_validated = False
    errors = []
    min_labels_rule(errors, node, cstruct)
    if 'labels' in cstruct:
        labels = copy.deepcopy(cstruct['labels'])
        label_type_rule(errors, node, request, labels)
        label_lang_rule(errors, node, request, labels)
        max_preflabels_rule(errors, node, labels)
    if 'related' in cstruct:
        related = copy.deepcopy(cstruct['related'])
        related = [m['id'] for m in related]
        r_validated = concept_exists_andnot_different_conceptscheme_rule(
            errors, node['related'], request, conceptscheme_id, related)
        concept_relations_rule(errors, node['related'], related, concept_type)
    if 'narrower' in cstruct:
        narrower = copy.deepcopy(cstruct['narrower'])
        narrower = [m['id'] for m in narrower]
        n_validated = concept_exists_andnot_different_conceptscheme_rule(
            errors, node['narrower'], request, conceptscheme_id, narrower)
        concept_relations_rule(errors, node['narrower'], narrower,
                               concept_type)
    if 'broader' in cstruct:
        broader = copy.deepcopy(cstruct['broader'])
        broader = [m['id'] for m in broader]
        b_validated = concept_exists_andnot_different_conceptscheme_rule(
            errors, node['broader'], request, conceptscheme_id, broader)
        concept_relations_rule(errors, node['broader'], broader, concept_type)
    if 'members' in cstruct:
        members = copy.deepcopy(cstruct['members'])
        members = [m['id'] for m in members]
        m_validated = concept_exists_andnot_different_conceptscheme_rule(
            errors, node['members'], request, conceptscheme_id, members)
    if 'member_of' in cstruct:
        member_of = copy.deepcopy(cstruct['member_of'])
        member_of = [m['id'] for m in member_of]
        o_validated = concept_exists_andnot_different_conceptscheme_rule(
            errors, node['member_of'], request, conceptscheme_id, member_of)
    if r_validated and n_validated and b_validated:
        concept_type_rule(errors, node['narrower'], request, conceptscheme_id,
                          narrower)
        narrower_hierarchy_rule(errors, node['narrower'], request,
                                conceptscheme_id, cstruct)
        concept_type_rule(errors, node['broader'], request, conceptscheme_id,
                          broader)
        broader_hierarchy_rule(errors, node['broader'], request,
                               conceptscheme_id, cstruct)
        concept_type_rule(errors, node['related'], request, conceptscheme_id,
                          related)

    if m_validated and o_validated:
        members_only_in_collection_rule(errors, node['members'], concept_type,
                                        members)
        collection_members_unique_rule(errors, node['members'], members)
        collection_type_rule(errors, node['member_of'], request,
                             conceptscheme_id, member_of)
        memberof_hierarchy_rule(errors, node['member_of'], request,
                                conceptscheme_id, cstruct)
        members_hierarchy_rule(errors, node['members'], request,
                               conceptscheme_id, cstruct)

    if 'matches' in cstruct:
        matches = copy.deepcopy(cstruct['matches'])
        concept_matches_rule(errors, node['matches'], matches, concept_type)
        concept_matches_unique_rule(errors, node['matches'], matches)

    if 'subordinate_arrays' in cstruct:
        subordinate_arrays = copy.deepcopy(cstruct['subordinate_arrays'])
        subordinate_arrays = [m['id'] for m in subordinate_arrays]
        subordinate_arrays_only_in_concept_rule(errors,
                                                node['subordinate_arrays'],
                                                concept_type,
                                                subordinate_arrays)
        subordinate_arrays_type_rule(errors, node['subordinate_arrays'],
                                     request, conceptscheme_id,
                                     subordinate_arrays)
        subordinate_arrays_hierarchy_rule(errors, node['subordinate_arrays'],
                                          request, conceptscheme_id, cstruct)

    if 'superordinates' in cstruct:
        superordinates = copy.deepcopy(cstruct['superordinates'])
        superordinates = [m['id'] for m in superordinates]
        superordinates_only_in_concept_rule(errors, node['superordinates'],
                                            concept_type, superordinates)
        superordinates_type_rule(errors, node['superordinates'], request,
                                 conceptscheme_id, superordinates)
        superordinates_hierarchy_rule(errors, node['superordinates'], request,
                                      conceptscheme_id, cstruct)

    if len(errors) > 0:
        raise ValidationError('Concept could not be validated',
                              [e.asdict() for e in errors])