@gsriram24/structured-data-validator 1.6.1 → 1.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gsriram24/structured-data-validator",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Copyright 2025 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import BaseValidator from './base.js';
13
+
14
+ export default class QAPageValidator extends BaseValidator {
15
+ getConditions() {
16
+ return [
17
+ this.required('mainEntity', 'object'),
18
+ this.required('mainEntity.answerCount', 'number'),
19
+ ].map((c) => c.bind(this));
20
+ }
21
+ }
@@ -15,7 +15,11 @@ export default class QuestionValidator extends BaseValidator {
15
15
  getConditions() {
16
16
  return [
17
17
  this.required('name'),
18
- this.required('acceptedAnswer', 'object'),
18
+ this.or(
19
+ this.required('acceptedAnswer', 'arrayOrObject'),
20
+ this.required('suggestedAnswer', 'arrayOrObject'),
21
+ ),
22
+ this.recommended('text'),
19
23
  ].map((c) => c.bind(this));
20
24
  }
21
25
  }
@@ -189,6 +189,11 @@ export default class SchemaOrgValidator {
189
189
  });
190
190
  }
191
191
 
192
+ async validateType(type) {
193
+ const schema = await this.#loadSchema();
194
+ return !!schema[type];
195
+ }
196
+
192
197
  async validate(data) {
193
198
  const issues = [];
194
199
 
@@ -197,6 +202,22 @@ export default class SchemaOrgValidator {
197
202
  return [];
198
203
  }
199
204
 
205
+ const typeId = this.#stripSchema(this.type);
206
+
207
+ // Check if type exists in schema.org
208
+ const typeExists = await this.validateType(typeId);
209
+ if (!typeExists) {
210
+ issues.push({
211
+ issueMessage: `Type "${typeId}" is not a valid schema.org type`,
212
+ severity: 'WARNING',
213
+ path: this.path,
214
+ errorType: 'schemaOrg',
215
+ fieldName: '@type',
216
+ });
217
+ // Skip property validation since type is invalid
218
+ return issues;
219
+ }
220
+
200
221
  // Get list of properties, any other keys which do not start with @
201
222
  const properties = Object.keys(data).filter(
202
223
  (key) => !key.startsWith('@'),
@@ -206,7 +227,6 @@ export default class SchemaOrgValidator {
206
227
  await Promise.all(
207
228
  properties.map(async (property) => {
208
229
  const propertyId = this.#stripSchema(property);
209
- const typeId = this.#stripSchema(this.type);
210
230
 
211
231
  const isValid = await this.validateProperty(typeId, propertyId);
212
232
  if (!isValid) {
package/src/validator.js CHANGED
@@ -33,6 +33,7 @@ export class Validator {
33
33
  Event: [() => import('./types/Event.js')],
34
34
  FAQPage: [() => import('./types/FAQPage.js')],
35
35
  HowTo: [() => import('./types/HowTo.js')],
36
+ QAPage: [() => import('./types/QAPage.js')],
36
37
  ImageObject: [() => import('./types/ImageObject.js')],
37
38
  VideoObject: [() => import('./types/VideoObject.js')],
38
39
  Clip: [() => import('./types/Clip.js')],
@@ -178,14 +179,17 @@ export class Validator {
178
179
 
179
180
  // Find supported handlers (check direct mapping first, then parent types)
180
181
  const handlers = await this.#getHandlersForType(type);
181
- if (!handlers || handlers.length === 0) {
182
+ if (handlers.length === 0) {
182
183
  this.debug &&
183
184
  console.warn(
184
185
  `${spacing} WARN: No handlers registered for type: ${type}`,
185
186
  );
186
- return [];
187
187
  }
188
+ // Always run global handlers (e.g., schemaOrg) even if no type-specific handler exists
188
189
  handlers.push(...(this.globalHandlers || []));
190
+ if (handlers.length === 0) {
191
+ return [];
192
+ }
189
193
 
190
194
  const handlerPromises = handlers.map(async (handler) => {
191
195
  const handlerClass = (await handler()).default;