-
Notifications
You must be signed in to change notification settings - Fork 90
feat: manually generated json-schema with validation #919
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,12 @@ import * as os from 'os'; | |
| import * as fs_path from 'path'; | ||
| import { ToolkitError } from '@aws-cdk/toolkit-lib'; | ||
| import * as fs from 'fs-extra'; | ||
| import { validate } from 'jsonschema'; | ||
| import { Context, PROJECT_CONTEXT } from '../api/context'; | ||
| import { Settings } from '../api/settings'; | ||
| import type { Tag } from '../api/tags'; | ||
| import type { IoHelper } from '../api-private'; | ||
| import { cdkConfigSchema } from '../schema'; | ||
|
|
||
| export const PROJECT_CONFIG = 'cdk.json'; | ||
| export { PROJECT_CONTEXT } from '../api/context'; | ||
|
|
@@ -210,6 +212,9 @@ async function settingsFromFile(ioHelper: IoHelper, fileName: string): Promise<S | |
| const expanded = expandHomeDir(fileName); | ||
| if (await fs.pathExists(expanded)) { | ||
| const data = await fs.readJson(expanded); | ||
|
|
||
| await validateConfigurationFile(data, fileName, ioHelper); | ||
|
|
||
| settings = new Settings(data); | ||
| } else { | ||
| settings = new Settings(); | ||
|
|
@@ -408,3 +413,96 @@ async function parseStringTagsListToObject( | |
| } | ||
| return tags.length > 0 ? tags : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Find similar property names to suggest corrections for typos | ||
| */ | ||
| function getTypeCorrectionHint(expectedType: string, actualValue: any): string { | ||
| if (expectedType === 'boolean' && (actualValue === 'true' || actualValue === 'false')) { | ||
| return ` (use ${actualValue} without quotes)`; | ||
| } | ||
| if (expectedType === 'string' && typeof actualValue === 'number') { | ||
| return ` (use "${actualValue}" with quotes)`; | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| /** | ||
| * Validates configuration data against the CDK JSON Schema and emits warnings for issues | ||
| * | ||
| * @param data - The configuration object to validate | ||
| * @param fileName - The file name for error reporting | ||
| * @param ioHelper - IoHelper for logging warnings | ||
| */ | ||
| async function validateConfigurationFile(data: any, fileName: string, ioHelper: IoHelper): Promise<void> { | ||
| try { | ||
| const schema = cdkConfigSchema; | ||
| const result = validate(data, schema); | ||
|
|
||
| await handleSchemaErrors(result.errors, fileName, ioHelper); | ||
|
|
||
| await handleUnknownProperties(data, schema, fileName, ioHelper); | ||
| } catch (error) { | ||
| await ioHelper.defaults.debug(`Schema validation failed for ${fileName}: ${error}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles schema validation errors and emits appropriate warnings | ||
| */ | ||
| async function handleSchemaErrors(errors: any[], fileName: string, ioHelper: IoHelper): Promise<void> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐝
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐝
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐝
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐝 |
||
| if (!errors || errors.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| for (const error of errors) { | ||
| const propertyPath = error.property?.replace('instance.', '') || 'root'; | ||
| const propertyName = propertyPath || 'property'; | ||
|
|
||
| if (error.name === 'type') { | ||
| const errorSchema = error.schema as any; | ||
| const expectedType = Array.isArray(errorSchema.type) | ||
| ? errorSchema.type.join(' or ') | ||
| : errorSchema.type || 'unknown'; | ||
| const actualType = typeof error.instance; | ||
| const hint = getTypeCorrectionHint(expectedType, error.instance); | ||
|
|
||
| await ioHelper.defaults.warn( | ||
| `${fileName}: '${propertyName}' should be ${expectedType}, got ${actualType}${hint}`, | ||
| ); | ||
| } else if (error.name === 'enum') { | ||
| const allowedValues = (error.schema as any).enum?.join(', ') || 'unknown'; | ||
| await ioHelper.defaults.warn( | ||
| `${fileName}: '${propertyName}' must be one of: ${allowedValues}`, | ||
| ); | ||
| } else if (error.name !== 'additionalProperties') { | ||
| // Generic fallback for other validation errors (skip additionalProperties as we handle those separately) | ||
| await ioHelper.defaults.warn( | ||
| `${fileName}: ${error.message}`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles unknown properties | ||
| */ | ||
| async function handleUnknownProperties( | ||
| data: any, | ||
| schema: any, | ||
| fileName: string, | ||
| ioHelper: IoHelper, | ||
| ): Promise<void> { | ||
| if (!data || typeof data !== 'object' || Array.isArray(data)) { | ||
| return; | ||
| } | ||
|
|
||
| const knownProperties = Object.keys(schema.properties || {}); | ||
| const unknownProperties = Object.keys(data).filter(prop => !knownProperties.includes(prop)); | ||
|
|
||
| for (const prop of unknownProperties) { | ||
| await ioHelper.defaults.warn( | ||
| `${fileName}: Unknown property '${prop}' (not a standard CDK property)`, | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 is there a better way to do this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 is there a better way to do this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 is there a better way to do this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 is there a better way to do this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 what do you think? is there a better way to do this
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 The Hive Has Spoken
Question: "what do you think? is there a better way to do this"
Options considered:
Winner: Zod Schema as Source of Truth (1 round)
Debate details
Round 1: Zod Schema as Source of Truth(3) · Manual JSON Schema(1) · TypeScript-to-Schema Generation(0) · JSON Schema with Ajv Compilation(0) · TypeBox Schema Definition(0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 what do you think? is there a better way to do this
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 The Hive Has Spoken
Question: "what do you think? is there a better way to do this"
Options considered:
npx projen, treating the schema as a managed project artifact rather than manually authored code.Winner: Zod Runtime Validation (1 round)
Debate details
Round 1: Zod Runtime Validation(9) · TypeScript-Generated Schema(3) · Projen Schema Integration(1) · Manual JSON Schema(0) · JSON Schema from JSII(0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 what do you think? is there a better way to do thi
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐝 The Hive Has Spoken
Question: "what do you think? is there a better way to do thi"
Options considered:
npx projen.Winner: Zod Runtime Validation (1 round)
Debate details
Round 1: Zod Runtime Validation(7) · JSON Schema with Codegen(3) · Projen Structured Config(2) · TypeScript-Generated Schema(1) · Manual JSON Schema(0)