forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: data validation using schema (renovatebot#30797)
Co-authored-by: Michael Kriese <[email protected]>
- Loading branch information
1 parent
fbe4dba
commit d1af677
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import fs from 'fs-extra'; | ||
import upath from 'upath'; | ||
import { Json } from '../lib/util/schema-utils'; | ||
import { capitalize } from '../tools/docs/utils'; | ||
import * as Schemas from '../tools/schemas/schema'; | ||
|
||
describe('validate-schemas', () => { | ||
it('validate json files in lib/data against their schemas', async () => { | ||
const dataFileDir = 'lib/data'; | ||
const schemaDir = 'tools/schemas'; | ||
const schemasAndJsonFiles: { | ||
schemaName: keyof typeof Schemas; | ||
dataFileName: string; | ||
}[] = []; | ||
|
||
const schemaFiles = (await fs.readdir(schemaDir)).filter( | ||
(file) => upath.extname(file) === '.json', | ||
); | ||
|
||
for (const schemaFile of schemaFiles) { | ||
const correspondingDatFileName = schemaFile.replace('-schema', ''); | ||
const schemaName = `${schemaFile | ||
.replace('.json', '') | ||
.split('-') | ||
.map(capitalize) | ||
.join('')}` as keyof typeof Schemas; | ||
schemasAndJsonFiles.push({ | ||
schemaName, | ||
dataFileName: correspondingDatFileName, | ||
}); | ||
} | ||
|
||
const settledPromises = await Promise.allSettled( | ||
schemasAndJsonFiles.map(async ({ schemaName, dataFileName }) => { | ||
const data = Json.parse( | ||
await fs.readFile(upath.join(dataFileDir, dataFileName), 'utf8'), | ||
); | ||
|
||
// validate json data against schema: using parse here instead of safeParse so we throw | ||
// this leads to a better error message when the assertion fails | ||
// eslint-disable-next-line import/namespace | ||
Schemas[schemaName].parse(data); | ||
}), | ||
); | ||
|
||
for (let i = 0; i < settledPromises.length; i++) { | ||
const { schemaName, dataFileName } = schemasAndJsonFiles[i]; | ||
const res = { | ||
schemaName, | ||
dataFileName, | ||
settledPromise: { reason: undefined, ...settledPromises[i] }, | ||
}; | ||
|
||
expect(res).toMatchObject({ | ||
schemaName, | ||
dataFileName, | ||
settledPromise: { | ||
status: 'fulfilled', | ||
reason: undefined, | ||
}, | ||
}); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"repoGroups": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9. -]+$": { | ||
"oneOf": [ | ||
{ "type": "string", "format": "uri" }, | ||
{ | ||
"type": "array", | ||
"items": { "type": "string", "format": "uri" } | ||
} | ||
] | ||
} | ||
}, | ||
"additionalProperties": false | ||
}, | ||
"orgGroups": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9. -]+$": { | ||
"oneOf": [ | ||
{ "type": "string", "format": "uri" }, | ||
{ | ||
"type": "array", | ||
"items": { "type": "string", "format": "uri" } | ||
} | ||
] | ||
} | ||
}, | ||
"additionalProperties": false | ||
}, | ||
"patternGroups": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9. -]+$": { | ||
"oneOf": [ | ||
{ "type": "string", "pattern": "^/.+/$" }, | ||
{ | ||
"type": "array", | ||
"items": { "type": "string", "pattern": "^/.+/$" } | ||
} | ||
] | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
}, | ||
"required": ["repoGroups", "orgGroups", "patternGroups"], | ||
"additionalProperties": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from 'zod'; | ||
|
||
const UrlSchema = z.record( | ||
z.string(), | ||
z.union([z.string(), z.array(z.string())]), | ||
); | ||
|
||
const MonorepoSchema = z.object({ | ||
repoGroups: UrlSchema, | ||
orgGroups: UrlSchema, | ||
patternGroups: UrlSchema, | ||
}); | ||
|
||
export { MonorepoSchema }; |