Skip to content

Commit

Permalink
refactor: data validation using schema (#30797)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <[email protected]>
  • Loading branch information
RahulGautamSingh and viceice committed Aug 29, 2024
1 parent fbe4dba commit d1af677
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/data/monorepo.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tools/schemas/monorepo-schema.json",
"repoGroups": {
"accounts": "https://github.com/accounts-js/accounts",
"acot": "https://github.com/acot-a11y/acot",
Expand Down
64 changes: 64 additions & 0 deletions test/validate-schemas.spec.ts
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,
},
});
}
});
});
53 changes: 53 additions & 0 deletions tools/schemas/monorepo-schema.json
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
}
14 changes: 14 additions & 0 deletions tools/schemas/schema.ts
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 };

0 comments on commit d1af677

Please sign in to comment.