Skip to content

Commit c141e3b

Browse files
🔧 enable ajv strict mode and fix view schema type declaration (#366)
- Add `strict: true` to Ajv options to surface schema issues earlier - Add `allowUnionTypes: true` to relax the strict mode rule disallowing union types, since the schemas use them intentionally - Wrap `ajv.validate()` in a try/catch to report schema-level errors (e.g. unknown keywords) without crashing, since strict mode can throw instead of returning false - Add `"type": "object"` to the `view` property in view-schema.json, which strict mode now requires
1 parent 5ddfd3b commit c141e3b

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

schemas/rum/view-schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"readOnly": true
2828
},
2929
"view": {
30+
"type": "object",
3031
"required": ["time_spent", "action", "error", "resource"],
3132
"properties": {
3233
"time_spent": { "type": "integer" },

scripts/validate.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,26 @@ function validateSchemasIds() {
9090

9191
function validateSamples() {
9292
const ajv = new Ajv({
93+
strict: true,
9394
// By default, ajv objects to heterogeneous tuples; the reasoning is that
9495
// they are awkward to work with in some languages. Disable this warning
9596
// since we're using this feature extensively and are aware of the tradeoffs.
9697
strictTuples: false,
98+
allowUnionTypes: true,
9799
})
98100
forEachFile(SCHEMAS_DIRECTORY, (schemaPath) => ajv.addSchema(readJson(schemaPath)))
99101
forEachFile(SAMPLES_DIRECTORY, (samplePath) => {
100102
const schemaId = computeSchemaIdFromSamplePath(samplePath)
101-
const valid = ajv.validate(schemaId, readJson(samplePath))
103+
let valid
104+
try {
105+
valid = ajv.validate(schemaId, readJson(samplePath))
106+
} catch (error) {
107+
console.log(`❌ ${samplePath} had a validation error against ${schemaId}:`)
108+
console.log(` - ${error.message}`)
109+
process.exitCode = 1
110+
return
111+
}
112+
102113
if (valid) {
103114
console.log(`✅ ${samplePath}`)
104115
} else {

0 commit comments

Comments
 (0)