-
Notifications
You must be signed in to change notification settings - Fork 16
feat(models): setup types and parser with zod #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 48 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
d067799
feat(models): setup types and parser with zod
BioPhoton 46d4408
fix(models): add package.lock
BioPhoton 0e5ef27
refactor(models): format
BioPhoton 1369565
refactor(models): align package.json
BioPhoton 63c1bcc
refactor(models): format
BioPhoton 8096b85
refactor(models): package.lock
BioPhoton 70fc405
Update packages/models/project.json
BioPhoton c885349
Update packages/models/project.json
BioPhoton 337c9c2
refactor(models): remove budgets from initial code base
BioPhoton aaa17a5
Update packages/models/src/lib/category-config.ts
BioPhoton 0a67872
refactor(models): remove budgets
BioPhoton 48dd062
Update packages/models/src/lib/plugins.ts
BioPhoton 3b35871
Update packages/models/src/lib/plugins.ts
BioPhoton 34ceba1
Update packages/models/src/lib/upload.ts
BioPhoton 71c5a4d
Update packages/models/src/lib/upload.ts
BioPhoton 05486fd
refactor(models): add int and non negative validators
BioPhoton 23cf79a
refactor(models): add cross field validators
BioPhoton d52de86
refactor(models): format files
BioPhoton b9e1244
refactor(models): implement feedback from PR, format
BioPhoton 9bc21e6
Update packages/models/project.json
BioPhoton 20ccf92
Update packages/models/src/lib/category-config.ts
BioPhoton 401ed9a
Update packages/models/src/lib/implementation/schemas.ts
BioPhoton cbee718
Update packages/models/src/lib/category-config.ts
BioPhoton a000ee9
Update packages/models/src/lib/core-config.spec.ts
BioPhoton 651498b
Update packages/models/src/lib/output.ts
BioPhoton 4ec4bc4
Update packages/models/src/lib/output.ts
BioPhoton c0b4164
Update packages/models/src/lib/plugins.ts
BioPhoton 462c5ed
Update packages/models/src/lib/plugins.ts
BioPhoton deb7209
Update packages/models/src/lib/core-config.spec.ts
BioPhoton d3cbc08
Update packages/models/src/lib/output.ts
BioPhoton 82cc90f
Update packages/models/src/lib/output.ts
BioPhoton 7fcdf05
Update packages/models/src/lib/core-config.ts
BioPhoton b7e985c
Update packages/models/src/lib/core-config.ts
BioPhoton fca30a4
Update packages/models/src/lib/implementation/utils.spec.ts
BioPhoton 74e6594
Update packages/models/src/lib/implementation/utils.spec.ts
BioPhoton 9a1a2c6
Merge remote-tracking branch 'origin/main' into add-models
BioPhoton 4e0fd2e
refactor(models): add regex and validators and tests
BioPhoton 66a5399
refactor(models): fix tests and lint errors
BioPhoton ce2362b
refactor(models): fix tests and comments
BioPhoton 1c0e36e
refactor(models): add test case
BioPhoton 9979695
Update schemas.ts
BioPhoton c000792
Update schemas.ts
BioPhoton acf20d6
Update schemas.ts
BioPhoton 8b3c562
Update schemas.ts
BioPhoton ad2f7e9
Update utils.ts
BioPhoton ea3d7b1
Update utils.ts
BioPhoton 5406c96
Update global-cli-options.ts
BioPhoton d6a82d6
refactor(models): merge suggestions, fix tests
BioPhoton 7831a5a
Update packages/models/src/lib/category-config.spec.ts
BioPhoton 2f7b498
Update packages/models/src/lib/implementation/schemas.ts
BioPhoton 8f2e201
Update packages/models/src/lib/category-config.spec.ts
BioPhoton 41b4edc
refactor(models): reinstall
BioPhoton 9e97fa4
refactor(models): fix dependencies
BioPhoton c54d461
refactor(models): fix dependencies 2
BioPhoton d2ea0f4
refactor(models): fix dependencies 2
BioPhoton d6fd5e8
refactor(models): fix dependencies 3
BioPhoton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,25 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": ["error"] | ||
} | ||
} | ||
] | ||
} |
This file contains hidden or 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 @@ | ||
# models | ||
|
||
Model definitions and validators for the CLI configuration as well as plugin types and respective parser. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { CoreConfigSchema, pluginConfigSchema } from '@quality-metrics/models'; | ||
|
||
export default { | ||
// ... | ||
plugins: [pluginConfigSchema.parse({ ... })], | ||
} satisfies CoreConfigSchema; | ||
``` |
This file contains hidden or 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,24 @@ | ||
{ | ||
"name": "models", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "packages/models/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["packages/models/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/vite:test", | ||
"outputs": ["{workspaceRoot}/coverage/packages/cli"], | ||
"options": { | ||
"passWithNoTests": true, | ||
"reportsDirectory": "../../coverage/packages/cli" | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
This file contains hidden or 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,18 @@ | ||
export { coreConfigSchema, CoreConfigSchema } from './lib/core-config'; | ||
export { uploadConfigSchema, UploadConfigSchema } from './lib/upload-config'; | ||
export { pluginConfigSchema, PluginConfigSchema } from './lib/plugin-config'; | ||
export { | ||
runnerOutputSchema, | ||
RunnerOutputSchema, | ||
runnerOutputAuditRefsPresentInPluginConfigs, | ||
PluginsOutputSchema, | ||
} from './lib/output'; | ||
export { persistConfigSchema, PersistConfigSchema } from './lib/persist-config'; | ||
export { | ||
categoryConfigSchema, | ||
CategoryConfigSchema, | ||
} from './lib/category-config'; | ||
export { | ||
globalCliArgsSchema, | ||
GlobalCliArgsSchema, | ||
} from './lib/global-cli-options'; |
This file contains hidden or 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,43 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { mockCategory } from './implementation/helpers.mock'; | ||
import { categoryConfigSchema } from './category-config'; | ||
|
||
describe('categoryConfigSchema', () => { | ||
it('should parse if configuration with audit refs is valid', () => { | ||
const cfg = mockCategory({ auditRefOrGroupRef: ['test#a', 'test#b'] }); | ||
expect(() => categoryConfigSchema.parse(cfg)).not.toThrow(); | ||
}); | ||
|
||
it('should parse if configuration with group refs is valid', () => { | ||
const cfg = mockCategory({ auditRefOrGroupRef: ['es-lint#group:base'] }); | ||
expect(() => categoryConfigSchema.parse(cfg)).not.toThrow(); | ||
}); | ||
|
||
it('should throw if category slug has a invalid pattern', () => { | ||
const invalidCategorySlug = '-invalid-category-slug'; | ||
const cfg = mockCategory({ categorySlug: invalidCategorySlug }); | ||
|
||
expect(() => categoryConfigSchema.parse(cfg)).toThrow( | ||
`slug has to follow the patter`, | ||
); | ||
}); | ||
|
||
it('should throw if audit ref in metrics is invalid', () => { | ||
const invalidAuditRef = 'no-any'; | ||
const cfg = mockCategory({ auditRefOrGroupRef: [invalidAuditRef] }); | ||
|
||
expect(() => categoryConfigSchema.parse(cfg)).toThrow( | ||
`ref has to follow the patter`, | ||
BioPhoton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
}); | ||
|
||
it('should throw if duplicate refs to audits or groups in metrics are given', () => { | ||
const duplicatedSlug = 'test#a'; | ||
const cfg = mockCategory({ | ||
auditRefOrGroupRef: [duplicatedSlug, duplicatedSlug], | ||
}); | ||
expect(() => categoryConfigSchema.parse(cfg)).toThrow( | ||
'the following audit or group refs are duplicates', | ||
); | ||
}); | ||
}); |
This file contains hidden or 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,72 @@ | ||
import { z } from 'zod'; | ||
import { | ||
refSchema, | ||
descriptionSchema, | ||
slugSchema, | ||
titleSchema, | ||
weightSchema, | ||
} from './implementation/schemas'; | ||
import { errorItems, hasDuplicateStrings } from './implementation/utils'; | ||
|
||
/** | ||
* | ||
* Define Zod schema for the CategoryConfig type | ||
* | ||
* @example | ||
* | ||
* // Example data for the CategoryConfig type | ||
* const data = { | ||
* // ... | ||
* }; | ||
* | ||
* // Validate the data against the schema | ||
* const validationResult = categoryConfigSchema.safeParse(data); | ||
* | ||
* if (validationResult.success) { | ||
* console.log('Valid category config:', validationResult.data); | ||
* } else { | ||
* console.error('Invalid category config:', validationResult.error); | ||
* } | ||
*/ | ||
export const categoryConfigSchema = z.object( | ||
{ | ||
slug: slugSchema(), | ||
title: titleSchema('Display name for the category '), | ||
description: descriptionSchema('Optional description in Markdown format'), | ||
metrics: z | ||
.array( | ||
z.object( | ||
{ | ||
ref: refSchema( | ||
"Reference to a plugin's audit (e.g. 'eslint#max-lines') or group (e.g. 'lhci#group:performance')", | ||
), | ||
weight: weightSchema(), | ||
}, | ||
{ description: 'Array of metrics associated with the category' }, | ||
), | ||
) | ||
// metrics have unique refs to audits or groups within a category | ||
.refine( | ||
metrics => !getDuplicateRefsInCategoryMetrics(metrics), | ||
metrics => ({ | ||
message: duplicateRefsInCategoryMetricsErrorMsg(metrics), | ||
}), | ||
), | ||
}, | ||
{ | ||
description: 'Weighted references to plugin-specific audits/categories', | ||
}, | ||
); | ||
|
||
export type CategoryConfigSchema = z.infer<typeof categoryConfigSchema>; | ||
|
||
// helper for validator: categories have unique refs to audits or groups | ||
export function duplicateRefsInCategoryMetricsErrorMsg(metrics) { | ||
const duplicateRefs = getDuplicateRefsInCategoryMetrics(metrics); | ||
return `In the categories, the following audit or group refs are duplicates: ${errorItems( | ||
duplicateRefs, | ||
)}`; | ||
} | ||
function getDuplicateRefsInCategoryMetrics(metrics) { | ||
return hasDuplicateStrings(metrics.map(({ ref }) => ref)); | ||
} |
This file contains hidden or 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,84 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { | ||
mockCategory, | ||
mockConfig, | ||
mockPluginConfig, | ||
} from './implementation/helpers.mock'; | ||
import { coreConfigSchema } from './core-config'; | ||
|
||
/* | ||
- plugin slug: es-lint | ||
- audit slug: no-any | ||
- group slug: basics | ||
- audit: no-any | ||
- category: best-practices | ||
|
||
- from category to audit: es-lint#no-any | ||
- from category to group: es-lint#group:basics | ||
*/ | ||
describe('CoreConfig', () => { | ||
it('should parse if configuration is valid', () => { | ||
const cfg = mockConfig({ pluginSlug: 'test', auditSlug: ['a', 'b'] }); | ||
cfg.categories.push( | ||
mockCategory({ auditRefOrGroupRef: ['test#a', 'test#b'] }), | ||
); | ||
expect(() => coreConfigSchema.parse(cfg)).not.toThrow(); | ||
}); | ||
|
||
it('should parse if configuration and groups are valid', () => { | ||
const pluginSlug = 'plg'; | ||
const cfg = mockConfig(); | ||
cfg.plugins = [ | ||
mockPluginConfig({ pluginSlug, auditSlug: 'lcp', groupSlug: 'perf' }), | ||
]; | ||
cfg.categories = [ | ||
mockCategory({ auditRefOrGroupRef: ['plg#lcp', 'plg#group:perf'] }), | ||
]; | ||
// In the categories, the following plugin refs do not exist in the provided plugins: test#group:group-slug | ||
expect(() => coreConfigSchema.parse(cfg)).not.toThrow(); | ||
}); | ||
|
||
it('should throw if the category slugs are not unique', () => { | ||
const cfg = mockConfig({ pluginSlug: 'test', auditSlug: ['a', 'b'] }); | ||
const duplicatedSlug = 'test'; | ||
cfg.categories.push( | ||
mockCategory({ categorySlug: 'test', auditRefOrGroupRef: ['test#a'] }), | ||
mockCategory({ categorySlug: 'test', auditRefOrGroupRef: ['test#b'] }), | ||
); | ||
expect(() => coreConfigSchema.parse(cfg)).toThrow( | ||
`In the categories, the following slugs are duplicated: ${duplicatedSlug}`, | ||
); | ||
}); | ||
|
||
it('should throw if ref in a category does not exist in audits', () => { | ||
const cfg = mockConfig({ pluginSlug: 'test', auditSlug: ['a', 'b'] }); | ||
const missingSlug = 'missing-plugin-slug-in-category#auditref'; | ||
cfg.categories.push( | ||
mockCategory({ | ||
categorySlug: 'test', | ||
auditRefOrGroupRef: [`${missingSlug}`], | ||
}), | ||
); | ||
expect(() => coreConfigSchema.parse(cfg)).toThrow( | ||
`In the categories, the following plugin refs do not exist in the provided plugins: ${missingSlug}`, | ||
); | ||
}); | ||
|
||
it('should throw if ref in a category does not exist in groups', () => { | ||
const cfg = mockConfig({ | ||
pluginSlug: 'test', | ||
auditSlug: ['a', 'b'], | ||
groupSlug: 'test#a', | ||
}); | ||
const missingSlug = 'missing-plugin-slug-in-category#groups:auditref'; | ||
cfg.categories.push( | ||
mockCategory({ | ||
categorySlug: 'test', | ||
auditRefOrGroupRef: [`${missingSlug}`], | ||
}), | ||
); | ||
expect(() => coreConfigSchema.parse(cfg)).toThrow( | ||
`In the categories, the following plugin refs do not exist in the provided plugins: ${missingSlug}`, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.