Skip to content

Commit

Permalink
Added support for type shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
karelklima committed Nov 18, 2023
1 parent c71290b commit a3960e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion library/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type SchemaPrototypeProperties = {
};

export type SchemaPrototypeType = {
"@type": string | readonly string[];
"@type"?: string | readonly string[];
};

export type SchemaPrototype = SchemaPrototypeProperties & SchemaPrototypeType;
Expand Down
15 changes: 12 additions & 3 deletions library/schema/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import rdf from "../namespaces/rdf.ts";
import xsd from "../namespaces/xsd.ts";

import type {
Expand All @@ -13,9 +14,15 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {
}

if (Object.keys(schemaPrototype).length === 0) {
throw new Error(`Invalid schema, empty object, expected "@type" key or property definition`);
throw new Error(
`Invalid schema, empty object, expected "@type" key or property definition`,
);
}

const expandShortcut = (value: string) => {
return value === "@type" ? rdf.type : value;
};

const expandArray = <T extends string>(stringOrStrings: T | readonly T[]) => {
return Array.isArray(stringOrStrings) ? stringOrStrings : [stringOrStrings];
};
Expand All @@ -25,7 +32,7 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {
) => {
if (typeof stringOrProperty === "string") {
return {
"@id": stringOrProperty,
"@id": expandShortcut(stringOrProperty),
"@type": xsd.string,
};
}
Expand All @@ -52,6 +59,8 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {
const expandedProperty = Object.keys(property).reduce((acc, key) => {
if (key === "@context") {
acc[key] = expandSchema(property[key]!);
} else if (key === "@id") {
acc[key] = expandShortcut(property[key]);
} else if (validKeys.includes(key as keyof PropertyPrototype)) {
acc[key] = property[key as keyof PropertyPrototype] as unknown;
}
Expand All @@ -71,7 +80,7 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {

return Object.keys(schemaPrototype).reduce((acc, key) => {
if (key === "@type") {
acc[key] = expandArray(schemaPrototype[key]);
acc[key] = expandArray(schemaPrototype[key]!);
} else {
acc[key] = expandSchemaProperty(
schemaPrototype[key] as string | PropertyPrototype,
Expand Down
20 changes: 18 additions & 2 deletions tests/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { Equals, x } from "./test_utils.ts";

import {
expandSchema,
Property,
type Schema,
type SchemaInterface,
type SchemaPrototype,
} from "../library/schema/mod.ts";
import { xsd } from "../library/namespaces/mod.ts";
import rdf from "../library/namespaces/rdf.ts";

type ThingType = {
$id: string;
$type: string[];
required: string;
optional: string | undefined;
array: string[];
Expand All @@ -22,7 +23,6 @@ type ThingType = {
date: Date;
nested: {
$id: string;
$type: string[];
nestedValue: string;
};
};
Expand Down Expand Up @@ -137,3 +137,19 @@ Deno.test("Schema / should have at least one property or @type restriction", ()
expandSchema({} as unknown as SchemaPrototype);
});
});

Deno.test("Schema / should expand @type shortcut definition", () => {
const schema = {
"type": "@type",
};
const expandedSchema = expandSchema(schema);
assertEquals((expandedSchema["type"] as Property)["@id"], rdf.type);
});

Deno.test("Schema / should expand @type property definition", () => {
const schema = {
"type": { "@id": "@type" },
};
const expandedSchema = expandSchema(schema);
assertEquals((expandedSchema["type"] as Property)["@id"], rdf.type);
});

0 comments on commit a3960e8

Please sign in to comment.