-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(web_core): implement v0.9 basic catalog Zod schemas and ComponentApi #814
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
jacobsimionato
merged 2 commits into
google:main
from
jacobsimionato:feat/web-core-v09-basic-catalog-schemas
Mar 11, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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.
161 changes: 161 additions & 0 deletions
161
renderers/web_core/src/v0_9/basic_catalog/components/basic_components.test.ts
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,161 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { describe, it } from "node:test"; | ||
| import * as assert from "node:assert"; | ||
| import { readFileSync } from "fs"; | ||
| import { resolve, join, dirname } from "path"; | ||
| import { fileURLToPath } from "url"; | ||
| import { BASIC_COMPONENTS } from "./basic_components.js"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
|
|
||
| // `__dirname` will be `dist/src/v0_9/basic_catalog/components` when run via `node --test dist/**/*.test.js` | ||
| const SPEC_DIR_V0_9 = resolve( | ||
| __dirname, | ||
| "../../../../../../../specification/v0_9/json", | ||
| ); | ||
|
|
||
| function getZodShape(zodObj: any): any { | ||
| let current = zodObj; | ||
| while (current?._def) { | ||
| if (current._def.typeName === "ZodObject") return current.shape || current._def.shape(); | ||
| current = current._def.innerType ?? current._def.schema; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function getZodArrayElement(zodObj: any): any { | ||
| let current = zodObj; | ||
| while (current?._def) { | ||
| if (current._def.typeName === "ZodArray") return current._def.type; | ||
| current = current._def.innerType ?? current._def.schema; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| describe("Basic Components Schema Verification", () => { | ||
| it("verifies all basic components exist in the catalog and their required properties and descriptions align", () => { | ||
| const jsonSpecPath = join(SPEC_DIR_V0_9, "basic_catalog.json"); | ||
| const officialSchema = JSON.parse(readFileSync(jsonSpecPath, "utf-8")); | ||
|
|
||
| const componentsMap = officialSchema.components; | ||
|
|
||
| for (const api of BASIC_COMPONENTS) { | ||
| const componentName = api.name; | ||
| const jsonComponentDef = componentsMap[componentName]; | ||
| assert.ok( | ||
| jsonComponentDef, | ||
| `Component ${componentName} not found in basic_catalog.json` | ||
| ); | ||
|
|
||
| const specificPropsDef = jsonComponentDef.allOf.find( | ||
| (item: any) => item.properties && item.properties.component | ||
| ); | ||
|
|
||
| assert.ok( | ||
| specificPropsDef, | ||
| `Could not find specific properties definition for ${componentName} in basic_catalog.json` | ||
| ); | ||
|
|
||
| if (specificPropsDef.description) { | ||
| assert.strictEqual( | ||
| api.schema.description, | ||
| specificPropsDef.description, | ||
| `Component description mismatch for ${componentName}` | ||
| ); | ||
| } | ||
|
|
||
| const jsonProperties = specificPropsDef.properties; | ||
| const jsonRequired = specificPropsDef.required || []; | ||
|
|
||
| const zodShape = getZodShape(api.schema); | ||
|
|
||
| // Check CatalogComponentCommon properties which are not in specificPropsDef but in allOf | ||
| const catalogCommonDef = officialSchema.$defs.CatalogComponentCommon; | ||
| if (catalogCommonDef?.properties) { | ||
| for (const propName in catalogCommonDef.properties) { | ||
| const jsonProp = catalogCommonDef.properties[propName]; | ||
| if (zodShape[propName] && jsonProp.description) { | ||
| assert.strictEqual( | ||
| zodShape[propName].description, | ||
| jsonProp.description, | ||
| `Description mismatch for common property '${propName}' of component '${componentName}'` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const propName of Object.keys(jsonProperties)) { | ||
| if (propName === "component") continue; // Handled by envelope | ||
| const jsonProp = jsonProperties[propName]; | ||
| const zodPropSchema = zodShape[propName]; | ||
|
|
||
| assert.ok( | ||
| zodPropSchema, | ||
| `Property '${propName}' is missing in Zod schema for component '${componentName}'` | ||
| ); | ||
|
|
||
| if (jsonProp.description) { | ||
| assert.strictEqual( | ||
| zodPropSchema.description, | ||
| jsonProp.description, | ||
| `Description mismatch for property '${propName}' of component '${componentName}'` | ||
| ); | ||
| } | ||
|
|
||
| // Check array items | ||
| if (jsonProp.type === "array" && jsonProp.items && jsonProp.items.properties) { | ||
| const itemProps = jsonProp.items.properties; | ||
| const zodItemShape = getZodShape(getZodArrayElement(zodPropSchema)); | ||
| for (const itemProp of Object.keys(itemProps)) { | ||
| if (itemProps[itemProp].description) { | ||
| assert.strictEqual( | ||
| zodItemShape[itemProp].description, | ||
| itemProps[itemProp].description, | ||
| `Description mismatch for array item property '${propName}.${itemProp}' of component '${componentName}'` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const reqProp of jsonRequired) { | ||
| if (reqProp === "component") continue; | ||
| assert.ok( | ||
| zodShape[reqProp], | ||
| `Required property '${reqProp}' from JSON schema is missing in Zod schema for component '${componentName}'` | ||
| ); | ||
|
|
||
| const propSchema = zodShape[reqProp]; | ||
| let isOptional = false; | ||
| let current = propSchema; | ||
| while (current && current._def) { | ||
| if (current._def.typeName === "ZodOptional" || current._def.typeName === "ZodDefault") { | ||
| isOptional = true; | ||
| break; | ||
| } | ||
| current = current._def.innerType; | ||
| } | ||
|
|
||
| if (isOptional) { | ||
| assert.fail(`Property '${reqProp}' is required in JSON but optional/default in Zod for '${componentName}'`); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
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 path to the specification JSON file is constructed using a long relative path (
../../../../../../../). This is brittle and highly dependent on the build's output directory structure. If the location of the compiled test file changes, this path will break. Consider a more robust approach, such as assuming the test is run from the project root and usingprocess.cwd()to construct an absolute path.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.
Well, I don't like Gemini's solution either. Tests should be able to be run from any directory. I think this is fine.
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.
Yeah +1, let's not change this. The relative path is fine.