@platecms/delta-client 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +309 -96
- package/package.json +4 -6
- package/src/__generated__/fragment-masking.ts +87 -0
- package/src/__generated__/gql.ts +52 -0
- package/src/__generated__/graphql.ts +3141 -0
- package/src/__generated__/{index.d.ts → index.ts} +1 -1
- package/src/api/index.ts +1 -0
- package/src/apollo/index.ts +70 -0
- package/src/graphql/tags/tag.query.gql +5 -0
- package/src/graphql/tags/tags.fragments.gql +17 -0
- package/src/index.ts +1 -0
- package/src/schema/index.spec.ts +211 -0
- package/src/schema/index.ts +18 -0
- package/src/schema/lib/schemas/array.spec.ts +111 -0
- package/src/schema/lib/schemas/array.ts +38 -0
- package/src/schema/lib/schemas/asset.spec.ts +101 -0
- package/src/schema/lib/schemas/asset.ts +12 -0
- package/src/schema/lib/schemas/baseSchema.ts +42 -0
- package/src/schema/lib/schemas/boolean.spec.ts +65 -0
- package/src/schema/lib/schemas/boolean.ts +12 -0
- package/src/schema/lib/schemas/buildingBlock.spec.ts +56 -0
- package/src/schema/lib/schemas/buildingBlock.ts +33 -0
- package/src/schema/lib/schemas/contentItem.spec.ts +61 -0
- package/src/schema/lib/schemas/contentItem.ts +31 -0
- package/src/schema/lib/schemas/contentType.spec.ts +113 -0
- package/src/schema/lib/schemas/contentType.ts +12 -0
- package/src/schema/lib/schemas/date.spec.ts +82 -0
- package/src/schema/lib/schemas/date.ts +17 -0
- package/src/schema/lib/schemas/gridPlacement.spec.ts +77 -0
- package/src/schema/lib/schemas/gridPlacement.ts +12 -0
- package/src/schema/lib/schemas/index.ts +66 -0
- package/src/schema/lib/schemas/number.spec.ts +65 -0
- package/src/schema/lib/schemas/number.ts +12 -0
- package/src/schema/lib/schemas/pathPart.spec.ts +120 -0
- package/src/schema/lib/schemas/pathPart.ts +12 -0
- package/src/schema/lib/schemas/smartText.spec.ts +105 -0
- package/src/schema/lib/schemas/smartText.ts +12 -0
- package/src/schema/lib/schemas/string.spec.ts +65 -0
- package/src/schema/lib/schemas/string.ts +12 -0
- package/src/schema/lib/schemas/tag.spec.ts +89 -0
- package/src/schema/lib/schemas/tag.ts +12 -0
- package/src/schema/lib/utils/isContentValue.spec.ts +111 -0
- package/src/schema/lib/utils/isContentValue.ts +17 -0
- package/src/schema/lib/utils/isPrimitiveValue.spec.ts +38 -0
- package/src/schema/lib/utils/isPrimitiveValue.ts +7 -0
- package/src/slate/index.ts +84 -0
- package/src/utils/{index.d.ts → index.ts} +3 -2
- package/src/utils/lib/connectors/BaseConnector.ts +26 -0
- package/src/utils/lib/connectors/WindowConnector.ts +70 -0
- package/src/utils/lib/events/ConnectorEvents.ts +89 -0
- package/src/utils/lib/events/EventEmitter.ts +19 -0
- package/src/__generated__/fragment-masking.d.ts +0 -19
- package/src/__generated__/fragment-masking.js +0 -22
- package/src/__generated__/fragment-masking.js.map +0 -1
- package/src/__generated__/gql.d.ts +0 -12
- package/src/__generated__/gql.js +0 -13
- package/src/__generated__/gql.js.map +0 -1
- package/src/__generated__/graphql.d.ts +0 -2157
- package/src/__generated__/graphql.js +0 -70
- package/src/__generated__/graphql.js.map +0 -1
- package/src/__generated__/index.js +0 -6
- package/src/__generated__/index.js.map +0 -1
- package/src/api/index.d.ts +0 -2
- package/src/api/index.js +0 -4
- package/src/api/index.js.map +0 -1
- package/src/apollo/index.d.ts +0 -7
- package/src/apollo/index.js +0 -40
- package/src/apollo/index.js.map +0 -1
- package/src/index.d.ts +0 -1
- package/src/index.js +0 -3
- package/src/index.js.map +0 -1
- package/src/schema/index.d.ts +0 -2
- package/src/schema/index.js +0 -8
- package/src/schema/index.js.map +0 -1
- package/src/schema/lib/nodes.d.ts +0 -46
- package/src/schema/lib/nodes.js +0 -14
- package/src/schema/lib/nodes.js.map +0 -1
- package/src/schema/lib/parser.d.ts +0 -12
- package/src/schema/lib/parser.js +0 -49
- package/src/schema/lib/parser.js.map +0 -1
- package/src/schema/lib/schema.d.ts +0 -17
- package/src/schema/lib/schema.js +0 -65
- package/src/schema/lib/schema.js.map +0 -1
- package/src/schema/lib/utils.d.ts +0 -12
- package/src/schema/lib/utils.js +0 -66
- package/src/schema/lib/utils.js.map +0 -1
- package/src/slate/index.d.ts +0 -65
- package/src/slate/index.js +0 -3
- package/src/slate/index.js.map +0 -1
- package/src/utils/index.js +0 -6
- package/src/utils/index.js.map +0 -1
- package/src/utils/lib/connectors/BaseConnector.d.ts +0 -16
- package/src/utils/lib/connectors/BaseConnector.js +0 -17
- package/src/utils/lib/connectors/BaseConnector.js.map +0 -1
- package/src/utils/lib/connectors/WindowConnector.d.ts +0 -10
- package/src/utils/lib/connectors/WindowConnector.js +0 -53
- package/src/utils/lib/connectors/WindowConnector.js.map +0 -1
- package/src/utils/lib/events/ConnectorEvents.d.ts +0 -63
- package/src/utils/lib/events/ConnectorEvents.js +0 -24
- package/src/utils/lib/events/ConnectorEvents.js.map +0 -1
- package/src/utils/lib/events/EventEmitter.d.ts +0 -7
- package/src/utils/lib/events/EventEmitter.js +0 -21
- package/src/utils/lib/events/EventEmitter.js.map +0 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { BooleanSchema } from "./boolean";
|
|
3
|
+
|
|
4
|
+
describe("Boolean schema", () => {
|
|
5
|
+
it("should parse to a boolean when parsing a boolean content value", () => {
|
|
6
|
+
const schema = new BooleanSchema();
|
|
7
|
+
|
|
8
|
+
const result = schema.parse([
|
|
9
|
+
{
|
|
10
|
+
primitiveValue: true,
|
|
11
|
+
},
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
expect(result).toEqual(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return null when parsing an a non boolean content value", () => {
|
|
18
|
+
const schema = new BooleanSchema();
|
|
19
|
+
|
|
20
|
+
const result = schema.parse([
|
|
21
|
+
{
|
|
22
|
+
primitiveValue: "hello",
|
|
23
|
+
},
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
expect(result).toEqual(null);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("when placeholders are enabled", () => {
|
|
30
|
+
it("should return the placeholder when parsing an empty array", () => {
|
|
31
|
+
const schema = new BooleanSchema().placeholder(true);
|
|
32
|
+
|
|
33
|
+
const result = schema.parse([], {
|
|
34
|
+
placeholders: true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(result).toEqual(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should return the placeholder when parsing an non boolean content value", () => {
|
|
41
|
+
const schema = new BooleanSchema().placeholder(true);
|
|
42
|
+
|
|
43
|
+
const result = schema.parse(
|
|
44
|
+
[
|
|
45
|
+
{
|
|
46
|
+
primitiveValue: "hello",
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
{
|
|
50
|
+
placeholders: true,
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(result).toEqual(true);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("when nullable is disabled", () => {
|
|
59
|
+
it("should throw an error when parsing an non boolean content value", () => {
|
|
60
|
+
const schema = new BooleanSchema().nullable(false);
|
|
61
|
+
|
|
62
|
+
expect(() => schema.parse([{ primitiveValue: "hello" }])).toThrow("Data could not be found and is not nullable");
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BaseSchema } from "./baseSchema";
|
|
2
|
+
import { isPrimitiveValue } from "../utils/isPrimitiveValue";
|
|
3
|
+
|
|
4
|
+
export class BooleanSchema extends BaseSchema<boolean, unknown> {
|
|
5
|
+
protected override findValue(data: unknown): boolean | null {
|
|
6
|
+
if (isPrimitiveValue(data) && typeof data.primitiveValue === "boolean") {
|
|
7
|
+
return data.primitiveValue;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { BuildingBlockSchema } from "./buildingBlock";
|
|
3
|
+
import { schema } from ".";
|
|
4
|
+
import { BuildingBlockFieldFulfillment } from "../../../__generated__/graphql";
|
|
5
|
+
|
|
6
|
+
describe("BuildingBlock schema", () => {
|
|
7
|
+
it("should parse an empty building block", () => {
|
|
8
|
+
const buildingBlockSchema = new BuildingBlockSchema({});
|
|
9
|
+
|
|
10
|
+
const result = buildingBlockSchema.parse([]);
|
|
11
|
+
|
|
12
|
+
expect(result).toEqual({});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should parse a building block with its values", () => {
|
|
16
|
+
const buildingBlockSchema = new BuildingBlockSchema({
|
|
17
|
+
title: schema.string(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const result = buildingBlockSchema.parse([
|
|
21
|
+
{
|
|
22
|
+
buildingBlockField: {
|
|
23
|
+
slug: "title",
|
|
24
|
+
},
|
|
25
|
+
contentValue: {
|
|
26
|
+
primitiveValue: "hello",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
] as BuildingBlockFieldFulfillment[]);
|
|
30
|
+
|
|
31
|
+
expect(result).toEqual({
|
|
32
|
+
title: "hello",
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should parse a building block with multiple values", () => {
|
|
37
|
+
const buildingBlockSchema = schema.buildingBlock({
|
|
38
|
+
title: schema.string(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const result = buildingBlockSchema.parse([
|
|
42
|
+
{
|
|
43
|
+
buildingBlockField: {
|
|
44
|
+
slug: "title",
|
|
45
|
+
},
|
|
46
|
+
contentValue: {
|
|
47
|
+
primitiveValue: "hello",
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
] as unknown as BuildingBlockFieldFulfillment[]);
|
|
51
|
+
|
|
52
|
+
expect(result).toEqual({
|
|
53
|
+
title: "hello",
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BuildingBlockFieldFulfillment } from "../../../__generated__/graphql";
|
|
2
|
+
import { ObjectSchema, Schema, SchemaConfig } from ".";
|
|
3
|
+
|
|
4
|
+
export class BuildingBlockSchema<T extends Record<string, Schema | Schema[]>>
|
|
5
|
+
implements Schema<ObjectSchema<T>, BuildingBlockFieldFulfillment>
|
|
6
|
+
{
|
|
7
|
+
public constructor(private readonly structure: T) {}
|
|
8
|
+
|
|
9
|
+
public parse(
|
|
10
|
+
data: BuildingBlockFieldFulfillment | BuildingBlockFieldFulfillment[],
|
|
11
|
+
config?: SchemaConfig,
|
|
12
|
+
): ObjectSchema<T> {
|
|
13
|
+
if (!Array.isArray(data)) {
|
|
14
|
+
data = [data];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const result = {} as unknown as ObjectSchema<T>;
|
|
18
|
+
|
|
19
|
+
for (const [key, schemaOrSchemas] of Object.entries(this.structure)) {
|
|
20
|
+
const valuesForField = data
|
|
21
|
+
.filter((item) => item.buildingBlockField.slug === key)
|
|
22
|
+
.map((item) => item.contentValue);
|
|
23
|
+
|
|
24
|
+
const schemas = Array.isArray(schemaOrSchemas) ? schemaOrSchemas : [schemaOrSchemas];
|
|
25
|
+
const parsedValues = schemas.map((schema) => schema.parse(valuesForField, config));
|
|
26
|
+
|
|
27
|
+
result[key as keyof T] = (parsedValues.find((parsedValue) => parsedValue !== null) ??
|
|
28
|
+
null) as ObjectSchema<T>[keyof T];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { ContentItemSchema } from "./contentItem";
|
|
3
|
+
import { schema } from ".";
|
|
4
|
+
|
|
5
|
+
describe("ContentItem schema", () => {
|
|
6
|
+
it("should parse an empty content item", () => {
|
|
7
|
+
const contentItemSchema = new ContentItemSchema({});
|
|
8
|
+
|
|
9
|
+
const result = contentItemSchema.parse({
|
|
10
|
+
contentValues: [],
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
expect(result).toEqual({});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should parse a content item with its values", () => {
|
|
17
|
+
const contentItemSchema = new ContentItemSchema({
|
|
18
|
+
title: schema.string(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const result = contentItemSchema.parse({
|
|
22
|
+
relatedContentItem: {
|
|
23
|
+
contentValues: [
|
|
24
|
+
{
|
|
25
|
+
contentField: {
|
|
26
|
+
name: "title",
|
|
27
|
+
},
|
|
28
|
+
primitiveValue: "hello",
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(result).toEqual({
|
|
35
|
+
title: "hello",
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should parse a content item with multiple values", () => {
|
|
40
|
+
const contentItemSchema = new ContentItemSchema({
|
|
41
|
+
title: [schema.number(), schema.string()],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const result = contentItemSchema.parse({
|
|
45
|
+
relatedContentItem: {
|
|
46
|
+
contentValues: [
|
|
47
|
+
{
|
|
48
|
+
contentField: {
|
|
49
|
+
name: "title",
|
|
50
|
+
},
|
|
51
|
+
primitiveValue: "hello",
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
expect(result).toEqual({
|
|
58
|
+
title: "hello",
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ObjectSchema, Schema, SchemaConfig } from ".";
|
|
2
|
+
import { isArray } from "lodash";
|
|
3
|
+
import { isContentValue } from "../utils/isContentValue";
|
|
4
|
+
|
|
5
|
+
export class ContentItemSchema<T extends Record<string, Schema | Schema[]>>
|
|
6
|
+
implements Schema<ObjectSchema<T>, unknown>
|
|
7
|
+
{
|
|
8
|
+
public constructor(private readonly structure: T) {}
|
|
9
|
+
|
|
10
|
+
public parse(data: unknown, config?: SchemaConfig): ObjectSchema<T> | null {
|
|
11
|
+
if (isArray(data)) {
|
|
12
|
+
data = data[0];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return Object.entries(this.structure).reduce(
|
|
16
|
+
(acc, [key, value]) => {
|
|
17
|
+
const relatedContentItem = isContentValue(data) ? data.relatedContentItem : null;
|
|
18
|
+
|
|
19
|
+
const contentValuesForKey =
|
|
20
|
+
relatedContentItem?.contentValues.filter((contentValue) => contentValue.contentField?.name === key) ?? [];
|
|
21
|
+
const shapes = Array.isArray(value) ? value : [value];
|
|
22
|
+
const parsedValues = shapes.map((shape) => shape.parse(contentValuesForKey, config));
|
|
23
|
+
|
|
24
|
+
acc[key as keyof T] = (parsedValues.find((parsedValue) => parsedValue !== null) ??
|
|
25
|
+
null) as ObjectSchema<T>[keyof T];
|
|
26
|
+
return acc;
|
|
27
|
+
},
|
|
28
|
+
{} as unknown as ObjectSchema<T>,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { ContentTypeSchema } from "./contentType";
|
|
3
|
+
|
|
4
|
+
describe("ContentType schema", () => {
|
|
5
|
+
it("should parse to a content type when parsing a content type content value", () => {
|
|
6
|
+
const schema = new ContentTypeSchema();
|
|
7
|
+
|
|
8
|
+
const result = schema.parse([
|
|
9
|
+
{
|
|
10
|
+
linkedContentType: {
|
|
11
|
+
prn: "123",
|
|
12
|
+
name: "title",
|
|
13
|
+
contentFields: [
|
|
14
|
+
{
|
|
15
|
+
name: "title",
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
expect(result).toEqual({
|
|
23
|
+
prn: "123",
|
|
24
|
+
name: "title",
|
|
25
|
+
contentFields: [
|
|
26
|
+
{
|
|
27
|
+
name: "title",
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should return null when parsing an a non content type content value", () => {
|
|
34
|
+
const schema = new ContentTypeSchema();
|
|
35
|
+
|
|
36
|
+
const result = schema.parse([
|
|
37
|
+
{
|
|
38
|
+
primitiveValue: "hello",
|
|
39
|
+
},
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
expect(result).toEqual(null);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("when placeholders are enabled", () => {
|
|
46
|
+
it("should return the placeholder when parsing an empty array", () => {
|
|
47
|
+
const schema = new ContentTypeSchema().placeholder({
|
|
48
|
+
prn: "123",
|
|
49
|
+
name: "title",
|
|
50
|
+
contentFields: [
|
|
51
|
+
{
|
|
52
|
+
name: "title",
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const result = schema.parse([], {
|
|
58
|
+
placeholders: true,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(result).toEqual({
|
|
62
|
+
prn: "123",
|
|
63
|
+
name: "title",
|
|
64
|
+
contentFields: [
|
|
65
|
+
{
|
|
66
|
+
name: "title",
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should return the placeholder when parsing an non content type content value", () => {
|
|
73
|
+
const schema = new ContentTypeSchema().placeholder({
|
|
74
|
+
prn: "123",
|
|
75
|
+
name: "title",
|
|
76
|
+
contentFields: [
|
|
77
|
+
{
|
|
78
|
+
name: "title",
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const result = schema.parse(
|
|
84
|
+
[
|
|
85
|
+
{
|
|
86
|
+
primitiveValue: true,
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
{
|
|
90
|
+
placeholders: true,
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
expect(result).toEqual({
|
|
95
|
+
prn: "123",
|
|
96
|
+
name: "title",
|
|
97
|
+
contentFields: [
|
|
98
|
+
{
|
|
99
|
+
name: "title",
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("when nullable is disabled", () => {
|
|
107
|
+
it("should throw an error when parsing an non content type content value", () => {
|
|
108
|
+
const schema = new ContentTypeSchema().nullable(false);
|
|
109
|
+
|
|
110
|
+
expect(() => schema.parse([{ primitiveValue: 42 }])).toThrow("Data could not be found and is not nullable");
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ContentType } from "../../../__generated__/graphql";
|
|
2
|
+
import { BaseSchema } from "./baseSchema";
|
|
3
|
+
|
|
4
|
+
export class ContentTypeSchema extends BaseSchema<ContentType | null, unknown> {
|
|
5
|
+
protected override findValue(data: unknown): ContentType | null {
|
|
6
|
+
if (typeof data === "object" && data !== null && "linkedContentType" in data) {
|
|
7
|
+
return (data as { linkedContentType: ContentType }).linkedContentType;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { DateSchema } from "./date";
|
|
3
|
+
|
|
4
|
+
describe("Date schema", () => {
|
|
5
|
+
it("should parse to a date when parsing a date content value", () => {
|
|
6
|
+
const schema = new DateSchema();
|
|
7
|
+
|
|
8
|
+
const result = schema.parse([
|
|
9
|
+
{
|
|
10
|
+
primitiveValue: "2021-01-01",
|
|
11
|
+
},
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
expect(result).toEqual(new Date("2021-01-01"));
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return null when parsing an a non date content value", () => {
|
|
18
|
+
const schema = new DateSchema();
|
|
19
|
+
|
|
20
|
+
const result = schema.parse([
|
|
21
|
+
{
|
|
22
|
+
primitiveValue: true,
|
|
23
|
+
},
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
expect(result).toEqual(null);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("when placeholders are enabled", () => {
|
|
30
|
+
it("should return the placeholder when parsing an empty array", () => {
|
|
31
|
+
const schema = new DateSchema().placeholderFromString("2021-01-01");
|
|
32
|
+
|
|
33
|
+
const result = schema.parse([], {
|
|
34
|
+
placeholders: true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(result).toEqual(new Date("2021-01-01"));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should return the placeholder when parsing an non date content value", () => {
|
|
41
|
+
const schema = new DateSchema().placeholder(new Date("2021-01-01"));
|
|
42
|
+
|
|
43
|
+
const result = schema.parse(
|
|
44
|
+
[
|
|
45
|
+
{
|
|
46
|
+
primitiveValue: true,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
{
|
|
50
|
+
placeholders: true,
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(result).toEqual(new Date("2021-01-01"));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should return the placeholder when parsing an non date content value", () => {
|
|
58
|
+
const schema = new DateSchema().placeholder(new Date("2021-01-01"));
|
|
59
|
+
|
|
60
|
+
const result = schema.parse(
|
|
61
|
+
[
|
|
62
|
+
{
|
|
63
|
+
primitiveValue: true,
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
{
|
|
67
|
+
placeholders: true,
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
expect(result).toEqual(new Date("2021-01-01"));
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("when nullable is disabled", () => {
|
|
76
|
+
it("should throw an error when parsing an non date content value", () => {
|
|
77
|
+
const schema = new DateSchema().nullable(false);
|
|
78
|
+
|
|
79
|
+
expect(() => schema.parse([{ primitiveValue: true }])).toThrow("Data could not be found and is not nullable");
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseSchema } from "./baseSchema";
|
|
2
|
+
import { isPrimitiveValue } from "../utils/isPrimitiveValue";
|
|
3
|
+
|
|
4
|
+
export class DateSchema extends BaseSchema<Date | null, unknown> {
|
|
5
|
+
public placeholderFromString(date: string): this {
|
|
6
|
+
this._placeholder = new Date(date);
|
|
7
|
+
return this;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
protected override findValue(data: unknown): Date | null {
|
|
11
|
+
if (isPrimitiveValue(data) && typeof data.primitiveValue === "string") {
|
|
12
|
+
return new Date(data.primitiveValue);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { GridPlacementSchema } from "./gridPlacement";
|
|
3
|
+
|
|
4
|
+
describe("GridPlacement schema", () => {
|
|
5
|
+
it("should parse to a grid placement when parsing a grid placement content value", () => {
|
|
6
|
+
const schema = new GridPlacementSchema();
|
|
7
|
+
|
|
8
|
+
const result = schema.parse([
|
|
9
|
+
{
|
|
10
|
+
gridPlacement: {
|
|
11
|
+
row: 0,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
expect(result).toEqual({
|
|
17
|
+
row: 0,
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should return null when parsing an a non grid placement content value", () => {
|
|
22
|
+
const schema = new GridPlacementSchema();
|
|
23
|
+
|
|
24
|
+
const result = schema.parse([
|
|
25
|
+
{
|
|
26
|
+
primitiveValue: "hello",
|
|
27
|
+
},
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
expect(result).toEqual(null);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("when placeholders are enabled", () => {
|
|
34
|
+
it("should return the placeholder when parsing an empty array", () => {
|
|
35
|
+
const schema = new GridPlacementSchema().placeholder({
|
|
36
|
+
row: 0,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const result = schema.parse([], {
|
|
40
|
+
placeholders: true,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
expect(result).toEqual({
|
|
44
|
+
row: 0,
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should return the placeholder when parsing an non grid placement content value", () => {
|
|
49
|
+
const schema = new GridPlacementSchema().placeholder({
|
|
50
|
+
row: 0,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const result = schema.parse(
|
|
54
|
+
[
|
|
55
|
+
{
|
|
56
|
+
primitiveValue: true,
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
{
|
|
60
|
+
placeholders: true,
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
expect(result).toEqual({
|
|
65
|
+
row: 0,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("when nullable is disabled", () => {
|
|
71
|
+
it("should throw an error when parsing an non grid placement content value", () => {
|
|
72
|
+
const schema = new GridPlacementSchema().nullable(false);
|
|
73
|
+
|
|
74
|
+
expect(() => schema.parse([{ primitiveValue: 42 }])).toThrow("Data could not be found and is not nullable");
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GridPlacement } from "../../../__generated__/graphql";
|
|
2
|
+
import { BaseSchema } from "./baseSchema";
|
|
3
|
+
|
|
4
|
+
export class GridPlacementSchema extends BaseSchema<GridPlacement | null, unknown> {
|
|
5
|
+
protected override findValue(data: unknown): GridPlacement | null {
|
|
6
|
+
if (typeof data === "object" && data !== null && "gridPlacement" in data) {
|
|
7
|
+
return (data as { gridPlacement: GridPlacement }).gridPlacement;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { StringSchema } from "./string";
|
|
2
|
+
import { ContentItemSchema } from "./contentItem";
|
|
3
|
+
import { ArraySchema } from "./array";
|
|
4
|
+
import { BuildingBlockSchema } from "./buildingBlock";
|
|
5
|
+
import { NumberSchema } from "./number";
|
|
6
|
+
import { BooleanSchema } from "./boolean";
|
|
7
|
+
import { DateSchema } from "./date";
|
|
8
|
+
import { AssetSchema } from "./asset";
|
|
9
|
+
import { SmartTextSchema } from "./smartText";
|
|
10
|
+
import { GridPlacementSchema } from "./gridPlacement";
|
|
11
|
+
import { PathPartSchema } from "./pathPart";
|
|
12
|
+
import { TagSchema } from "./tag";
|
|
13
|
+
import { ContentTypeSchema } from "./contentType";
|
|
14
|
+
import { Asset, ContentType, GridPlacement, PathPart, Tag } from "../../../__generated__/graphql";
|
|
15
|
+
import { Root } from "@platecms/delta-cast";
|
|
16
|
+
|
|
17
|
+
export type ResultType =
|
|
18
|
+
| Asset
|
|
19
|
+
| ContentType
|
|
20
|
+
| Date
|
|
21
|
+
| GridPlacement
|
|
22
|
+
| PathPart
|
|
23
|
+
| Root
|
|
24
|
+
| Tag
|
|
25
|
+
| boolean
|
|
26
|
+
| number
|
|
27
|
+
| string
|
|
28
|
+
| null;
|
|
29
|
+
|
|
30
|
+
export interface Schema<TResult = unknown, TData = unknown> {
|
|
31
|
+
parse: (data: TData | TData[], config?: SchemaConfig) => TResult | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type ObjectSchema<T extends Record<string, Schema | Schema[]>> = {
|
|
35
|
+
[key in keyof T]: T[key] extends Schema[]
|
|
36
|
+
? ReturnType<T[key][number]["parse"]> | null
|
|
37
|
+
: T[key] extends Schema
|
|
38
|
+
? ReturnType<T[key]["parse"]> | null
|
|
39
|
+
: never;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export interface SchemaConfig {
|
|
43
|
+
placeholders?: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
47
|
+
export const schema = {
|
|
48
|
+
buildingBlock: <T extends Record<string, Schema | Schema[]>>(structure: T): BuildingBlockSchema<T> =>
|
|
49
|
+
new BuildingBlockSchema(structure),
|
|
50
|
+
contentItem: <T extends Record<string, Schema | Schema[]>>(structure: T): ContentItemSchema<T> =>
|
|
51
|
+
new ContentItemSchema(structure),
|
|
52
|
+
array: <T extends Schema | Schema[]>(structure: T): ArraySchema<T> => new ArraySchema(structure),
|
|
53
|
+
string: (): StringSchema => new StringSchema(),
|
|
54
|
+
number: (): NumberSchema => new NumberSchema(),
|
|
55
|
+
boolean: (): BooleanSchema => new BooleanSchema(),
|
|
56
|
+
date: (): DateSchema => new DateSchema(),
|
|
57
|
+
asset: (): AssetSchema => new AssetSchema(),
|
|
58
|
+
smartText: (): SmartTextSchema => new SmartTextSchema(),
|
|
59
|
+
gridPlacement: (): GridPlacementSchema => new GridPlacementSchema(),
|
|
60
|
+
contentType: (): ContentTypeSchema => new ContentTypeSchema(),
|
|
61
|
+
pathPart: (): PathPartSchema => new PathPartSchema(),
|
|
62
|
+
tag: (): TagSchema => new TagSchema(),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention, id-length
|
|
66
|
+
export const s = schema;
|