@schemasentry/core 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Schema Sentry
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/dist/index.cjs ADDED
@@ -0,0 +1,358 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Article: () => Article,
24
+ BlogPosting: () => BlogPosting,
25
+ BreadcrumbList: () => BreadcrumbList,
26
+ FAQPage: () => FAQPage,
27
+ HowTo: () => HowTo,
28
+ Location: () => Location,
29
+ Organization: () => Organization,
30
+ Person: () => Person,
31
+ Product: () => Product,
32
+ SCHEMA_CONTEXT: () => SCHEMA_CONTEXT,
33
+ WebPage: () => WebPage,
34
+ WebSite: () => WebSite,
35
+ stableStringify: () => stableStringify,
36
+ validateSchema: () => validateSchema
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+ var SCHEMA_CONTEXT = "https://schema.org";
40
+ var withBase = (type, input) => {
41
+ const base = {
42
+ "@context": SCHEMA_CONTEXT,
43
+ "@type": type
44
+ };
45
+ if (input.id) {
46
+ base["@id"] = input.id;
47
+ }
48
+ return {
49
+ ...base,
50
+ ...input
51
+ };
52
+ };
53
+ var Organization = (input) => withBase("Organization", input);
54
+ var Person = (input) => withBase("Person", input);
55
+ var Location = (input) => withBase("Place", input);
56
+ var WebSite = (input) => withBase("WebSite", input);
57
+ var WebPage = (input) => withBase("WebPage", input);
58
+ var Article = (input) => {
59
+ const { authorName, ...rest } = input;
60
+ return withBase("Article", {
61
+ ...rest,
62
+ author: {
63
+ "@type": "Person",
64
+ name: authorName
65
+ }
66
+ });
67
+ };
68
+ var BlogPosting = (input) => {
69
+ const { authorName, ...rest } = input;
70
+ return withBase("BlogPosting", {
71
+ ...rest,
72
+ author: {
73
+ "@type": "Person",
74
+ name: authorName
75
+ }
76
+ });
77
+ };
78
+ var Product = (input) => {
79
+ const { brandName, ...rest } = input;
80
+ return withBase("Product", {
81
+ ...rest,
82
+ ...brandName ? {
83
+ brand: {
84
+ "@type": "Brand",
85
+ name: brandName
86
+ }
87
+ } : {}
88
+ });
89
+ };
90
+ var FAQPage = (input) => withBase("FAQPage", {
91
+ mainEntity: input.questions.map((item) => ({
92
+ "@type": "Question",
93
+ name: item.question,
94
+ acceptedAnswer: {
95
+ "@type": "Answer",
96
+ text: item.answer
97
+ }
98
+ }))
99
+ });
100
+ var HowTo = (input) => withBase("HowTo", {
101
+ name: input.name,
102
+ step: input.steps.map((step) => ({
103
+ "@type": "HowToStep",
104
+ ...step.name ? { name: step.name } : {},
105
+ text: step.text
106
+ }))
107
+ });
108
+ var BreadcrumbList = (input) => withBase("BreadcrumbList", {
109
+ itemListElement: input.items.map((item, index) => ({
110
+ "@type": "ListItem",
111
+ position: index + 1,
112
+ name: item.name,
113
+ item: item.useItemObject ? { "@id": item.url, name: item.name } : item.url
114
+ }))
115
+ });
116
+ var validateSchema = (nodes) => {
117
+ const issues = [];
118
+ if (!nodes.length) {
119
+ issues.push({
120
+ path: "root",
121
+ message: "No schema blocks provided",
122
+ severity: "error",
123
+ ruleId: "schema.empty"
124
+ });
125
+ }
126
+ nodes.forEach((node, index) => {
127
+ const pathPrefix = `nodes[${index}]`;
128
+ if (node["@context"] !== SCHEMA_CONTEXT) {
129
+ issues.push({
130
+ path: `${pathPrefix}.@context`,
131
+ message: "Invalid or missing @context",
132
+ severity: "error",
133
+ ruleId: "schema.context"
134
+ });
135
+ }
136
+ const type = node["@type"];
137
+ if (!isSchemaType(type)) {
138
+ issues.push({
139
+ path: `${pathPrefix}.@type`,
140
+ message: "Unknown or missing @type",
141
+ severity: "error",
142
+ ruleId: "schema.type"
143
+ });
144
+ return;
145
+ }
146
+ const requiredFields = REQUIRED_FIELDS[type] ?? [];
147
+ for (const field of requiredFields) {
148
+ if (field === "author") {
149
+ if (!hasAuthor(node)) {
150
+ issues.push({
151
+ path: `${pathPrefix}.author`,
152
+ message: "Missing required field 'author'",
153
+ severity: "error",
154
+ ruleId: "schema.required.author"
155
+ });
156
+ }
157
+ continue;
158
+ }
159
+ if (field === "mainEntity") {
160
+ const value2 = node[field];
161
+ const ok = Array.isArray(value2) && value2.length > 0;
162
+ if (!ok) {
163
+ issues.push({
164
+ path: `${pathPrefix}.mainEntity`,
165
+ message: "FAQPage must include at least one Question",
166
+ severity: "error",
167
+ ruleId: "schema.required.mainEntity"
168
+ });
169
+ }
170
+ continue;
171
+ }
172
+ if (field === "step") {
173
+ const value2 = node[field];
174
+ const ok = Array.isArray(value2) && value2.length > 0;
175
+ if (!ok) {
176
+ issues.push({
177
+ path: `${pathPrefix}.step`,
178
+ message: "HowTo must include at least one step",
179
+ severity: "error",
180
+ ruleId: "schema.required.step"
181
+ });
182
+ }
183
+ continue;
184
+ }
185
+ if (field === "itemListElement") {
186
+ const value2 = node[field];
187
+ const ok = Array.isArray(value2) && value2.length > 0;
188
+ if (!ok) {
189
+ issues.push({
190
+ path: `${pathPrefix}.itemListElement`,
191
+ message: "BreadcrumbList must include at least one breadcrumb item",
192
+ severity: "error",
193
+ ruleId: "schema.required.itemListElement"
194
+ });
195
+ }
196
+ continue;
197
+ }
198
+ const value = node[field];
199
+ if (isEmpty(value)) {
200
+ issues.push({
201
+ path: `${pathPrefix}.${field}`,
202
+ message: `Missing required field '${field}'`,
203
+ severity: "error",
204
+ ruleId: `schema.required.${field}`
205
+ });
206
+ }
207
+ }
208
+ if (type === "BreadcrumbList") {
209
+ validateBreadcrumbList(node, pathPrefix, issues);
210
+ }
211
+ });
212
+ const errorCount = issues.filter((issue) => issue.severity === "error").length;
213
+ const warnCount = issues.filter((issue) => issue.severity === "warn").length;
214
+ const score = Math.max(0, 100 - errorCount * 10 - warnCount * 2);
215
+ return {
216
+ ok: errorCount === 0,
217
+ score,
218
+ issues
219
+ };
220
+ };
221
+ var REQUIRED_FIELDS = {
222
+ Organization: ["name"],
223
+ Person: ["name"],
224
+ Place: ["name"],
225
+ WebSite: ["name", "url"],
226
+ WebPage: ["name", "url"],
227
+ Article: ["headline", "author", "datePublished", "url"],
228
+ BlogPosting: ["headline", "author", "datePublished", "url"],
229
+ Product: ["name", "description", "url"],
230
+ FAQPage: ["mainEntity"],
231
+ HowTo: ["name", "step"],
232
+ BreadcrumbList: ["itemListElement"]
233
+ };
234
+ var isSchemaType = (value) => typeof value === "string" && value in REQUIRED_FIELDS;
235
+ var isEmpty = (value) => {
236
+ if (value === null || value === void 0) {
237
+ return true;
238
+ }
239
+ if (typeof value === "string") {
240
+ return value.trim().length === 0;
241
+ }
242
+ if (Array.isArray(value)) {
243
+ return value.length === 0;
244
+ }
245
+ return false;
246
+ };
247
+ var hasAuthor = (node) => {
248
+ const author = node.author;
249
+ if (!author) {
250
+ return false;
251
+ }
252
+ if (typeof author === "string") {
253
+ return author.trim().length > 0;
254
+ }
255
+ if (typeof author === "object") {
256
+ const name = author.name;
257
+ return typeof name === "string" && name.trim().length > 0;
258
+ }
259
+ return false;
260
+ };
261
+ var validateBreadcrumbList = (node, pathPrefix, issues) => {
262
+ const itemListElement = node.itemListElement;
263
+ if (!Array.isArray(itemListElement)) {
264
+ return;
265
+ }
266
+ itemListElement.forEach((entry, index) => {
267
+ if (!entry || typeof entry !== "object") {
268
+ issues.push({
269
+ path: `${pathPrefix}.itemListElement[${index}]`,
270
+ message: "BreadcrumbList items must be objects",
271
+ severity: "error",
272
+ ruleId: "schema.breadcrumb.item"
273
+ });
274
+ return;
275
+ }
276
+ const listItem = entry;
277
+ const position = listItem.position;
278
+ if (typeof position !== "number" || !Number.isFinite(position) || position <= 0) {
279
+ issues.push({
280
+ path: `${pathPrefix}.itemListElement[${index}].position`,
281
+ message: "ListItem.position must be a positive number",
282
+ severity: "error",
283
+ ruleId: "schema.breadcrumb.position"
284
+ });
285
+ }
286
+ const item = listItem.item;
287
+ if (typeof item === "string") {
288
+ if (!isValidUrl(item)) {
289
+ issues.push({
290
+ path: `${pathPrefix}.itemListElement[${index}].item`,
291
+ message: "ListItem.item must be a valid URL",
292
+ severity: "error",
293
+ ruleId: "schema.breadcrumb.item.url"
294
+ });
295
+ }
296
+ return;
297
+ }
298
+ if (item && typeof item === "object") {
299
+ const itemObject = item;
300
+ const id = itemObject["@id"] ?? itemObject.url;
301
+ if (typeof id !== "string" || !isValidUrl(id)) {
302
+ issues.push({
303
+ path: `${pathPrefix}.itemListElement[${index}].item`,
304
+ message: "ListItem.item must include a valid @id or url",
305
+ severity: "error",
306
+ ruleId: "schema.breadcrumb.item.url"
307
+ });
308
+ }
309
+ return;
310
+ }
311
+ issues.push({
312
+ path: `${pathPrefix}.itemListElement[${index}].item`,
313
+ message: "ListItem.item is required",
314
+ severity: "error",
315
+ ruleId: "schema.breadcrumb.item"
316
+ });
317
+ });
318
+ };
319
+ var isValidUrl = (value) => {
320
+ try {
321
+ new URL(value);
322
+ return true;
323
+ } catch {
324
+ return false;
325
+ }
326
+ };
327
+ var sortKeys = (value) => {
328
+ if (Array.isArray(value)) {
329
+ return value.map(sortKeys);
330
+ }
331
+ if (value && typeof value === "object") {
332
+ const obj = value;
333
+ const sorted = {};
334
+ for (const key of Object.keys(obj).sort()) {
335
+ sorted[key] = sortKeys(obj[key]);
336
+ }
337
+ return sorted;
338
+ }
339
+ return value;
340
+ };
341
+ var stableStringify = (value) => JSON.stringify(sortKeys(value));
342
+ // Annotate the CommonJS export names for ESM import in node:
343
+ 0 && (module.exports = {
344
+ Article,
345
+ BlogPosting,
346
+ BreadcrumbList,
347
+ FAQPage,
348
+ HowTo,
349
+ Location,
350
+ Organization,
351
+ Person,
352
+ Product,
353
+ SCHEMA_CONTEXT,
354
+ WebPage,
355
+ WebSite,
356
+ stableStringify,
357
+ validateSchema
358
+ });
@@ -0,0 +1,114 @@
1
+ declare const SCHEMA_CONTEXT: "https://schema.org";
2
+ type JsonLdValue = string | number | boolean | null | JsonLdObject | JsonLdValue[];
3
+ type JsonLdObject = {
4
+ [key: string]: JsonLdValue;
5
+ };
6
+ type SchemaTypeName = "Organization" | "Person" | "Place" | "WebSite" | "WebPage" | "Article" | "BlogPosting" | "Product" | "FAQPage" | "HowTo" | "BreadcrumbList";
7
+ type SchemaNode = JsonLdObject & {
8
+ "@context": typeof SCHEMA_CONTEXT;
9
+ "@type": SchemaTypeName;
10
+ "@id"?: string;
11
+ };
12
+ type BaseInput = {
13
+ id?: string;
14
+ };
15
+ type OrganizationInput = BaseInput & {
16
+ name: string;
17
+ url?: string;
18
+ logo?: string;
19
+ sameAs?: string[];
20
+ description?: string;
21
+ };
22
+ declare const Organization: (input: OrganizationInput) => SchemaNode;
23
+ type PersonInput = BaseInput & {
24
+ name: string;
25
+ url?: string;
26
+ sameAs?: string[];
27
+ jobTitle?: string;
28
+ };
29
+ declare const Person: (input: PersonInput) => SchemaNode;
30
+ type LocationInput = BaseInput & {
31
+ name: string;
32
+ address?: string;
33
+ url?: string;
34
+ };
35
+ declare const Location: (input: LocationInput) => SchemaNode;
36
+ type WebSiteInput = BaseInput & {
37
+ name: string;
38
+ url: string;
39
+ description?: string;
40
+ };
41
+ declare const WebSite: (input: WebSiteInput) => SchemaNode;
42
+ type WebPageInput = BaseInput & {
43
+ name: string;
44
+ url: string;
45
+ description?: string;
46
+ isPartOf?: SchemaNode;
47
+ };
48
+ declare const WebPage: (input: WebPageInput) => SchemaNode;
49
+ type ArticleInput = BaseInput & {
50
+ headline: string;
51
+ authorName: string;
52
+ datePublished: string;
53
+ url: string;
54
+ dateModified?: string;
55
+ description?: string;
56
+ image?: string;
57
+ };
58
+ declare const Article: (input: ArticleInput) => SchemaNode;
59
+ type BlogPostingInput = ArticleInput;
60
+ declare const BlogPosting: (input: BlogPostingInput) => SchemaNode;
61
+ type ProductInput = BaseInput & {
62
+ name: string;
63
+ description: string;
64
+ url: string;
65
+ image?: string;
66
+ brandName?: string;
67
+ sku?: string;
68
+ };
69
+ declare const Product: (input: ProductInput) => SchemaNode;
70
+ type FAQItem = {
71
+ question: string;
72
+ answer: string;
73
+ };
74
+ type FAQPageInput = BaseInput & {
75
+ questions: FAQItem[];
76
+ };
77
+ declare const FAQPage: (input: FAQPageInput) => SchemaNode;
78
+ type HowToStep = {
79
+ name?: string;
80
+ text: string;
81
+ };
82
+ type HowToInput = BaseInput & {
83
+ name: string;
84
+ steps: HowToStep[];
85
+ };
86
+ declare const HowTo: (input: HowToInput) => SchemaNode;
87
+ type BreadcrumbItem = {
88
+ name: string;
89
+ url: string;
90
+ useItemObject?: boolean;
91
+ };
92
+ type BreadcrumbListInput = BaseInput & {
93
+ items: BreadcrumbItem[];
94
+ };
95
+ declare const BreadcrumbList: (input: BreadcrumbListInput) => SchemaNode;
96
+ type Manifest = {
97
+ routes: Record<string, SchemaTypeName[]>;
98
+ };
99
+ type ValidationSeverity = "error" | "warn";
100
+ type ValidationIssue = {
101
+ path: string;
102
+ message: string;
103
+ severity: ValidationSeverity;
104
+ ruleId: string;
105
+ };
106
+ type ValidationResult = {
107
+ ok: boolean;
108
+ score: number;
109
+ issues: ValidationIssue[];
110
+ };
111
+ declare const validateSchema: (nodes: SchemaNode[]) => ValidationResult;
112
+ declare const stableStringify: (value: JsonLdValue) => string;
113
+
114
+ export { Article, type ArticleInput, BlogPosting, type BlogPostingInput, type BreadcrumbItem, BreadcrumbList, type BreadcrumbListInput, type FAQItem, FAQPage, type FAQPageInput, HowTo, type HowToInput, type HowToStep, type JsonLdObject, type JsonLdValue, Location, type LocationInput, type Manifest, Organization, type OrganizationInput, Person, type PersonInput, Product, type ProductInput, SCHEMA_CONTEXT, type SchemaNode, type SchemaTypeName, type ValidationIssue, type ValidationResult, type ValidationSeverity, WebPage, type WebPageInput, WebSite, type WebSiteInput, stableStringify, validateSchema };
@@ -0,0 +1,114 @@
1
+ declare const SCHEMA_CONTEXT: "https://schema.org";
2
+ type JsonLdValue = string | number | boolean | null | JsonLdObject | JsonLdValue[];
3
+ type JsonLdObject = {
4
+ [key: string]: JsonLdValue;
5
+ };
6
+ type SchemaTypeName = "Organization" | "Person" | "Place" | "WebSite" | "WebPage" | "Article" | "BlogPosting" | "Product" | "FAQPage" | "HowTo" | "BreadcrumbList";
7
+ type SchemaNode = JsonLdObject & {
8
+ "@context": typeof SCHEMA_CONTEXT;
9
+ "@type": SchemaTypeName;
10
+ "@id"?: string;
11
+ };
12
+ type BaseInput = {
13
+ id?: string;
14
+ };
15
+ type OrganizationInput = BaseInput & {
16
+ name: string;
17
+ url?: string;
18
+ logo?: string;
19
+ sameAs?: string[];
20
+ description?: string;
21
+ };
22
+ declare const Organization: (input: OrganizationInput) => SchemaNode;
23
+ type PersonInput = BaseInput & {
24
+ name: string;
25
+ url?: string;
26
+ sameAs?: string[];
27
+ jobTitle?: string;
28
+ };
29
+ declare const Person: (input: PersonInput) => SchemaNode;
30
+ type LocationInput = BaseInput & {
31
+ name: string;
32
+ address?: string;
33
+ url?: string;
34
+ };
35
+ declare const Location: (input: LocationInput) => SchemaNode;
36
+ type WebSiteInput = BaseInput & {
37
+ name: string;
38
+ url: string;
39
+ description?: string;
40
+ };
41
+ declare const WebSite: (input: WebSiteInput) => SchemaNode;
42
+ type WebPageInput = BaseInput & {
43
+ name: string;
44
+ url: string;
45
+ description?: string;
46
+ isPartOf?: SchemaNode;
47
+ };
48
+ declare const WebPage: (input: WebPageInput) => SchemaNode;
49
+ type ArticleInput = BaseInput & {
50
+ headline: string;
51
+ authorName: string;
52
+ datePublished: string;
53
+ url: string;
54
+ dateModified?: string;
55
+ description?: string;
56
+ image?: string;
57
+ };
58
+ declare const Article: (input: ArticleInput) => SchemaNode;
59
+ type BlogPostingInput = ArticleInput;
60
+ declare const BlogPosting: (input: BlogPostingInput) => SchemaNode;
61
+ type ProductInput = BaseInput & {
62
+ name: string;
63
+ description: string;
64
+ url: string;
65
+ image?: string;
66
+ brandName?: string;
67
+ sku?: string;
68
+ };
69
+ declare const Product: (input: ProductInput) => SchemaNode;
70
+ type FAQItem = {
71
+ question: string;
72
+ answer: string;
73
+ };
74
+ type FAQPageInput = BaseInput & {
75
+ questions: FAQItem[];
76
+ };
77
+ declare const FAQPage: (input: FAQPageInput) => SchemaNode;
78
+ type HowToStep = {
79
+ name?: string;
80
+ text: string;
81
+ };
82
+ type HowToInput = BaseInput & {
83
+ name: string;
84
+ steps: HowToStep[];
85
+ };
86
+ declare const HowTo: (input: HowToInput) => SchemaNode;
87
+ type BreadcrumbItem = {
88
+ name: string;
89
+ url: string;
90
+ useItemObject?: boolean;
91
+ };
92
+ type BreadcrumbListInput = BaseInput & {
93
+ items: BreadcrumbItem[];
94
+ };
95
+ declare const BreadcrumbList: (input: BreadcrumbListInput) => SchemaNode;
96
+ type Manifest = {
97
+ routes: Record<string, SchemaTypeName[]>;
98
+ };
99
+ type ValidationSeverity = "error" | "warn";
100
+ type ValidationIssue = {
101
+ path: string;
102
+ message: string;
103
+ severity: ValidationSeverity;
104
+ ruleId: string;
105
+ };
106
+ type ValidationResult = {
107
+ ok: boolean;
108
+ score: number;
109
+ issues: ValidationIssue[];
110
+ };
111
+ declare const validateSchema: (nodes: SchemaNode[]) => ValidationResult;
112
+ declare const stableStringify: (value: JsonLdValue) => string;
113
+
114
+ export { Article, type ArticleInput, BlogPosting, type BlogPostingInput, type BreadcrumbItem, BreadcrumbList, type BreadcrumbListInput, type FAQItem, FAQPage, type FAQPageInput, HowTo, type HowToInput, type HowToStep, type JsonLdObject, type JsonLdValue, Location, type LocationInput, type Manifest, Organization, type OrganizationInput, Person, type PersonInput, Product, type ProductInput, SCHEMA_CONTEXT, type SchemaNode, type SchemaTypeName, type ValidationIssue, type ValidationResult, type ValidationSeverity, WebPage, type WebPageInput, WebSite, type WebSiteInput, stableStringify, validateSchema };
package/dist/index.js ADDED
@@ -0,0 +1,320 @@
1
+ // src/index.ts
2
+ var SCHEMA_CONTEXT = "https://schema.org";
3
+ var withBase = (type, input) => {
4
+ const base = {
5
+ "@context": SCHEMA_CONTEXT,
6
+ "@type": type
7
+ };
8
+ if (input.id) {
9
+ base["@id"] = input.id;
10
+ }
11
+ return {
12
+ ...base,
13
+ ...input
14
+ };
15
+ };
16
+ var Organization = (input) => withBase("Organization", input);
17
+ var Person = (input) => withBase("Person", input);
18
+ var Location = (input) => withBase("Place", input);
19
+ var WebSite = (input) => withBase("WebSite", input);
20
+ var WebPage = (input) => withBase("WebPage", input);
21
+ var Article = (input) => {
22
+ const { authorName, ...rest } = input;
23
+ return withBase("Article", {
24
+ ...rest,
25
+ author: {
26
+ "@type": "Person",
27
+ name: authorName
28
+ }
29
+ });
30
+ };
31
+ var BlogPosting = (input) => {
32
+ const { authorName, ...rest } = input;
33
+ return withBase("BlogPosting", {
34
+ ...rest,
35
+ author: {
36
+ "@type": "Person",
37
+ name: authorName
38
+ }
39
+ });
40
+ };
41
+ var Product = (input) => {
42
+ const { brandName, ...rest } = input;
43
+ return withBase("Product", {
44
+ ...rest,
45
+ ...brandName ? {
46
+ brand: {
47
+ "@type": "Brand",
48
+ name: brandName
49
+ }
50
+ } : {}
51
+ });
52
+ };
53
+ var FAQPage = (input) => withBase("FAQPage", {
54
+ mainEntity: input.questions.map((item) => ({
55
+ "@type": "Question",
56
+ name: item.question,
57
+ acceptedAnswer: {
58
+ "@type": "Answer",
59
+ text: item.answer
60
+ }
61
+ }))
62
+ });
63
+ var HowTo = (input) => withBase("HowTo", {
64
+ name: input.name,
65
+ step: input.steps.map((step) => ({
66
+ "@type": "HowToStep",
67
+ ...step.name ? { name: step.name } : {},
68
+ text: step.text
69
+ }))
70
+ });
71
+ var BreadcrumbList = (input) => withBase("BreadcrumbList", {
72
+ itemListElement: input.items.map((item, index) => ({
73
+ "@type": "ListItem",
74
+ position: index + 1,
75
+ name: item.name,
76
+ item: item.useItemObject ? { "@id": item.url, name: item.name } : item.url
77
+ }))
78
+ });
79
+ var validateSchema = (nodes) => {
80
+ const issues = [];
81
+ if (!nodes.length) {
82
+ issues.push({
83
+ path: "root",
84
+ message: "No schema blocks provided",
85
+ severity: "error",
86
+ ruleId: "schema.empty"
87
+ });
88
+ }
89
+ nodes.forEach((node, index) => {
90
+ const pathPrefix = `nodes[${index}]`;
91
+ if (node["@context"] !== SCHEMA_CONTEXT) {
92
+ issues.push({
93
+ path: `${pathPrefix}.@context`,
94
+ message: "Invalid or missing @context",
95
+ severity: "error",
96
+ ruleId: "schema.context"
97
+ });
98
+ }
99
+ const type = node["@type"];
100
+ if (!isSchemaType(type)) {
101
+ issues.push({
102
+ path: `${pathPrefix}.@type`,
103
+ message: "Unknown or missing @type",
104
+ severity: "error",
105
+ ruleId: "schema.type"
106
+ });
107
+ return;
108
+ }
109
+ const requiredFields = REQUIRED_FIELDS[type] ?? [];
110
+ for (const field of requiredFields) {
111
+ if (field === "author") {
112
+ if (!hasAuthor(node)) {
113
+ issues.push({
114
+ path: `${pathPrefix}.author`,
115
+ message: "Missing required field 'author'",
116
+ severity: "error",
117
+ ruleId: "schema.required.author"
118
+ });
119
+ }
120
+ continue;
121
+ }
122
+ if (field === "mainEntity") {
123
+ const value2 = node[field];
124
+ const ok = Array.isArray(value2) && value2.length > 0;
125
+ if (!ok) {
126
+ issues.push({
127
+ path: `${pathPrefix}.mainEntity`,
128
+ message: "FAQPage must include at least one Question",
129
+ severity: "error",
130
+ ruleId: "schema.required.mainEntity"
131
+ });
132
+ }
133
+ continue;
134
+ }
135
+ if (field === "step") {
136
+ const value2 = node[field];
137
+ const ok = Array.isArray(value2) && value2.length > 0;
138
+ if (!ok) {
139
+ issues.push({
140
+ path: `${pathPrefix}.step`,
141
+ message: "HowTo must include at least one step",
142
+ severity: "error",
143
+ ruleId: "schema.required.step"
144
+ });
145
+ }
146
+ continue;
147
+ }
148
+ if (field === "itemListElement") {
149
+ const value2 = node[field];
150
+ const ok = Array.isArray(value2) && value2.length > 0;
151
+ if (!ok) {
152
+ issues.push({
153
+ path: `${pathPrefix}.itemListElement`,
154
+ message: "BreadcrumbList must include at least one breadcrumb item",
155
+ severity: "error",
156
+ ruleId: "schema.required.itemListElement"
157
+ });
158
+ }
159
+ continue;
160
+ }
161
+ const value = node[field];
162
+ if (isEmpty(value)) {
163
+ issues.push({
164
+ path: `${pathPrefix}.${field}`,
165
+ message: `Missing required field '${field}'`,
166
+ severity: "error",
167
+ ruleId: `schema.required.${field}`
168
+ });
169
+ }
170
+ }
171
+ if (type === "BreadcrumbList") {
172
+ validateBreadcrumbList(node, pathPrefix, issues);
173
+ }
174
+ });
175
+ const errorCount = issues.filter((issue) => issue.severity === "error").length;
176
+ const warnCount = issues.filter((issue) => issue.severity === "warn").length;
177
+ const score = Math.max(0, 100 - errorCount * 10 - warnCount * 2);
178
+ return {
179
+ ok: errorCount === 0,
180
+ score,
181
+ issues
182
+ };
183
+ };
184
+ var REQUIRED_FIELDS = {
185
+ Organization: ["name"],
186
+ Person: ["name"],
187
+ Place: ["name"],
188
+ WebSite: ["name", "url"],
189
+ WebPage: ["name", "url"],
190
+ Article: ["headline", "author", "datePublished", "url"],
191
+ BlogPosting: ["headline", "author", "datePublished", "url"],
192
+ Product: ["name", "description", "url"],
193
+ FAQPage: ["mainEntity"],
194
+ HowTo: ["name", "step"],
195
+ BreadcrumbList: ["itemListElement"]
196
+ };
197
+ var isSchemaType = (value) => typeof value === "string" && value in REQUIRED_FIELDS;
198
+ var isEmpty = (value) => {
199
+ if (value === null || value === void 0) {
200
+ return true;
201
+ }
202
+ if (typeof value === "string") {
203
+ return value.trim().length === 0;
204
+ }
205
+ if (Array.isArray(value)) {
206
+ return value.length === 0;
207
+ }
208
+ return false;
209
+ };
210
+ var hasAuthor = (node) => {
211
+ const author = node.author;
212
+ if (!author) {
213
+ return false;
214
+ }
215
+ if (typeof author === "string") {
216
+ return author.trim().length > 0;
217
+ }
218
+ if (typeof author === "object") {
219
+ const name = author.name;
220
+ return typeof name === "string" && name.trim().length > 0;
221
+ }
222
+ return false;
223
+ };
224
+ var validateBreadcrumbList = (node, pathPrefix, issues) => {
225
+ const itemListElement = node.itemListElement;
226
+ if (!Array.isArray(itemListElement)) {
227
+ return;
228
+ }
229
+ itemListElement.forEach((entry, index) => {
230
+ if (!entry || typeof entry !== "object") {
231
+ issues.push({
232
+ path: `${pathPrefix}.itemListElement[${index}]`,
233
+ message: "BreadcrumbList items must be objects",
234
+ severity: "error",
235
+ ruleId: "schema.breadcrumb.item"
236
+ });
237
+ return;
238
+ }
239
+ const listItem = entry;
240
+ const position = listItem.position;
241
+ if (typeof position !== "number" || !Number.isFinite(position) || position <= 0) {
242
+ issues.push({
243
+ path: `${pathPrefix}.itemListElement[${index}].position`,
244
+ message: "ListItem.position must be a positive number",
245
+ severity: "error",
246
+ ruleId: "schema.breadcrumb.position"
247
+ });
248
+ }
249
+ const item = listItem.item;
250
+ if (typeof item === "string") {
251
+ if (!isValidUrl(item)) {
252
+ issues.push({
253
+ path: `${pathPrefix}.itemListElement[${index}].item`,
254
+ message: "ListItem.item must be a valid URL",
255
+ severity: "error",
256
+ ruleId: "schema.breadcrumb.item.url"
257
+ });
258
+ }
259
+ return;
260
+ }
261
+ if (item && typeof item === "object") {
262
+ const itemObject = item;
263
+ const id = itemObject["@id"] ?? itemObject.url;
264
+ if (typeof id !== "string" || !isValidUrl(id)) {
265
+ issues.push({
266
+ path: `${pathPrefix}.itemListElement[${index}].item`,
267
+ message: "ListItem.item must include a valid @id or url",
268
+ severity: "error",
269
+ ruleId: "schema.breadcrumb.item.url"
270
+ });
271
+ }
272
+ return;
273
+ }
274
+ issues.push({
275
+ path: `${pathPrefix}.itemListElement[${index}].item`,
276
+ message: "ListItem.item is required",
277
+ severity: "error",
278
+ ruleId: "schema.breadcrumb.item"
279
+ });
280
+ });
281
+ };
282
+ var isValidUrl = (value) => {
283
+ try {
284
+ new URL(value);
285
+ return true;
286
+ } catch {
287
+ return false;
288
+ }
289
+ };
290
+ var sortKeys = (value) => {
291
+ if (Array.isArray(value)) {
292
+ return value.map(sortKeys);
293
+ }
294
+ if (value && typeof value === "object") {
295
+ const obj = value;
296
+ const sorted = {};
297
+ for (const key of Object.keys(obj).sort()) {
298
+ sorted[key] = sortKeys(obj[key]);
299
+ }
300
+ return sorted;
301
+ }
302
+ return value;
303
+ };
304
+ var stableStringify = (value) => JSON.stringify(sortKeys(value));
305
+ export {
306
+ Article,
307
+ BlogPosting,
308
+ BreadcrumbList,
309
+ FAQPage,
310
+ HowTo,
311
+ Location,
312
+ Organization,
313
+ Person,
314
+ Product,
315
+ SCHEMA_CONTEXT,
316
+ WebPage,
317
+ WebSite,
318
+ stableStringify,
319
+ validateSchema
320
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@schemasentry/core",
3
+ "version": "0.1.0",
4
+ "description": "Typed schema builders and validation primitives for Schema Sentry.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.cjs"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/arindamdawn/schema-sentry.git",
24
+ "directory": "packages/core"
25
+ },
26
+ "homepage": "https://github.com/arindamdawn/schema-sentry#readme",
27
+ "bugs": {
28
+ "url": "https://github.com/arindamdawn/schema-sentry/issues"
29
+ },
30
+ "keywords": [
31
+ "schema",
32
+ "json-ld",
33
+ "structured-data",
34
+ "seo",
35
+ "nextjs",
36
+ "typescript"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "scripts": {
42
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean --tsconfig tsconfig.build.json",
43
+ "lint": "echo \"lint not configured\" && exit 0",
44
+ "test": "vitest run"
45
+ }
46
+ }