Skip to content

Commit

Permalink
Merge pull request #1519 from zazuko/error-messages
Browse files Browse the repository at this point in the history
Error messages
  • Loading branch information
giacomociti authored Jun 3, 2024
2 parents 6ea3bb1 + aba5441 commit 28eeb01
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/unlucky-lobsters-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@cube-creator/cli": patch
"@cube-creator/ui": patch
---

Improve error messages and prevent mixing language and datatype
10 changes: 10 additions & 0 deletions apis/core/bootstrap/shapes/column-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ ${literalShapeId} {
${sh.maxCount} 1 ;
${sh.order} 60 ;
] ;
${sh.message} "Errors" ;
${sh.node} [
${sh.message} "Either set Language or Data type, not both" ;
${dash.hidden} true ;
${sh.or} (
[ ${sh.path} ${cc.datatype} ; ${sh.hasValue} ${xsd.string} ]
[ ${sh.path} [ ${sh.alternativePath} (${cc.language} ${cc.datatype})] ; ${sh.maxCount} 1 ]
)
]
.
${datatypes.map(([id, labels]) => labels.map(label => turtle`${id} ${rdfs.label} "${label}" .`))}
Expand Down
7 changes: 5 additions & 2 deletions apis/core/lib/domain/column-mapping/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ async function createLiteralColumnMapping({ targetProperty, table, source, resou
throw new NotFoundError(columnId)
}

const language = resource.out(cc.language).value
const datatype = language ? rdf.langString : resource.out(cc.datatype).term as NamedNode

return table.addLiteralColumnMapping({
store,
sourceColumn: column,
targetProperty,
datatype: resource.out(cc.datatype).term as NamedNode,
language: resource.out(cc.language).value,
datatype,
language,
defaultValue: resource.out(cc.defaultValue).term,
dimensionType: resource.out(cc.dimensionType).term,
})
Expand Down
4 changes: 2 additions & 2 deletions apis/core/test/domain/column-mapping/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('domain/column-mapping/create', () => {
.node($rdf.namedNode(''))
.addOut(cc.sourceColumn, $rdf.namedNode('my-column'))
.addOut(cc.targetProperty, $rdf.namedNode('test'))
.addOut(cc.datatype, xsd.integer)
.addOut(cc.datatype, xsd.string) // becomes rdf.langString because of language
.addOut(cc.language, $rdf.literal('fr'))
.addOut(cc.defaultValue, $rdf.literal('test'))

Expand All @@ -121,7 +121,7 @@ describe('domain/column-mapping/create', () => {
minCount: 1,
}, {
path: cc.datatype,
hasValue: xsd.integer,
hasValue: rdf.langString,
minCount: 1,
}, {
path: cc.language,
Expand Down
47 changes: 45 additions & 2 deletions cli/lib/validation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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'
import { validateQuad } from 'rdf-validate-datatype'
import TermMap from '@rdfjs/term-map'
import { xsd } from '@tpluscode/rdf-ns-builders'

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 +19,44 @@ function quadToString(quad: Quad): string {
graph: quad.graph as NamedNode,
})
}

const messages = new TermMap<NamedNode, string>([
[xsd.boolean, 'Boolean values should be formatted as "true" and "false" (or "0" and "1"). See also https://www.w3.org/TR/xmlschema-2/#boolean.'],
[xsd.date, 'Dates should be formatted as YYYY-MM-DD as specified by ISO 8601. See also https://www.w3.org/TR/xmlschema-2/#date.'],
[xsd.dateTime, 'Values should be formatted as YYYY-MM-DDThh:mm:ss. See also https://www.w3.org/TR/xmlschema-2/#dateTime.'],
[xsd.gDay, 'Days should be formatted as DD. See also https://www.w3.org/TR/xmlschema-2/#gDay.'],
[xsd.decimal, 'Decimals should be formatted as digits, possibly with a period to separate the fractional part. An optional leading sign is allowed. See also https://www.w3.org/TR/xmlschema-2/#decimal.'],
[xsd.integer, 'Integers should be formatted as digits, with an optional leading sign. See also https://www.w3.org/TR/xmlschema-2/#integer.'],
[xsd.gMonth, 'Months should be formatted as MM. See also https://www.w3.org/TR/xmlschema-2/#gMonth.'],
[xsd.string, 'See also https://www.w3.org/TR/xmlschema-2/#string.'],
[xsd.time, 'Values should be formatted as hh:mm:ss. See also https://www.w3.org/TR/xmlschema-2/#time.'],
[xsd.gYear, 'Years should be formatted as YYYY. See also https://www.w3.org/TR/xmlschema-2/#gYear.'],
[xsd.gYearMonth, 'Values should be formatted as YYYY-MM. See also https://www.w3.org/TR/xmlschema-2/#gYearMonth.'],
])

const typeName = new TermMap<NamedNode, string>([
[xsd.boolean, 'boolean'],
[xsd.date, 'date'],
[xsd.dateTime, 'dateTime'],
[xsd.gDay, 'day'],
[xsd.decimal, 'decimal'],
[xsd.integer, 'integer'],
[xsd.gMonth, 'month'],
[xsd.string, 'string'],
[xsd.time, 'time'],
[xsd.gYear, 'year'],
[xsd.gYearMonth, 'year+month'],
])

function errorMessage(quad: Quad): string {
const literal = quad.object as Literal
if (!literal.datatype) {
return `Invalid datatype for non-literal ${literal.value}`
}
const message = messages.get(literal.datatype)
if (message) {
return `the value "${literal.value}" in your CSV file does not conform to the selected datatype "${typeName.get(literal.datatype)}".\n${message}`
}

return `literal "${literal.value}" is not a valid value for datatype <${literal.datatype.value}>.`
}
3 changes: 0 additions & 3 deletions e2e-tests/column-mapping/create.hydra
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ With Class api:Table {
cc:sourceColumn <https://cube-creator.lndo.site/cube-project/ubd/csv-source/ubd/column/year> ;
cc:targetProperty "dimension/year-built" ;
cc:datatype xsd:date ;
cc:language "en" ;
cc:defaultValue "2000-01-01" .
```
} => {
Expand All @@ -42,7 +41,6 @@ With Class api:Table {
Expect Property cc:datatype {
Expect Id xsd:date
}
Expect Property cc:language "en"
Expect Property cc:defaultValue "2000-01-01"
}

Expand All @@ -58,7 +56,6 @@ With Class api:Table {
cc:sourceColumn <https://cube-creator.lndo.site/cube-project/ubd/csv-source/ubd/column/year> ;
cc:targetProperty "dimension/year-built" ;
cc:datatype xsd:date ;
cc:language "en" ;
cc:defaultValue "2000-01-01" .
```
} => {
Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/column-mapping/update-literal.hydra
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ With Class cc:LiteralColumnMapping {
a cc:ColumnMapping, cc:LiteralColumnMapping ;
cc:sourceColumn <cube-project/ubd/csv-source/ubd/column/station> ;
cc:targetProperty schema:identifier ;
cc:datatype xsd:integer ;
cc:datatype xsd:string ;
cc:language "en" ;
.
```
Expand All @@ -38,7 +38,7 @@ With Class cc:LiteralColumnMapping {
Expect Id <https://cube-creator.lndo.site/cube-project/ubd/csv-source/ubd/column/station>
}
Expect Property cc:datatype {
Expect Id xsd:integer
Expect Id xsd:string
}
Expect Property cc:language "en"
}
Expand Down

0 comments on commit 28eeb01

Please sign in to comment.