Skip to content

Instantly share code, notes, and snippets.

@joeltg
Last active October 16, 2020 20:46
Show Gist options
  • Save joeltg/423141cd96c858931f618fbf97fdc1b9 to your computer and use it in GitHub Desktop.
Save joeltg/423141cd96c858931f618fbf97fdc1b9 to your computer and use it in GitHub Desktop.
TypeScript module declarations for shex.js
declare module "@shexjs/parser" {
function construct(): Parser
interface Parser {
parse(shex: string): Schema
}
type Schema = {
"@context"?: "http://www.w3.org/ns/shex.jsonld"
type: "Schema"
startActs?: SemAct[]
start?: shapeExpr
shapes?: ({ id: string } & shapeExprObject)[]
imports?: string[]
}
type shapeExpr = shapeExprObject | string
type shapeExprObject =
| ShapeOr
| ShapeAnd
| ShapeNot
| NodeConstraint
| Shape
| ShapeExternal
type ShapeOr = {
id?: string
type: "ShapeOr"
shapeExprs: shapeExpr[]
}
type ShapeAnd = {
id?: string
type: "ShapeAnd"
shapeExprs: shapeExpr[]
}
type ShapeNot = {id?: string; type: "ShapeNot"; shapeExpr: shapeExpr }
type ShapeExternal = { id?: string; type: "ShapeExternal" }
type NodeConstraint = {id?: string; type: "NodeConstraint" } & (
| nonLiteralNodeConstraint
| literalNodeConstraint
)
type nonLiteralNodeConstraint =
| ({ nodeKind?: "iri" } & stringFacets)
| { nodeKind: "bnode" | "nonliteral" }
type literalNodeConstraint =
| ({ nodeKind: "literal" } & xsFacets)
| datatypeConstraint
| valueSetConstraint
| numericFacets
type datatypeConstraint = { datatype: string } & xsFacets
type valueSetConstraint = { values: valueSetValue[] } & xsFacets
type stringLength =
| { length: number }
| { minlength?: number; maxlength?: number } // This doubles as an empty string facet
type stringFacets = stringLength & { pattern?: string; flags?: string }
type numericFacets = {
mininclusive?: number
minexclusive?: number
maxinclusive?: number
maxexclusive?: number
totaldigits?: number
fractiondigits?: number
}
type xsFacets = stringFacets & numericFacets
type valueSetValue =
| objectValue
| IriStem
| IriStemRange
| LiteralStem
| LiteralStemRange
| Language
| LanguageStem
| LanguageStemRange
type objectValue = string | ObjectLiteral
type ObjectLiteral = { value: string; language?: string; type?: string }
type IriStem = { type: "IriStem"; stem: string }
type IriStemRange = {
type: "IriStemRange"
stem: string | Wildcard
exclusions: (string | IriStem)[]
}
type LiteralStem = { type: "LiteralStem"; stem: string }
type LiteralStemRange = {
type: "LiteralStemRange"
stem: string | Wildcard
exclusions: (string | LiteralStem)[]
}
type Language = { type: "Language"; languageTag: string }
type LanguageStem = { type: "LanguageStem"; stem: string }
type LanguageStemRange = {
type: "LanguageStemRange"
stem: string | Wildcard
exclusions: (string | LanguageStem)[]
}
type Wildcard = { type: "Wildcard" }
type Shape = {
id?: string
type: "Shape"
closed?: boolean
extra?: string[]
expression?: tripleExpr
semActs?: SemAct[]
annotations?: Annotation[]
}
type tripleExpr = tripleExprObject | string
type tripleExprObject = EachOf | OneOf | TripleConstraint
type EachOf = {
id?: string
type: "EachOf"
expressions: tripleExpr[]
min?: number
max?: number
semActs?: SemAct[]
annotations?: Annotation[]
}
type OneOf = {
id?: string
type: "OneOf"
expressions: tripleExpr[]
min?: number
max?: number
semActs?: SemAct[]
annotations?: Annotation[]
}
type TripleConstraint<
P extends string = string,
V extends shapeExpr | undefined = shapeExpr | undefined
> = {
id?: string
type: "TripleConstraint"
inverse?: boolean
predicate: string
valueExpr?: shapeExpr
min?: number
max?: number
semActs?: SemAct[]
annotations?: Annotation[]
}
type SemAct = { type: "SemAct"; name: string; code?: string }
type Annotation<P = string, O = objectValue> = {
type: "Annotation"
predicate: P
object: O
}
}
declare module "@shexjs/util" {
import RDF from "rdf-js"
import { Schema } from "@shexjs/parser"
interface DB {
getQuads(
subject: RDF.Term | string | null,
predicate: RDF.Term | string | null,
object: RDF.Term | string | null,
graph: RDF.Term | string | null
): RDF.Quad[]
getSubjects(
predicate: RDF.Term | string | null,
object: RDF.Term | string | null,
graph: RDF.Term | string | null
): RDF.Quad_Subject[]
getPredicates(
subject: RDF.Term | string | null,
object: RDF.Term | string | null,
graph: RDF.Term | string | null
): RDF.Quad_Predicate[]
getObjects(
subject: RDF.Term | string | null,
predicate: RDF.Term | string | null,
graph: RDF.Term | string | null
): RDF.Quad_Object[]
getGraphs(
subject: RDF.Term | string | null,
predicate: RDF.Term | string | null,
object: RDF.Term | string | null
): RDF.Quad_Graph[]
size: number
}
export interface RdfJsDB extends DB {}
export function rdfjsDB(store: DB): RdfJsDB
export function emptySchema(): { type: "Schema" }
export function merge(
left: Schema,
right: Schema,
overwrite: boolean,
inPlace: boolean
): void
}
declare module "@shexjs/validator" {
import { Schema, shapeExpr, objectValue, Annotation } from "@shexjs/parser"
import * as ShExUtil from "@shexjs/util"
export type Start = { term: "START" }
export default class Validator {
static start: Start
static construct(
schema: Schema,
db: ShExUtil.RdfJsDB,
options?: { or?: string; partition?: string; results?: string }
): Validator
validate(
id: string,
start: shapeExpr
): SuccessResult | FailureResult
}
export type SuccessResult =
| ShapeAndResults
| ShapeOrResults
| ShapeNotResults
| ShapeTest
| NodeConstraintTest
| Recursion
export type FailureResult =
| ShapeAndFailure
| ShapeOrFailure
| ShapeNotFailure
| Failure
export type ShapeAndFailure = {
type: "ShapeAndFailure"
errors: {}[]
}
export type ShapeOrFailure = {
type: "ShapeOrFailure"
errors: {}[]
}
export type ShapeNotFailure = {
type: "ShapeNotFailure"
errors: {}[]
}
export type Failure = {
type: "Failure"
shape: string
node: string
errors: {}[]
}
export type Recursion = {
type: "Recursion"
node: string
shape: string
}
export type ShapeAndResults = {
type: "ShapeAndResults"
solutions: SuccessResult[]
}
export type ShapeOrResults = {
type: "ShapeOrResults"
solution: SuccessResult
}
export type ShapeNotResults = {
type: "ShapeNotResult"
solution: SuccessResult
}
export type NodeConstraintTest = {
type: "NodeConstraintTest"
node: string
shape: string
shapeExpr: shapeExpr
}
export type ShapeTest = {
type: "ShapeTest"
node: string
shape: string
solution: solutions
annotations?: Annotation[]
}
type solutions = EachOfSolutions | OneOfSolutions | TripleConstraintSolutions
export type EachOfSolutions = {
type: "EachOfSolutions"
solutions: EachOfSolution[]
min?: number
max?: number
annotations?: Annotation[]
}
export type EachOfSolution = {
type: "EachOfSolution"
expressions: solutions[]
}
export type OneOfSolutions = {
type: "OneOfSolutions"
solutions: OneOfSolution[]
min?: number
max?: number
annotations?: Annotation[]
}
export type OneOfSolution = {
type: "OneOfSolution"
expressions: solutions[]
}
export type TripleConstraintSolutions<O = objectValue> = {
type: "TripleConstraintSolutions"
predicate: string
solutions: TestedTriple<O>[]
valueExpr?: shapeExpr
productionLabel?: string
min?: number
max?: number
annotations?: Annotation[]
}
export type TestedTriple<O = objectValue> = {
type: "TestedTriple"
subject: string
predicate: string
object: O
referenced?: SuccessResult
}
}
declare module "@shexjs/visitor" {
import { Schema, tripleExprObject, shapeExprObject } from "@shexjs/parser"
export type Index = {
tripleExprs: { [id: string]: tripleExprObject }
shapeExprs: { [id: string]: shapeExprObject }
}
export default class Visitor {
static index(schema: Schema): Index
}
}
{
"version": "0.2.0",
"name": "@types/shexjs",
"main": "",
"types": "index.d.ts",
"dependencies": {
"@types/rdf-js": "^4.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment