@gsriram24/structured-data-validator 1.7.0 → 1.7.2
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 +9 -8
- package/package.json +5 -5
- package/src/types/BreadcrumbList.js +2 -2
- package/src/types/DefinedRegion.js +0 -1
- package/src/types/MerchantReturnPolicy.js +0 -1
- package/src/types/Product.js +0 -2
- package/src/types/ProductMerchant.js +0 -1
- package/src/types/Rating.js +1 -1
- package/src/types/base.js +7 -8
- package/src/types/schemaOrg.js +2 -2
package/README.md
CHANGED
|
@@ -11,8 +11,8 @@ A JavaScript library for validating and parsing structured data according to Sch
|
|
|
11
11
|
|
|
12
12
|
This fork adds the following features on top of Adobe's original library:
|
|
13
13
|
|
|
14
|
-
### 1. `
|
|
15
|
-
Every validation error now includes a `
|
|
14
|
+
### 1. `fieldNames` Property on Validation Errors
|
|
15
|
+
Every validation error now includes a `fieldNames` array for precise programmatic access:
|
|
16
16
|
|
|
17
17
|
```javascript
|
|
18
18
|
// Before (Adobe's version) - requires string parsing
|
|
@@ -27,19 +27,20 @@ Every validation error now includes a `fieldName` property for precise programma
|
|
|
27
27
|
issueMessage: 'Required attribute "price" is missing',
|
|
28
28
|
severity: 'ERROR',
|
|
29
29
|
path: [...],
|
|
30
|
-
|
|
30
|
+
fieldNames: ['price'] // ✨ New!
|
|
31
31
|
}
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
For `or()` conditions
|
|
34
|
+
For `or()` conditions with multiple fields:
|
|
35
35
|
```javascript
|
|
36
36
|
{
|
|
37
37
|
issueMessage: 'One of the following attributes is required...',
|
|
38
|
-
|
|
39
|
-
fieldNames: ['aggregateRating', 'offers', 'review'] // All fields
|
|
38
|
+
fieldNames: ['aggregateRating', 'offers', 'review'] // All relevant fields
|
|
40
39
|
}
|
|
41
40
|
```
|
|
42
41
|
|
|
42
|
+
Access the primary field with `error.fieldNames[0]}`, or iterate over all fields as needed.
|
|
43
|
+
|
|
43
44
|
### 2. New Validators for Common Schema Types
|
|
44
45
|
Added validators for commonly-used schema.org types:
|
|
45
46
|
|
|
@@ -90,10 +91,10 @@ const validator = new Validator(schemaOrgJson);
|
|
|
90
91
|
// Validate the extracted structured data
|
|
91
92
|
const results = await validator.validate(extractedData);
|
|
92
93
|
|
|
93
|
-
// Use
|
|
94
|
+
// Use fieldNames for precise error handling
|
|
94
95
|
results.forEach(issue => {
|
|
95
96
|
if (issue.severity === 'ERROR') {
|
|
96
|
-
console.log(`Field "${issue.
|
|
97
|
+
console.log(`Field "${issue.fieldNames?.[0]}" has error: ${issue.issueMessage}`);
|
|
97
98
|
}
|
|
98
99
|
});
|
|
99
100
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gsriram24/structured-data-validator",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.2",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -34,16 +34,16 @@
|
|
|
34
34
|
"@marbec/web-auto-extractor": "^2.1.0",
|
|
35
35
|
"@semantic-release/changelog": "^6.0.3",
|
|
36
36
|
"@semantic-release/git": "^10.0.1",
|
|
37
|
-
"@semantic-release/github": "^
|
|
38
|
-
"@semantic-release/npm": "^
|
|
37
|
+
"@semantic-release/github": "^12.0.0",
|
|
38
|
+
"@semantic-release/npm": "^13.0.0",
|
|
39
39
|
"c8": "^10.1.3",
|
|
40
|
-
"chai": "^
|
|
40
|
+
"chai": "^6.0.0",
|
|
41
41
|
"eslint": "^9.22.0",
|
|
42
42
|
"eslint-plugin-headers": "^1.2.1",
|
|
43
43
|
"globals": "^16.0.0",
|
|
44
44
|
"mocha": "^11.1.0",
|
|
45
45
|
"prettier": "^3.5.3",
|
|
46
|
-
"semantic-release": "^
|
|
46
|
+
"semantic-release": "^25.0.0",
|
|
47
47
|
"sinon": "^21.0.0"
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -32,7 +32,7 @@ export default class BreadcrumbListValidator extends BaseValidator {
|
|
|
32
32
|
issueMessage: 'At least two ListItems are required',
|
|
33
33
|
severity: 'WARNING',
|
|
34
34
|
path: this.path,
|
|
35
|
-
|
|
35
|
+
fieldNames: ['itemListElement'],
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
38
|
return null;
|
|
@@ -138,7 +138,7 @@ export default class BreadcrumbListValidator extends BaseValidator {
|
|
|
138
138
|
issueMessage: e,
|
|
139
139
|
severity: 'WARNING',
|
|
140
140
|
path: newPath,
|
|
141
|
-
|
|
141
|
+
fieldNames: [urlPath || 'item'],
|
|
142
142
|
});
|
|
143
143
|
}
|
|
144
144
|
}
|
|
@@ -30,7 +30,6 @@ export default class DefinedRegionValidator extends BaseValidator {
|
|
|
30
30
|
issueMessage: 'Only one of addressRegion or postalCode can be used',
|
|
31
31
|
severity: 'WARNING',
|
|
32
32
|
path: this.path,
|
|
33
|
-
fieldName: 'addressRegion',
|
|
34
33
|
fieldNames: ['addressRegion', 'postalCode'],
|
|
35
34
|
};
|
|
36
35
|
}
|
|
@@ -84,7 +84,6 @@ export default class MerchantReturnPolicyValidator extends BaseValidator {
|
|
|
84
84
|
'Either applicableCountry and returnPolicyCategory or merchantReturnLink must be present',
|
|
85
85
|
severity: 'ERROR',
|
|
86
86
|
path: this.path,
|
|
87
|
-
fieldName: 'applicableCountry',
|
|
88
87
|
fieldNames: [
|
|
89
88
|
'applicableCountry',
|
|
90
89
|
'returnPolicyCategory',
|
package/src/types/Product.js
CHANGED
|
@@ -47,7 +47,6 @@ export default class ProductValidator extends BaseValidator {
|
|
|
47
47
|
'At least 2 notes, either positive or negative, are required',
|
|
48
48
|
severity: 'WARNING',
|
|
49
49
|
path: this.path,
|
|
50
|
-
fieldName: 'review',
|
|
51
50
|
fieldNames: ['review.positiveNotes', 'review.negativeNotes'],
|
|
52
51
|
});
|
|
53
52
|
}
|
|
@@ -65,7 +64,6 @@ export default class ProductValidator extends BaseValidator {
|
|
|
65
64
|
'One of the following attributes is required: "aggregateRating", "offers" or "review"',
|
|
66
65
|
severity: 'ERROR',
|
|
67
66
|
path: this.path,
|
|
68
|
-
fieldName: 'aggregateRating',
|
|
69
67
|
fieldNames: ['aggregateRating', 'offers', 'review'],
|
|
70
68
|
});
|
|
71
69
|
}
|
|
@@ -81,7 +81,6 @@ export default class ProductMerchantValidator extends BaseValidator {
|
|
|
81
81
|
issueMessage: `Missing one of field ${gtinFields.map((a) => `"${a}"`).join(', ')} on either product or all offers`,
|
|
82
82
|
severity: 'WARNING',
|
|
83
83
|
path: this.path,
|
|
84
|
-
fieldName: 'gtin',
|
|
85
84
|
fieldNames: gtinFields,
|
|
86
85
|
};
|
|
87
86
|
}
|
package/src/types/Rating.js
CHANGED
package/src/types/base.js
CHANGED
|
@@ -58,7 +58,7 @@ export default class BaseValidator {
|
|
|
58
58
|
issueMessage: `Required attribute "${name}" is missing`,
|
|
59
59
|
severity: 'ERROR',
|
|
60
60
|
path: this.path,
|
|
61
|
-
|
|
61
|
+
fieldNames: [name],
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
64
|
if (type && !this.checkType(value, type, ...opts)) {
|
|
@@ -66,7 +66,7 @@ export default class BaseValidator {
|
|
|
66
66
|
issueMessage: `Invalid type for attribute "${name}"`,
|
|
67
67
|
severity: 'ERROR',
|
|
68
68
|
path: this.path,
|
|
69
|
-
|
|
69
|
+
fieldNames: [name],
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
72
|
return null;
|
|
@@ -94,8 +94,8 @@ export default class BaseValidator {
|
|
|
94
94
|
// Collect all field names from the conditions
|
|
95
95
|
const fieldNames = issues
|
|
96
96
|
.flat()
|
|
97
|
-
.filter((i) => i && i.
|
|
98
|
-
.
|
|
97
|
+
.filter((i) => i && i.fieldNames)
|
|
98
|
+
.flatMap((i) => i.fieldNames);
|
|
99
99
|
|
|
100
100
|
return {
|
|
101
101
|
issueMessage: `One of the following conditions needs to be met: ${issues
|
|
@@ -104,8 +104,7 @@ export default class BaseValidator {
|
|
|
104
104
|
.join(' or ')}`,
|
|
105
105
|
severity,
|
|
106
106
|
path: this.path,
|
|
107
|
-
|
|
108
|
-
fieldNames: fieldNames.length > 0 ? fieldNames : undefined,
|
|
107
|
+
fieldNames: fieldNames.length > 0 ? fieldNames : [],
|
|
109
108
|
};
|
|
110
109
|
};
|
|
111
110
|
}
|
|
@@ -118,7 +117,7 @@ export default class BaseValidator {
|
|
|
118
117
|
issueMessage: `Missing field "${name}" (optional)`,
|
|
119
118
|
severity: 'WARNING',
|
|
120
119
|
path: this.path,
|
|
121
|
-
|
|
120
|
+
fieldNames: [name],
|
|
122
121
|
};
|
|
123
122
|
}
|
|
124
123
|
if (type && !this.checkType(value, type, ...opts)) {
|
|
@@ -126,7 +125,7 @@ export default class BaseValidator {
|
|
|
126
125
|
issueMessage: `Invalid type for attribute "${name}"`,
|
|
127
126
|
severity: 'WARNING',
|
|
128
127
|
path: this.path,
|
|
129
|
-
|
|
128
|
+
fieldNames: [name],
|
|
130
129
|
};
|
|
131
130
|
}
|
|
132
131
|
return null;
|
package/src/types/schemaOrg.js
CHANGED
|
@@ -209,7 +209,7 @@ export default class SchemaOrgValidator {
|
|
|
209
209
|
if (!typeExists) {
|
|
210
210
|
issues.push({
|
|
211
211
|
issueMessage: `Type "${typeId}" is not a valid schema.org type`,
|
|
212
|
-
severity: '
|
|
212
|
+
severity: 'ERROR',
|
|
213
213
|
path: this.path,
|
|
214
214
|
errorType: 'schemaOrg',
|
|
215
215
|
fieldName: '@type',
|
|
@@ -235,7 +235,7 @@ export default class SchemaOrgValidator {
|
|
|
235
235
|
severity: 'WARNING',
|
|
236
236
|
path: this.path,
|
|
237
237
|
errorType: 'schemaOrg',
|
|
238
|
-
|
|
238
|
+
fieldNames: [propertyId],
|
|
239
239
|
});
|
|
240
240
|
}
|
|
241
241
|
}),
|