From d18d59d3805c02d8b9ff019a82b87f04b60365c1 Mon Sep 17 00:00:00 2001 From: jameswilddev Date: Tue, 28 May 2019 01:12:21 +0100 Subject: [PATCH] Added entity type sets (#6). --- src/entity-type-set.tests.ts | 7 ++++++ src/entity-type-set.ts | 21 ++++++++++++++++ src/index.tests.ts | 4 +++ src/index.ts | 5 ++++ src/shared.tests.ts | 48 ++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 src/entity-type-set.tests.ts create mode 100644 src/entity-type-set.ts diff --git a/src/entity-type-set.tests.ts b/src/entity-type-set.tests.ts new file mode 100644 index 0000000..84d8d55 --- /dev/null +++ b/src/entity-type-set.tests.ts @@ -0,0 +1,7 @@ +import "jasmine" +import * as entityTypeSet from "./entity-type-set" +import * as shared from "./shared.tests" + +describe(`entity type set`, () => { + shared.testEntityTypeSet(entityTypeSet.schema, value => value, `instance`) +}) diff --git a/src/entity-type-set.ts b/src/entity-type-set.ts new file mode 100644 index 0000000..c1caf6b --- /dev/null +++ b/src/entity-type-set.ts @@ -0,0 +1,21 @@ +import * as jsonschema from "jsonschema" +import * as entityType from "./entity-type" + +export const schema: jsonschema.Schema = { + description: `Maps entity type identifiers to schemas.`, + type: `object`, + additionalProperties: false, + patternProperties: { + "^[_a-z0-9]{6}$": entityType.schema + } +} + +/** + * Maps entity type identifiers to schemas. + */ +export type Type = { + /** + * A mapping of an entity type identifier to its corresponding schema. + */ + readonly [column: string]: entityType.Type +} diff --git a/src/index.tests.ts b/src/index.tests.ts index 988897f..a818e15 100644 --- a/src/index.tests.ts +++ b/src/index.tests.ts @@ -12,6 +12,7 @@ import * as columnSet from "./column-set" import * as labelPart from "./label-part" import * as label from "./label" import * as entityType from "./entity-type" +import * as entityTypeSet from "./entity-type-set" import * as schema from "./schema" import * as index from "./index" @@ -56,6 +57,9 @@ describe(`index`, () => { describe(`entity type`, () => { it(`schema`, () => expect(index.entityType).toBe(entityType.schema)) }) + describe(`entity type set`, () => { + it(`schema`, () => expect(index.entityTypeSet).toBe(entityTypeSet.schema)) + }) describe(`schema`, () => { it(`schema`, () => expect(index.schema).toBe(schema.schema)) }) diff --git a/src/index.ts b/src/index.ts index 8a32583..2dc5681 100644 --- a/src/index.ts +++ b/src/index.ts @@ -63,6 +63,11 @@ export { Type as EntityType } from "./entity-type" +export { + schema as entityTypeSet, + Type as EntityTypeSet +} from "./entity-type-set" + export { schema as schema, Type as Schema diff --git a/src/shared.tests.ts b/src/shared.tests.ts index 6ca5be1..6611c91 100644 --- a/src/shared.tests.ts +++ b/src/shared.tests.ts @@ -892,3 +892,51 @@ export function testEntityType( }), `${property}.columns`) }) } + +export function testEntityTypeSet( + schema: jsonschema.Schema, + instanceFactory: InstanceFactory, + property: string +): void { + run(nonObjects, value => rejects(schema, instanceFactory(value), property, `is not of a type(s) object`)) + run(emptyObjects, value => accepts(schema, instanceFactory(value))) + run(identifierStrings, value => accepts(schema, instanceFactory(keyValue(value, { + singular: {}, + plural: {}, + label: [], + columns: {} + })))) + run(nonIdentifierStrings, value => rejects(schema, instanceFactory(keyValue(value, { + singular: {}, + plural: {}, + label: [], + columns: {} + })), property, `additionalProperty ${JSON.stringify(value)} exists in instance when not allowed`)) + testEntityType(schema, value => instanceFactory(keyValue(`for_eg`, value)), `${property}.for_eg`) + describe(`multiple columns`, () => accepts(schema, instanceFactory({ + for_eg: { + singular: {}, + plural: {}, + label: [], + columns: {} + }, + oth_id: { + singular: {}, + plural: {}, + label: [], + columns: {} + }, + anther: { + singular: {}, + plural: {}, + label: [], + columns: {} + }, + lastid: { + singular: {}, + plural: {}, + label: [], + columns: {} + } + }))) +}