-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvalidate.mjs
More file actions
190 lines (166 loc) · 6.27 KB
/
Copy pathvalidate.mjs
File metadata and controls
190 lines (166 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import path from 'path'
import Ajv from 'ajv'
import { readdirSync, readFileSync } from 'fs'
const SAMPLES_DIRECTORY = './samples'
const SCHEMAS_DIRECTORY = './schemas'
validateSchemasObjectsPropertiesCase()
validateSchemasIds()
validateSamples()
validateRequiredProperties()
if (process.exitCode !== 0 && process.exitCode !== undefined) {
console.log('❌ Some validation errors were found')
}
function validateSchemasObjectsPropertiesCase() {
// Some properties don't follow the convention. Ideally they should be fixed in the future.
const CASING_EXCEPTIONS = new Map([
[
`${SCHEMAS_DIRECTORY}/session-replay/common/_common-segment-metadata-schema.json`,
['records_count', 'index_in_view', 'has_full_snapshot'],
],
[`${SCHEMAS_DIRECTORY}/session-replay/browser/segment-metadata-schema.json`, ['creation_reason']],
[`${SCHEMAS_DIRECTORY}/session-replay/common/focus-record-schema.json`, ['has_focus']],
[`${SCHEMAS_DIRECTORY}/rum/resource-schema.json`, ['operationType', 'operationName']],
[`${SCHEMAS_DIRECTORY}/profiling/_common-schema.json`, ['long_task', 'tags_profiler']],
[`${SCHEMAS_DIRECTORY}/profiling/browser/profile-event-schema.json`, ['_dd', 'clock_drift']],
])
let displayConvention = false
forEachFile(SCHEMAS_DIRECTORY, (schemaPath) => {
const schema = readJson(schemaPath)
// RUM and telemetry schemas object properties should be snake_case, other schemas objects should
// be camelCase
const shouldBeSnakeCase =
schemaPath.startsWith(`${SCHEMAS_DIRECTORY}/rum/`) || schemaPath.startsWith(`${SCHEMAS_DIRECTORY}/telemetry/`)
const caseExceptions = CASING_EXCEPTIONS.get(schemaPath) || []
forEachObjectProperty(schema, (key) => {
const isCorrectCase = shouldBeSnakeCase ? isSnakeCase(key) : isCamelCase(key)
if (!isCorrectCase && !caseExceptions.includes(key)) {
console.log(`❌ Schema ${schemaPath} property ${key} is not ${shouldBeSnakeCase ? 'snake_case' : 'camelCase'}`)
displayConvention = true
process.exitCode = 1
}
})
})
if (displayConvention) {
console.log(
'ℹ️ RUM and telemetry schemas object properties should be snake_case, other schemas objects should be camelCase'
)
}
}
function validateRequiredProperties() {
forEachFile(SCHEMAS_DIRECTORY, (schemaPath) => {
forEachObject(readJson(schemaPath), (schema) => {
if (schema.required) {
for (const requiredPropertyName of schema.required) {
if (!schema.properties?.[requiredPropertyName]) {
console.log(`❌ Schema ${schemaPath} is missing required property ${requiredPropertyName}`)
process.exitCode = 1
}
}
}
})
})
}
function validateSchemasIds() {
forEachFile(SCHEMAS_DIRECTORY, (schemaPath) => {
const schema = readJson(schemaPath)
// We need to be careful about schema ids because they need to:
// * be unique, or else Ajv will throw
// * represent a path, as Ajv will use it to resolve $refs
// Here, we make sure that both requirements are respected.
const schemaId = computeSchemaIdFromSchemaPath(schemaPath)
if (schema.$id !== schemaId) {
console.log(`❌ Schema ${schemaPath} $id should be ${schemaId}`)
process.exitCode = 1
}
})
}
function validateSamples() {
const ajv = new Ajv({
strict: true,
// By default, ajv objects to heterogeneous tuples; the reasoning is that
// they are awkward to work with in some languages. Disable this warning
// since we're using this feature extensively and are aware of the tradeoffs.
strictTuples: false,
allowUnionTypes: true,
})
forEachFile(SCHEMAS_DIRECTORY, (schemaPath) => ajv.addSchema(readJson(schemaPath)))
forEachFile(SAMPLES_DIRECTORY, (samplePath) => {
const schemaId = computeSchemaIdFromSamplePath(samplePath)
let valid
try {
valid = ajv.validate(schemaId, readJson(samplePath))
} catch (error) {
console.log(`❌ ${samplePath} had a validation error against ${schemaId}:`)
console.log(` - ${error.message}`)
process.exitCode = 1
return
}
if (valid) {
console.log(`✅ ${samplePath}`)
} else {
console.log(`❌ ${samplePath} is not valid against ${schemaId}:`)
console.log(` - ${ajv.errorsText(undefined, { separator: '\n - ' })}`)
process.exitCode = 1
}
})
}
function computeSchemaIdFromSchemaPath(schemaPath) {
// Strip the schema directory from the provided path:
// "./schemas/session-replay/mobile/record-schema.json" -> "session-replay/mobile/record-schema.json"
return schemaPath.slice(SCHEMAS_DIRECTORY.length + 1)
}
function computeSchemaIdFromSamplePath(samplePath) {
// Keep only the directory path and strip the sample directory from the provided path:
// "./samples/session-replay/mobile/record/full-snapshot-record.json" -> "session-replay/mobile/record-schema.json"
return `${path.dirname(samplePath).slice(SAMPLES_DIRECTORY.length + 1)}-schema.json`
}
function forEachFile(directoryPath, callback) {
for (const entry of readdirSync(directoryPath, { withFileTypes: true })) {
const entryPath = `${directoryPath}/${entry.name}`
if (entry.isFile()) {
callback(entryPath)
} else {
forEachFile(entryPath, callback)
}
}
}
function readJson(filePath) {
return JSON.parse(readFileSync(filePath, 'utf8'))
}
/**
* Iterates over each properties of objects specified in the provided JSON schema.
*/
function forEachObjectProperty(schema, callback) {
forEachObject(schema, (schema) => {
if (schema.properties) {
for (const [key, value] of Object.entries(schema.properties)) {
callback(key, value)
}
}
})
}
/**
* Iterates over each objects specified in the provided JSON schema.
*/
function forEachObject(schema, callback) {
if (Array.isArray(schema)) {
// traverse arrays
for (const value of schema) {
forEachObject(value, callback)
}
} else if (typeof schema === 'object' && schema !== null) {
// traverse objects
for (const value of Object.values(schema)) {
forEachObject(value, callback)
}
if (schema.type === 'object' || schema.properties) {
callback(schema)
}
}
}
function isSnakeCase(str) {
return /^[a-z0-9_]+$/.test(str)
}
function isCamelCase(str) {
return /^[a-z0-9][A-Za-z0-9]*$/.test(str)
}