Validating .ttl elements with rdflib

631 views Asked by At

Is there a way using rdflib or a similar package to validate a set of elements?

e.g.

from rdflib import Graph, Namespace, Literal
from rdflib.namespace import DCTERMS

n = Namespace("http://example.org/books/")
n.book

g = Graph()
g.bind("dc", DCTERMS)
g.bind("ex", n)
g.add((n["book"], DCTERMS["title"], Literal("Example Title"))) # Valid
g.add((n["book"], DCTERMS["tite"], Literal("Example Title"))) # Invalid

Or how it would look as a .ttl file:

@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ex: <http://example.org/books/>

ex:book dc:title "Example Title" . # Valid
ex:book dc:tite "Example Title" . # Invalid

There's a good chance I'm approaching this from the wrong angle entirely so any help is appreciated.

1

There are 1 answers

1
Nicholas Car On BEST ANSWER

@UninformedUser commented:

check if the property is part of the DCTerms vocabulary - which is trivial to check

  1. download the vocabulary - either in code or separately
  2. load the vocabulary into another RDFlib Graph
  3. check to see if any property you're using is an rdf:Property in the other graph, as per:
if not (the_property_being_tested, RDF.type, RDF.Property) in graph:

Or, just see if it's a subject in the graph (looser test):

in_graph = False
for s, p, o in g.triples((the_property_being_tested, None, None)):
    in_graph = True