Skip to content

Commit

Permalink
improve datatype error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomociti committed May 16, 2024
1 parent 4a9a139 commit 5d90026
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cli/lib/validation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { NamedNode, Quad } from '@rdfjs/types'
import type { Literal, NamedNode, Quad } from '@rdfjs/types'
import { turtle } from '@tpluscode/rdf-string'
import type { Context } from 'barnard59-core/lib/Pipeline'
import { validateQuad } from 'rdf-validate-datatype'

export function validate(this: Context, quad: Quad): Quad {
if (!validateQuad(quad)) {
throw new Error(`Quad object has invalid datatype:\n\t${quadToString(quad)}`)
throw new Error(`${errorMessage(quad)}\n\n${quadToString(quad)}`)
}

return quad
Expand All @@ -17,3 +17,12 @@ function quadToString(quad: Quad): string {
graph: quad.graph as NamedNode,
})
}

function errorMessage(quad: Quad): string {
const literal = quad.object as Literal
if (!literal.datatype) {
return `Invalid datatype for non-literal ${literal.value}`
}

return `literal "${literal.value}" is not a valid value for datatype <${literal.datatype.value}>.`
}

0 comments on commit 5d90026

Please sign in to comment.