-
Notifications
You must be signed in to change notification settings - Fork 412
Fix error message truncation in TypeScript validator #325
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b363130
Initial plan
Copilot 90c90e5
Fix error message truncation in TypeScript validator
Copilot b21e3d1
Refine expandMissingPropertiesMessage: use getStart() and add JSDoc
Copilot 8249305
Add unit tests for TypeScript validator and npm test script
Copilot 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
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
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,15 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "es2021", | ||
| "lib": ["es2021"], | ||
| "module": "node16", | ||
| "moduleResolution": "node16", | ||
| "types": ["node"], | ||
| "esModuleInterop": true, | ||
| "outDir": "../out", | ||
| "rootDir": ".", | ||
| "skipLibCheck": true, | ||
| "strict": true | ||
| }, | ||
| "include": ["./**/*.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,196 @@ | ||
| import { describe, it } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { createTypeScriptJsonValidator } from "../dist/ts/index.js"; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Shared schemas | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| // Schema with 8 required properties — enough to trigger TS error 2740 | ||
| // ("and N more") when several are absent. | ||
| const largeSchema = ` | ||
| export interface Doc { | ||
| id: number; | ||
| title: string; | ||
| slug: string; | ||
| abstract: string; | ||
| description: string; | ||
| author: string; | ||
| date: string; | ||
| category: string; | ||
| }`; | ||
|
|
||
| // Schema with a mix of required and optional properties. | ||
| const mixedSchema = ` | ||
| export interface Item { | ||
| id: number; | ||
| name: string; | ||
| tag?: string; | ||
| extra1: string; | ||
| extra2: string; | ||
| extra3: string; | ||
| extra4: string; | ||
| extra5: string; | ||
| }`; | ||
|
|
||
| // Small schema for simpler error cases. | ||
| const smallSchema = ` | ||
| export interface Point { | ||
| x: number; | ||
| y: number; | ||
| }`; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Helper | ||
| // --------------------------------------------------------------------------- | ||
| function validatorFor<T extends object>(schema: string, typeName: string) { | ||
| return createTypeScriptJsonValidator<T>(schema, typeName); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Tests | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("createTypeScriptJsonValidator", () => { | ||
|
|
||
| describe("valid objects", () => { | ||
| it("accepts an object that satisfies all required properties", () => { | ||
| const v = validatorFor(largeSchema, "Doc"); | ||
| const result = v.validate({ | ||
| id: 1, title: "T", slug: "s", abstract: "a", | ||
| description: "d", author: "au", date: "2024-01-01", category: "c" | ||
| }); | ||
| assert.ok(result.success, "expected validation to succeed"); | ||
| }); | ||
|
|
||
| it("accepts an object that supplies optional properties", () => { | ||
| const v = validatorFor(mixedSchema, "Item"); | ||
| const result = v.validate({ | ||
| id: 1, name: "n", tag: "t", | ||
| extra1: "a", extra2: "b", extra3: "c", extra4: "d", extra5: "e" | ||
| }); | ||
| assert.ok(result.success, "expected validation to succeed with optional props"); | ||
| }); | ||
|
|
||
| it("accepts an object that omits optional properties", () => { | ||
| const v = validatorFor(mixedSchema, "Item"); | ||
| const result = v.validate({ | ||
| id: 1, name: "n", | ||
| extra1: "a", extra2: "b", extra3: "c", extra4: "d", extra5: "e" | ||
| }); | ||
| assert.ok(result.success, "expected validation to succeed without optional props"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("type mismatch errors (backward-compatible: unchanged code path)", () => { | ||
| it("reports a type error when a property has the wrong type", () => { | ||
| const v = validatorFor(smallSchema, "Point"); | ||
| const result = v.validate({ x: "not-a-number", y: 2 }); | ||
| assert.ok(!result.success, "expected validation to fail"); | ||
| assert.ok( | ||
| result.message.includes("not assignable to type"), | ||
| `expected type-mismatch message, got: ${result.message}` | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("single missing required property (TS error 2741, unchanged code path)", () => { | ||
| it("reports the missing property by name", () => { | ||
| const v = validatorFor(smallSchema, "Point"); | ||
| const result = v.validate({ x: 1 }); | ||
| assert.ok(!result.success, "expected validation to fail"); | ||
| assert.ok( | ||
| result.message.includes("'y'") && result.message.includes("missing"), | ||
| `expected missing-property message, got: ${result.message}` | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("2–5 missing required properties (TS error 2739, unchanged code path)", () => { | ||
| it("lists all missing properties without truncation for 4 missing", () => { | ||
| const schema = `export interface T { a: number; b: string; c: boolean; d: number; e: string; }`; | ||
| const v = validatorFor(schema, "T"); | ||
| // Provide only 'a', missing b/c/d/e | ||
| const result = v.validate({ a: 1 }); | ||
| assert.ok(!result.success, "expected validation to fail"); | ||
| for (const prop of ["b", "c", "d", "e"]) { | ||
| assert.ok( | ||
| result.message.includes(prop), | ||
| `expected '${prop}' in error message, got: ${result.message}` | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("6+ missing required properties (TS error 2740 — the truncation fix)", () => { | ||
| it("returns the full list of missing properties without truncation", () => { | ||
| const v = validatorFor(largeSchema, "Doc"); | ||
| // Provide only id and title; slug/abstract/description/author/date/category are missing | ||
| const result = v.validate({ id: 1, title: "Hello" }); | ||
| assert.ok(!result.success, "expected validation to fail"); | ||
|
|
||
| const missing = ["slug", "abstract", "description", "author", "date", "category"]; | ||
| for (const prop of missing) { | ||
| assert.ok( | ||
| result.message.includes(prop), | ||
| `expected '${prop}' in error message but got: ${result.message}` | ||
| ); | ||
| } | ||
| assert.ok( | ||
| !result.message.includes("more"), | ||
| `error message should not contain "more" (truncation indicator), got: ${result.message}` | ||
| ); | ||
| }); | ||
|
|
||
| it("excludes optional properties from the missing-properties list", () => { | ||
| const v = validatorFor(mixedSchema, "Item"); | ||
| // Provide only id — name/extra1..5 are missing required; tag is optional | ||
| const result = v.validate({ id: 1 }); | ||
| assert.ok(!result.success, "expected validation to fail"); | ||
|
|
||
| const requiredMissing = ["name", "extra1", "extra2", "extra3", "extra4", "extra5"]; | ||
| for (const prop of requiredMissing) { | ||
| assert.ok( | ||
| result.message.includes(prop), | ||
| `expected '${prop}' in error message, got: ${result.message}` | ||
| ); | ||
| } | ||
| assert.ok( | ||
| !result.message.includes("tag"), | ||
| `optional property 'tag' should not appear in missing-properties message, got: ${result.message}` | ||
| ); | ||
| }); | ||
|
|
||
| it("does not include 'and N more' truncation text", () => { | ||
| const v = validatorFor(largeSchema, "Doc"); | ||
| const result = v.validate({}); | ||
| assert.ok(!result.success, "expected validation to fail"); | ||
| assert.ok( | ||
| !/ and \d+ more/.test(result.message), | ||
| `message should not contain "and N more", got: ${result.message}` | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getSchemaText and getTypeName", () => { | ||
| it("returns the original schema text", () => { | ||
| const v = validatorFor(smallSchema, "Point"); | ||
| assert.strictEqual(v.getSchemaText(), smallSchema); | ||
| }); | ||
|
|
||
| it("returns the correct type name", () => { | ||
| const v = validatorFor(smallSchema, "Point"); | ||
| assert.strictEqual(v.getTypeName(), "Point"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("createModuleTextFromJson", () => { | ||
| it("produces valid TypeScript module text for a simple object", () => { | ||
| const v = validatorFor(smallSchema, "Point"); | ||
| const result = v.createModuleTextFromJson({ x: 1, y: 2 }); | ||
| assert.ok(result.success, "expected module text creation to succeed"); | ||
| assert.ok(result.data.includes("import { Point } from './schema'")); | ||
| assert.ok(result.data.includes("const json: Point =")); | ||
| }); | ||
| }); | ||
| }); |
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.