@zeropress/preview-data-validator 0.1.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/LICENSE +21 -0
- package/README.md +57 -0
- package/package.json +25 -0
- package/src/index.d.ts +121 -0
- package/src/index.js +355 -0
- package/src/preview-data.v0.2.schema.json +215 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ZeroPress
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @zeropress/preview-data-validator
|
|
2
|
+
|
|
3
|
+
Shared validation core for ZeroPress preview data v0.2.
|
|
4
|
+
|
|
5
|
+
This package is the canonical runtime contract for preview payloads consumed by:
|
|
6
|
+
|
|
7
|
+
- `zeropress-theme dev`
|
|
8
|
+
- `zeropress-admin-api-v2`
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @zeropress/preview-data-validator
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Exports
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import {
|
|
20
|
+
PREVIEW_DATA_VERSION,
|
|
21
|
+
assertPreviewData,
|
|
22
|
+
isPreviewData,
|
|
23
|
+
validatePreviewData,
|
|
24
|
+
} from '@zeropress/preview-data-validator';
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Schema export:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import schemaUrl from '@zeropress/preview-data-validator/preview-data.v0.2.schema.json';
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## API
|
|
34
|
+
|
|
35
|
+
### `validatePreviewData(data)`
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
{
|
|
41
|
+
ok: true,
|
|
42
|
+
errors: [],
|
|
43
|
+
warnings: []
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### `assertPreviewData(data)`
|
|
48
|
+
|
|
49
|
+
Throws when the payload is invalid.
|
|
50
|
+
|
|
51
|
+
### `isPreviewData(data)`
|
|
52
|
+
|
|
53
|
+
Returns `true` when the payload is valid.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zeropress/preview-data-validator",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared ZeroPress preview data validation core",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./preview-data.v0.2.schema.json": "./src/preview-data.v0.2.schema.json"
|
|
10
|
+
},
|
|
11
|
+
"types": "./src/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/zeropress-app/zeropress-preview-data-validator.git"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --test"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18.18.0"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
export type PreviewStatus = 'published' | 'draft';
|
|
2
|
+
|
|
3
|
+
export interface ValidationIssue {
|
|
4
|
+
code: string;
|
|
5
|
+
path: string;
|
|
6
|
+
message: string;
|
|
7
|
+
severity: 'error' | 'warning';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface PreviewSiteData {
|
|
11
|
+
title: string;
|
|
12
|
+
description: string;
|
|
13
|
+
url: string;
|
|
14
|
+
language: string;
|
|
15
|
+
logo?: string;
|
|
16
|
+
social?: Record<string, string>;
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PreviewPostData {
|
|
21
|
+
id: string;
|
|
22
|
+
public_id: number;
|
|
23
|
+
title: string;
|
|
24
|
+
slug: string;
|
|
25
|
+
html: string;
|
|
26
|
+
excerpt: string;
|
|
27
|
+
published_at: string;
|
|
28
|
+
updated_at: string;
|
|
29
|
+
published_at_iso: string;
|
|
30
|
+
updated_at_iso: string;
|
|
31
|
+
reading_time: string;
|
|
32
|
+
author_name: string;
|
|
33
|
+
author_avatar?: string;
|
|
34
|
+
featured_image?: string;
|
|
35
|
+
categories_html: string;
|
|
36
|
+
tags_html: string;
|
|
37
|
+
comments_html: string;
|
|
38
|
+
status: PreviewStatus;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface PreviewPageData {
|
|
42
|
+
id: string;
|
|
43
|
+
title: string;
|
|
44
|
+
slug: string;
|
|
45
|
+
html: string;
|
|
46
|
+
status: PreviewStatus;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface PreviewCategoryData {
|
|
50
|
+
id: string;
|
|
51
|
+
name: string;
|
|
52
|
+
slug: string;
|
|
53
|
+
description?: string;
|
|
54
|
+
postCount: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface PreviewTagData {
|
|
58
|
+
id: string;
|
|
59
|
+
name: string;
|
|
60
|
+
slug: string;
|
|
61
|
+
postCount: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface PreviewRouteBlocks {
|
|
65
|
+
posts: string;
|
|
66
|
+
pagination: string;
|
|
67
|
+
categories?: string;
|
|
68
|
+
tags?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface PreviewCategoryRouteData {
|
|
72
|
+
slug: string;
|
|
73
|
+
posts: string;
|
|
74
|
+
pagination: string;
|
|
75
|
+
categories?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface PreviewTagRouteData {
|
|
79
|
+
slug: string;
|
|
80
|
+
posts: string;
|
|
81
|
+
pagination: string;
|
|
82
|
+
tags?: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface PreviewContentData {
|
|
86
|
+
posts: PreviewPostData[];
|
|
87
|
+
pages: PreviewPageData[];
|
|
88
|
+
categories: PreviewCategoryData[];
|
|
89
|
+
tags: PreviewTagData[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface PreviewRoutesData {
|
|
93
|
+
index: PreviewRouteBlocks & {
|
|
94
|
+
categories: string;
|
|
95
|
+
tags: string;
|
|
96
|
+
};
|
|
97
|
+
archive: PreviewRouteBlocks;
|
|
98
|
+
categories: PreviewCategoryRouteData[];
|
|
99
|
+
tags: PreviewTagRouteData[];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface PreviewDataV02 {
|
|
103
|
+
version: '0.2';
|
|
104
|
+
generator: string;
|
|
105
|
+
generated_at: string;
|
|
106
|
+
site: PreviewSiteData;
|
|
107
|
+
content: PreviewContentData;
|
|
108
|
+
routes: PreviewRoutesData;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface PreviewDataValidationResult {
|
|
112
|
+
ok: boolean;
|
|
113
|
+
errors: ValidationIssue[];
|
|
114
|
+
warnings: ValidationIssue[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const PREVIEW_DATA_VERSION: '0.2';
|
|
118
|
+
|
|
119
|
+
export function validatePreviewData(data: unknown): PreviewDataValidationResult;
|
|
120
|
+
export function assertPreviewData<T>(data: T): T;
|
|
121
|
+
export function isPreviewData(data: unknown): data is PreviewDataV02;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
export const PREVIEW_DATA_VERSION = '0.2';
|
|
2
|
+
|
|
3
|
+
export function validatePreviewData(data) {
|
|
4
|
+
const errors = [];
|
|
5
|
+
|
|
6
|
+
validateClosedObject(data, '', errors, ['version', 'generator', 'generated_at', 'site', 'content', 'routes']);
|
|
7
|
+
|
|
8
|
+
if (!errors.length) {
|
|
9
|
+
validateLiteral(data.version, '0.2', 'version', 'INVALID_VERSION', errors);
|
|
10
|
+
validateNonEmptyString(data.generator, 'generator', 'INVALID_GENERATOR', errors);
|
|
11
|
+
validateDateTimeString(data.generated_at, 'generated_at', 'INVALID_GENERATED_AT', errors);
|
|
12
|
+
|
|
13
|
+
validateSite(data.site, 'site', errors);
|
|
14
|
+
validateContent(data.content, 'content', errors);
|
|
15
|
+
validateRoutes(data.routes, 'routes', errors);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
ok: errors.length === 0,
|
|
20
|
+
errors,
|
|
21
|
+
warnings: [],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function assertPreviewData(data) {
|
|
26
|
+
const result = validatePreviewData(data);
|
|
27
|
+
if (!result.ok) {
|
|
28
|
+
const first = result.errors[0];
|
|
29
|
+
throw new Error(first ? `${first.code} ${first.path}: ${first.message}` : 'Invalid preview data');
|
|
30
|
+
}
|
|
31
|
+
return data;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function isPreviewData(data) {
|
|
35
|
+
return validatePreviewData(data).ok;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function validateSite(site, path, errors) {
|
|
39
|
+
validateObject(site, path, 'INVALID_SITE', errors);
|
|
40
|
+
if (!isObject(site)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
validateNonEmptyString(site.title, `${path}.title`, 'INVALID_SITE_TITLE', errors);
|
|
45
|
+
validateString(site.description, `${path}.description`, 'INVALID_SITE_DESCRIPTION', errors);
|
|
46
|
+
validateUri(site.url, `${path}.url`, 'INVALID_SITE_URL', errors);
|
|
47
|
+
validateNonEmptyString(site.language, `${path}.language`, 'INVALID_SITE_LANGUAGE', errors);
|
|
48
|
+
|
|
49
|
+
if (site.logo !== undefined) {
|
|
50
|
+
validateUri(site.logo, `${path}.logo`, 'INVALID_SITE_LOGO', errors);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (site.social !== undefined) {
|
|
54
|
+
validateObject(site.social, `${path}.social`, 'INVALID_SITE_SOCIAL', errors);
|
|
55
|
+
if (isObject(site.social)) {
|
|
56
|
+
for (const [key, value] of Object.entries(site.social)) {
|
|
57
|
+
validateString(value, `${path}.social.${key}`, 'INVALID_SITE_SOCIAL_VALUE', errors);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function validateContent(content, path, errors) {
|
|
64
|
+
validateClosedObject(content, path, errors, ['posts', 'pages', 'categories', 'tags']);
|
|
65
|
+
if (!isObject(content)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
validateArray(content.posts, `${path}.posts`, 'INVALID_POSTS', errors, (entry, index) => {
|
|
70
|
+
validatePreviewPost(entry, `${path}.posts[${index}]`, errors);
|
|
71
|
+
});
|
|
72
|
+
validateArray(content.pages, `${path}.pages`, 'INVALID_PAGES', errors, (entry, index) => {
|
|
73
|
+
validatePreviewPage(entry, `${path}.pages[${index}]`, errors);
|
|
74
|
+
});
|
|
75
|
+
validateArray(content.categories, `${path}.categories`, 'INVALID_CATEGORIES', errors, (entry, index) => {
|
|
76
|
+
validatePreviewCategory(entry, `${path}.categories[${index}]`, errors);
|
|
77
|
+
});
|
|
78
|
+
validateArray(content.tags, `${path}.tags`, 'INVALID_TAGS', errors, (entry, index) => {
|
|
79
|
+
validatePreviewTag(entry, `${path}.tags[${index}]`, errors);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function validateRoutes(routes, path, errors) {
|
|
84
|
+
validateClosedObject(routes, path, errors, ['index', 'archive', 'categories', 'tags']);
|
|
85
|
+
if (!isObject(routes)) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
validateRouteBlocks(routes.index, `${path}.index`, errors, {
|
|
90
|
+
requiredKeys: ['posts', 'pagination', 'categories', 'tags'],
|
|
91
|
+
allowKeys: ['posts', 'pagination', 'categories', 'tags'],
|
|
92
|
+
});
|
|
93
|
+
validateRouteBlocks(routes.archive, `${path}.archive`, errors, {
|
|
94
|
+
requiredKeys: ['posts', 'pagination'],
|
|
95
|
+
allowKeys: ['posts', 'pagination', 'categories', 'tags'],
|
|
96
|
+
});
|
|
97
|
+
validateArray(routes.categories, `${path}.categories`, 'INVALID_CATEGORY_ROUTES', errors, (entry, index) => {
|
|
98
|
+
validateCategoryRoute(entry, `${path}.categories[${index}]`, errors);
|
|
99
|
+
});
|
|
100
|
+
validateArray(routes.tags, `${path}.tags`, 'INVALID_TAG_ROUTES', errors, (entry, index) => {
|
|
101
|
+
validateTagRoute(entry, `${path}.tags[${index}]`, errors);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function validatePreviewPost(post, path, errors) {
|
|
106
|
+
validateClosedObject(post, path, errors, [
|
|
107
|
+
'id',
|
|
108
|
+
'public_id',
|
|
109
|
+
'title',
|
|
110
|
+
'slug',
|
|
111
|
+
'html',
|
|
112
|
+
'excerpt',
|
|
113
|
+
'published_at',
|
|
114
|
+
'updated_at',
|
|
115
|
+
'published_at_iso',
|
|
116
|
+
'updated_at_iso',
|
|
117
|
+
'reading_time',
|
|
118
|
+
'author_name',
|
|
119
|
+
'author_avatar',
|
|
120
|
+
'featured_image',
|
|
121
|
+
'categories_html',
|
|
122
|
+
'tags_html',
|
|
123
|
+
'comments_html',
|
|
124
|
+
'status',
|
|
125
|
+
]);
|
|
126
|
+
if (!isObject(post)) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
validateNonEmptyString(post.id, `${path}.id`, 'INVALID_POST_ID', errors);
|
|
131
|
+
validateInteger(post.public_id, `${path}.public_id`, 'INVALID_POST_PUBLIC_ID', errors, { minimum: 1 });
|
|
132
|
+
validateNonEmptyString(post.title, `${path}.title`, 'INVALID_POST_TITLE', errors);
|
|
133
|
+
validateNonEmptyString(post.slug, `${path}.slug`, 'INVALID_POST_SLUG', errors);
|
|
134
|
+
validateString(post.html, `${path}.html`, 'INVALID_POST_HTML', errors);
|
|
135
|
+
validateString(post.excerpt, `${path}.excerpt`, 'INVALID_POST_EXCERPT', errors);
|
|
136
|
+
validateNonEmptyString(post.published_at, `${path}.published_at`, 'INVALID_POST_PUBLISHED_AT', errors);
|
|
137
|
+
validateNonEmptyString(post.updated_at, `${path}.updated_at`, 'INVALID_POST_UPDATED_AT', errors);
|
|
138
|
+
validateDateTimeString(post.published_at_iso, `${path}.published_at_iso`, 'INVALID_POST_PUBLISHED_AT_ISO', errors);
|
|
139
|
+
validateDateTimeString(post.updated_at_iso, `${path}.updated_at_iso`, 'INVALID_POST_UPDATED_AT_ISO', errors);
|
|
140
|
+
validateNonEmptyString(post.reading_time, `${path}.reading_time`, 'INVALID_POST_READING_TIME', errors);
|
|
141
|
+
validateNonEmptyString(post.author_name, `${path}.author_name`, 'INVALID_POST_AUTHOR_NAME', errors);
|
|
142
|
+
validateString(post.categories_html, `${path}.categories_html`, 'INVALID_POST_CATEGORIES_HTML', errors);
|
|
143
|
+
validateString(post.tags_html, `${path}.tags_html`, 'INVALID_POST_TAGS_HTML', errors);
|
|
144
|
+
validateString(post.comments_html, `${path}.comments_html`, 'INVALID_POST_COMMENTS_HTML', errors);
|
|
145
|
+
validateEnum(post.status, `${path}.status`, 'INVALID_POST_STATUS', errors, ['published', 'draft']);
|
|
146
|
+
|
|
147
|
+
if (post.author_avatar !== undefined) {
|
|
148
|
+
validateUri(post.author_avatar, `${path}.author_avatar`, 'INVALID_POST_AUTHOR_AVATAR', errors);
|
|
149
|
+
}
|
|
150
|
+
if (post.featured_image !== undefined) {
|
|
151
|
+
validateUri(post.featured_image, `${path}.featured_image`, 'INVALID_POST_FEATURED_IMAGE', errors);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function validatePreviewPage(page, path, errors) {
|
|
156
|
+
validateClosedObject(page, path, errors, ['id', 'title', 'slug', 'html', 'status']);
|
|
157
|
+
if (!isObject(page)) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
validateNonEmptyString(page.id, `${path}.id`, 'INVALID_PAGE_ID', errors);
|
|
162
|
+
validateNonEmptyString(page.title, `${path}.title`, 'INVALID_PAGE_TITLE', errors);
|
|
163
|
+
validateNonEmptyString(page.slug, `${path}.slug`, 'INVALID_PAGE_SLUG', errors);
|
|
164
|
+
validateString(page.html, `${path}.html`, 'INVALID_PAGE_HTML', errors);
|
|
165
|
+
validateEnum(page.status, `${path}.status`, 'INVALID_PAGE_STATUS', errors, ['published', 'draft']);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function validatePreviewCategory(category, path, errors) {
|
|
169
|
+
validateClosedObject(category, path, errors, ['id', 'name', 'slug', 'description', 'postCount']);
|
|
170
|
+
if (!isObject(category)) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
validateNonEmptyString(category.id, `${path}.id`, 'INVALID_CATEGORY_ID', errors);
|
|
175
|
+
validateNonEmptyString(category.name, `${path}.name`, 'INVALID_CATEGORY_NAME', errors);
|
|
176
|
+
validateNonEmptyString(category.slug, `${path}.slug`, 'INVALID_CATEGORY_SLUG', errors);
|
|
177
|
+
validateInteger(category.postCount, `${path}.postCount`, 'INVALID_CATEGORY_POST_COUNT', errors, { minimum: 0 });
|
|
178
|
+
if (category.description !== undefined) {
|
|
179
|
+
validateString(category.description, `${path}.description`, 'INVALID_CATEGORY_DESCRIPTION', errors);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function validatePreviewTag(tag, path, errors) {
|
|
184
|
+
validateClosedObject(tag, path, errors, ['id', 'name', 'slug', 'postCount']);
|
|
185
|
+
if (!isObject(tag)) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
validateNonEmptyString(tag.id, `${path}.id`, 'INVALID_TAG_ID', errors);
|
|
190
|
+
validateNonEmptyString(tag.name, `${path}.name`, 'INVALID_TAG_NAME', errors);
|
|
191
|
+
validateNonEmptyString(tag.slug, `${path}.slug`, 'INVALID_TAG_SLUG', errors);
|
|
192
|
+
validateInteger(tag.postCount, `${path}.postCount`, 'INVALID_TAG_POST_COUNT', errors, { minimum: 0 });
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function validateCategoryRoute(route, path, errors) {
|
|
196
|
+
validateClosedObject(route, path, errors, ['slug', 'posts', 'pagination', 'categories']);
|
|
197
|
+
if (!isObject(route)) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
validateNonEmptyString(route.slug, `${path}.slug`, 'INVALID_CATEGORY_ROUTE_SLUG', errors);
|
|
202
|
+
validateString(route.posts, `${path}.posts`, 'INVALID_CATEGORY_ROUTE_POSTS', errors);
|
|
203
|
+
validateString(route.pagination, `${path}.pagination`, 'INVALID_CATEGORY_ROUTE_PAGINATION', errors);
|
|
204
|
+
if (route.categories !== undefined) {
|
|
205
|
+
validateString(route.categories, `${path}.categories`, 'INVALID_CATEGORY_ROUTE_CATEGORIES', errors);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function validateTagRoute(route, path, errors) {
|
|
210
|
+
validateClosedObject(route, path, errors, ['slug', 'posts', 'pagination', 'tags']);
|
|
211
|
+
if (!isObject(route)) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
validateNonEmptyString(route.slug, `${path}.slug`, 'INVALID_TAG_ROUTE_SLUG', errors);
|
|
216
|
+
validateString(route.posts, `${path}.posts`, 'INVALID_TAG_ROUTE_POSTS', errors);
|
|
217
|
+
validateString(route.pagination, `${path}.pagination`, 'INVALID_TAG_ROUTE_PAGINATION', errors);
|
|
218
|
+
if (route.tags !== undefined) {
|
|
219
|
+
validateString(route.tags, `${path}.tags`, 'INVALID_TAG_ROUTE_TAGS', errors);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function validateRouteBlocks(blocks, path, errors, options) {
|
|
224
|
+
validateClosedObject(blocks, path, errors, options.allowKeys);
|
|
225
|
+
if (!isObject(blocks)) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
for (const key of options.requiredKeys) {
|
|
230
|
+
validateString(blocks[key], `${path}.${key}`, `INVALID_ROUTE_${key.toUpperCase()}`, errors);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
for (const key of options.allowKeys) {
|
|
234
|
+
if (blocks[key] !== undefined) {
|
|
235
|
+
validateString(blocks[key], `${path}.${key}`, `INVALID_ROUTE_${key.toUpperCase()}`, errors);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function validateArray(value, path, code, errors, itemValidator) {
|
|
241
|
+
if (!Array.isArray(value)) {
|
|
242
|
+
errors.push(issue(code, path, 'Expected an array'));
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
value.forEach((entry, index) => itemValidator(entry, index));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function validateClosedObject(value, path, errors, allowedKeys) {
|
|
250
|
+
validateObject(value, path, 'INVALID_OBJECT', errors);
|
|
251
|
+
if (!isObject(value)) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
for (const key of Object.keys(value)) {
|
|
256
|
+
if (!allowedKeys.includes(key)) {
|
|
257
|
+
errors.push(issue('UNKNOWN_PROPERTY', path ? `${path}.${key}` : key, 'Unexpected property'));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
for (const key of allowedKeys) {
|
|
262
|
+
if (!(key in value) && !isOptionalKey(path, key)) {
|
|
263
|
+
errors.push(issue('MISSING_REQUIRED_PROPERTY', path ? `${path}.${key}` : key, 'Missing required property'));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function isOptionalKey(path, key) {
|
|
269
|
+
if (path === 'site') return key === 'logo' || key === 'social';
|
|
270
|
+
if (path.endsWith('.index')) return false;
|
|
271
|
+
if (path.endsWith('.archive')) return key === 'categories' || key === 'tags';
|
|
272
|
+
if (path.includes('categories[')) return key === 'categories';
|
|
273
|
+
if (path.includes('tags[')) return key === 'tags';
|
|
274
|
+
if (path.includes('.posts[')) return key === 'author_avatar' || key === 'featured_image';
|
|
275
|
+
if (path.includes('.categories[')) return key === 'description';
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function validateObject(value, path, code, errors) {
|
|
280
|
+
if (!isObject(value)) {
|
|
281
|
+
errors.push(issue(code, path, 'Expected an object'));
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function validateLiteral(value, expected, path, code, errors) {
|
|
286
|
+
if (value !== expected) {
|
|
287
|
+
errors.push(issue(code, path, `Expected '${expected}'`));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function validateNonEmptyString(value, path, code, errors) {
|
|
292
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
293
|
+
errors.push(issue(code, path, 'Expected a non-empty string'));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function validateString(value, path, code, errors) {
|
|
298
|
+
if (typeof value !== 'string') {
|
|
299
|
+
errors.push(issue(code, path, 'Expected a string'));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function validateInteger(value, path, code, errors, options = {}) {
|
|
304
|
+
if (!Number.isInteger(value)) {
|
|
305
|
+
errors.push(issue(code, path, 'Expected an integer'));
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (options.minimum !== undefined && value < options.minimum) {
|
|
309
|
+
errors.push(issue(code, path, `Expected an integer >= ${options.minimum}`));
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function validateEnum(value, path, code, errors, allowedValues) {
|
|
314
|
+
if (!allowedValues.includes(value)) {
|
|
315
|
+
errors.push(issue(code, path, `Expected one of: ${allowedValues.join(', ')}`));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function validateUri(value, path, code, errors) {
|
|
320
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
321
|
+
errors.push(issue(code, path, 'Expected a URI string'));
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
try {
|
|
326
|
+
new URL(value);
|
|
327
|
+
} catch {
|
|
328
|
+
errors.push(issue(code, path, 'Expected a valid URI'));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function validateDateTimeString(value, path, code, errors) {
|
|
333
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
334
|
+
errors.push(issue(code, path, 'Expected an ISO date-time string'));
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const parsed = Date.parse(value);
|
|
339
|
+
if (Number.isNaN(parsed)) {
|
|
340
|
+
errors.push(issue(code, path, 'Expected a valid ISO date-time string'));
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function isObject(value) {
|
|
345
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function issue(code, path, message) {
|
|
349
|
+
return {
|
|
350
|
+
code,
|
|
351
|
+
path,
|
|
352
|
+
message,
|
|
353
|
+
severity: 'error',
|
|
354
|
+
};
|
|
355
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://zeropress.dev/schemas/preview-data.v0.2.schema.json",
|
|
4
|
+
"title": "ZeroPress Theme Preview Data v0.2",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"additionalProperties": false,
|
|
7
|
+
"required": ["version", "generator", "generated_at", "site", "content", "routes"],
|
|
8
|
+
"properties": {
|
|
9
|
+
"version": {
|
|
10
|
+
"const": "0.2"
|
|
11
|
+
},
|
|
12
|
+
"generator": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"minLength": 1
|
|
15
|
+
},
|
|
16
|
+
"generated_at": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
"format": "date-time"
|
|
19
|
+
},
|
|
20
|
+
"site": {
|
|
21
|
+
"$ref": "#/$defs/site"
|
|
22
|
+
},
|
|
23
|
+
"content": {
|
|
24
|
+
"$ref": "#/$defs/content"
|
|
25
|
+
},
|
|
26
|
+
"routes": {
|
|
27
|
+
"$ref": "#/$defs/routes"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"$defs": {
|
|
31
|
+
"site": {
|
|
32
|
+
"type": "object",
|
|
33
|
+
"required": ["title", "description", "url", "language"],
|
|
34
|
+
"properties": {
|
|
35
|
+
"title": { "type": "string", "minLength": 1 },
|
|
36
|
+
"description": { "type": "string" },
|
|
37
|
+
"url": { "type": "string", "format": "uri" },
|
|
38
|
+
"language": { "type": "string", "minLength": 2 },
|
|
39
|
+
"logo": { "type": "string", "format": "uri" },
|
|
40
|
+
"social": {
|
|
41
|
+
"type": "object",
|
|
42
|
+
"additionalProperties": {
|
|
43
|
+
"type": "string"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"additionalProperties": true
|
|
48
|
+
},
|
|
49
|
+
"previewPost": {
|
|
50
|
+
"type": "object",
|
|
51
|
+
"additionalProperties": false,
|
|
52
|
+
"required": [
|
|
53
|
+
"id",
|
|
54
|
+
"public_id",
|
|
55
|
+
"title",
|
|
56
|
+
"slug",
|
|
57
|
+
"html",
|
|
58
|
+
"excerpt",
|
|
59
|
+
"published_at",
|
|
60
|
+
"updated_at",
|
|
61
|
+
"published_at_iso",
|
|
62
|
+
"updated_at_iso",
|
|
63
|
+
"reading_time",
|
|
64
|
+
"author_name",
|
|
65
|
+
"categories_html",
|
|
66
|
+
"tags_html",
|
|
67
|
+
"comments_html",
|
|
68
|
+
"status"
|
|
69
|
+
],
|
|
70
|
+
"properties": {
|
|
71
|
+
"id": { "type": "string", "minLength": 1 },
|
|
72
|
+
"public_id": { "type": "integer", "minimum": 1 },
|
|
73
|
+
"title": { "type": "string", "minLength": 1 },
|
|
74
|
+
"slug": { "type": "string", "minLength": 1 },
|
|
75
|
+
"html": { "type": "string" },
|
|
76
|
+
"excerpt": { "type": "string" },
|
|
77
|
+
"published_at": { "type": "string" },
|
|
78
|
+
"updated_at": { "type": "string" },
|
|
79
|
+
"published_at_iso": { "type": "string", "format": "date-time" },
|
|
80
|
+
"updated_at_iso": { "type": "string", "format": "date-time" },
|
|
81
|
+
"reading_time": { "type": "string", "minLength": 1 },
|
|
82
|
+
"author_name": { "type": "string", "minLength": 1 },
|
|
83
|
+
"author_avatar": { "type": "string", "format": "uri" },
|
|
84
|
+
"featured_image": { "type": "string", "format": "uri" },
|
|
85
|
+
"categories_html": { "type": "string" },
|
|
86
|
+
"tags_html": { "type": "string" },
|
|
87
|
+
"comments_html": { "type": "string" },
|
|
88
|
+
"status": {
|
|
89
|
+
"type": "string",
|
|
90
|
+
"enum": ["published", "draft"]
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
"previewPage": {
|
|
95
|
+
"type": "object",
|
|
96
|
+
"additionalProperties": false,
|
|
97
|
+
"required": ["id", "title", "slug", "html", "status"],
|
|
98
|
+
"properties": {
|
|
99
|
+
"id": { "type": "string", "minLength": 1 },
|
|
100
|
+
"title": { "type": "string", "minLength": 1 },
|
|
101
|
+
"slug": { "type": "string", "minLength": 1 },
|
|
102
|
+
"html": { "type": "string" },
|
|
103
|
+
"status": {
|
|
104
|
+
"type": "string",
|
|
105
|
+
"enum": ["published", "draft"]
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
"previewCategory": {
|
|
110
|
+
"type": "object",
|
|
111
|
+
"additionalProperties": false,
|
|
112
|
+
"required": ["id", "name", "slug", "postCount"],
|
|
113
|
+
"properties": {
|
|
114
|
+
"id": { "type": "string", "minLength": 1 },
|
|
115
|
+
"name": { "type": "string", "minLength": 1 },
|
|
116
|
+
"slug": { "type": "string", "minLength": 1 },
|
|
117
|
+
"description": { "type": "string" },
|
|
118
|
+
"postCount": { "type": "integer", "minimum": 0 }
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
"previewTag": {
|
|
122
|
+
"type": "object",
|
|
123
|
+
"additionalProperties": false,
|
|
124
|
+
"required": ["id", "name", "slug", "postCount"],
|
|
125
|
+
"properties": {
|
|
126
|
+
"id": { "type": "string", "minLength": 1 },
|
|
127
|
+
"name": { "type": "string", "minLength": 1 },
|
|
128
|
+
"slug": { "type": "string", "minLength": 1 },
|
|
129
|
+
"postCount": { "type": "integer", "minimum": 0 }
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
"content": {
|
|
133
|
+
"type": "object",
|
|
134
|
+
"additionalProperties": false,
|
|
135
|
+
"required": ["posts", "pages", "categories", "tags"],
|
|
136
|
+
"properties": {
|
|
137
|
+
"posts": {
|
|
138
|
+
"type": "array",
|
|
139
|
+
"items": { "$ref": "#/$defs/previewPost" }
|
|
140
|
+
},
|
|
141
|
+
"pages": {
|
|
142
|
+
"type": "array",
|
|
143
|
+
"items": { "$ref": "#/$defs/previewPage" }
|
|
144
|
+
},
|
|
145
|
+
"categories": {
|
|
146
|
+
"type": "array",
|
|
147
|
+
"items": { "$ref": "#/$defs/previewCategory" }
|
|
148
|
+
},
|
|
149
|
+
"tags": {
|
|
150
|
+
"type": "array",
|
|
151
|
+
"items": { "$ref": "#/$defs/previewTag" }
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
"routeBlocks": {
|
|
156
|
+
"type": "object",
|
|
157
|
+
"additionalProperties": false,
|
|
158
|
+
"required": ["posts", "pagination"],
|
|
159
|
+
"properties": {
|
|
160
|
+
"posts": { "type": "string" },
|
|
161
|
+
"pagination": { "type": "string" },
|
|
162
|
+
"categories": { "type": "string" },
|
|
163
|
+
"tags": { "type": "string" }
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
"categoryRoute": {
|
|
167
|
+
"type": "object",
|
|
168
|
+
"additionalProperties": false,
|
|
169
|
+
"required": ["slug", "posts", "pagination"],
|
|
170
|
+
"properties": {
|
|
171
|
+
"slug": { "type": "string", "minLength": 1 },
|
|
172
|
+
"posts": { "type": "string" },
|
|
173
|
+
"pagination": { "type": "string" },
|
|
174
|
+
"categories": { "type": "string" }
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
"tagRoute": {
|
|
178
|
+
"type": "object",
|
|
179
|
+
"additionalProperties": false,
|
|
180
|
+
"required": ["slug", "posts", "pagination"],
|
|
181
|
+
"properties": {
|
|
182
|
+
"slug": { "type": "string", "minLength": 1 },
|
|
183
|
+
"posts": { "type": "string" },
|
|
184
|
+
"pagination": { "type": "string" },
|
|
185
|
+
"tags": { "type": "string" }
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
"routes": {
|
|
189
|
+
"type": "object",
|
|
190
|
+
"additionalProperties": false,
|
|
191
|
+
"required": ["index", "archive", "categories", "tags"],
|
|
192
|
+
"properties": {
|
|
193
|
+
"index": {
|
|
194
|
+
"allOf": [
|
|
195
|
+
{ "$ref": "#/$defs/routeBlocks" },
|
|
196
|
+
{
|
|
197
|
+
"required": ["posts", "categories", "tags", "pagination"]
|
|
198
|
+
}
|
|
199
|
+
]
|
|
200
|
+
},
|
|
201
|
+
"archive": {
|
|
202
|
+
"$ref": "#/$defs/routeBlocks"
|
|
203
|
+
},
|
|
204
|
+
"categories": {
|
|
205
|
+
"type": "array",
|
|
206
|
+
"items": { "$ref": "#/$defs/categoryRoute" }
|
|
207
|
+
},
|
|
208
|
+
"tags": {
|
|
209
|
+
"type": "array",
|
|
210
|
+
"items": { "$ref": "#/$defs/tagRoute" }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|