rdf-validate-datatype
Validate literal value of an RDF term based on its declared datatype.
Install
$ npm install rdf-validate-datatype
Usage
validateTerm
import { validateTerm } from 'rdf-validate-datatype'
import rdf from '@rdfjs/data-model'
import { xsd } from '@tpluscode/rdf-ns-builders'
const term = rdf.literal('test')
const isValid = validateTerm(term)
const term = rdf.literal('2019-01-01', xsd.date)
const isValid = validateTerm(term)
const term = rdf.literal('invalid date', xsd.date)
const isValid = validateTerm(term)
validateQuad
validateQuad
validates that the .object
of a given quad is valid in regards
to its declared datatype.
import { validateQuad } from 'rdf-validate-datatype'
import rdf from '@rdfjs/data-model'
import { schema, xsd } from '@tpluscode/rdf-ns-builders'
const quad = rdf.quad(
rdf.namedNode('bob'),
schema.birthDate,
rdf.literal('2019-01-01', xsd.date)
)
const isValid = validateQuad(term)
const quad = rdf.quad(
rdf.namedNode('bob'),
schema.birthDate,
rdf.literal('invalid date', xsd.date)
)
const isValid = validateQuad(term)
Configuring validators
Datatype validators are stored in a registry. They can be changed at runtime.
import { validators, validateTerm } from 'rdf-validate-datatype'
import rdf from '@rdfjs/data-model'
import { xsd } from '@tpluscode/rdf-ns-builders'
const myDatatype = rdf.namedNode('my-datatype')
validators.register(myDatatype, value => value.startsWith('X-'))
validateTerm(rdf.literal('X-test', myDatatype))
validateTerm(rdf.literal('test', myDatatype))
validators.register(xsd.date, value => true)
validateTerm(rdf.literal('banana', xsd.date))