-
-
Notifications
You must be signed in to change notification settings - Fork 192
Fix template literal validation tag inheritance in type checking #1656
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
Draft
Copilot
wants to merge
4
commits into
master
Choose a base branch
from
copilot/fix-1635
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b3f9d20
Initial plan
Copilot 79e201e
Initial analysis: found template literal tag inheritance issue in int…
Copilot 1a8a584
Fix template literal validation tag inheritance by adding tag handlin…
Copilot 1891b30
Add comprehensive tests for template literal validation tag fix
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
Large diffs are not rendered by default.
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
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
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
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
75 changes: 75 additions & 0 deletions
75
test/src/features/issues/test_issue_1635_template_literal_validation_tags.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,75 @@ | ||
| import typia, { tags } from "typia"; | ||
|
|
||
| /** | ||
| * Reproduction of the exact issue reported in GitHub issue. | ||
| * | ||
| * This demonstrates that the fix correctly handles the original problematic case. | ||
| * Before the fix, Test1Type would pass when it should fail. | ||
| * After the fix, Test1Type correctly fails validation. | ||
| */ | ||
| export const test_issue_reproduction = (): void => { | ||
| console.log("=== Reproducing Original Issue ==="); | ||
|
|
||
| const testValue = 'prefix;"+,df0123456789postfix' as const; | ||
| console.log("Test value:", testValue); | ||
| console.log("Test value length:", testValue.length, "chars"); | ||
| console.log(); | ||
|
|
||
| // Function to log validation results like in the original issue | ||
| function logResult(name: string, res: typia.IValidation<unknown>) { | ||
| console.log(`${name} checking =>`); | ||
| if (res.success) { | ||
| console.log('Successfully validated.'); | ||
| } else { | ||
| console.log('Failed!'); | ||
| console.log(res.errors); | ||
| } | ||
| console.log(); | ||
| } | ||
|
|
||
| // Test1Type - Template literal with validation tags (FIXED) | ||
| // Before fix: this would pass incorrectly | ||
| // After fix: this should fail correctly | ||
| logResult( | ||
| 'Test1Type (FIXED)', | ||
| typia.validate< | ||
| tags.MaxLength<10> & | ||
| tags.Pattern<'^[a-zA-Z0-9_]+$'> & | ||
| `prefix${string}postfix` | ||
| >(testValue), | ||
| ); | ||
|
|
||
| // Test2Type - Regular string with validation tags (should always fail) | ||
| logResult( | ||
| 'Test2Type (Control)', | ||
| typia.validate<tags.MaxLength<10> & tags.Pattern<'^[a-zA-Z0-9_]+$'> & string>( | ||
| testValue, | ||
| ), | ||
| ); | ||
|
|
||
| // Test3Type - Mixed intersection (should always pass - no validation tags on template) | ||
| logResult( | ||
| 'Test3Type (Mixed)', | ||
| typia.validate< | ||
| tags.MaxLength<10> & | ||
| tags.Pattern<'^[a-zA-Z0-9_]+$'> & | ||
| // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents | ||
| string & | ||
| `prefix${string}postfix` | ||
| >(testValue), | ||
| ); | ||
|
|
||
| // Test4Type - Tags inside template (should always pass - no validation logic) | ||
| logResult( | ||
| 'Test4Type (Internal)', | ||
| typia.validate<`prefix${tags.MaxLength<10> & | ||
| tags.Pattern<'^[a-zA-Z0-9_]+$'> & | ||
| string}postfix`>(testValue), | ||
| ); | ||
|
|
||
| console.log("=== Expected Results After Fix ==="); | ||
| console.log("Test1Type: Should FAIL (template + validation tags now work)"); | ||
| console.log("Test2Type: Should FAIL (string + validation tags)"); | ||
| console.log("Test3Type: Should PASS (template pattern only, no validation tags on template)"); | ||
| console.log("Test4Type: Should PASS (validation tags inside template literal, not applied to whole)"); | ||
| }; |
85 changes: 85 additions & 0 deletions
85
test/src/features/issues/test_template_literal_tags_comprehensive.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,85 @@ | ||
| import typia, { tags } from "typia"; | ||
|
|
||
| /** | ||
| * Test the fix across different typia functions mentioned in the issue: | ||
| * - validate (already tested) | ||
| * - assert | ||
| * - json.schemas | ||
| */ | ||
| export const test_template_literal_tags_comprehensive = (): void => { | ||
| const validValue = 'prefix123postfix'; // Should pass all validations | ||
| const invalidValue = 'prefix;"+,invalid'; // Should fail validation (pattern + length) | ||
|
|
||
| console.log("=== Testing Template Literal Tags Across All Functions ==="); | ||
| console.log("Valid value:", validValue); | ||
| console.log("Invalid value:", invalidValue); | ||
| console.log(); | ||
|
|
||
| // Type alias for reusability | ||
| type TaggedTemplate = tags.MaxLength<20> & tags.Pattern<'^[a-zA-Z0-9_]+$'> & `prefix${string}postfix`; | ||
|
|
||
| // Test 1: typia.validate (main fix) | ||
| console.log("1. Testing typia.validate:"); | ||
| try { | ||
| const result1 = typia.validate<TaggedTemplate>(validValue); | ||
| console.log(" Valid input:", result1.success ? "PASS" : "FAIL"); | ||
|
|
||
| const result2 = typia.validate<TaggedTemplate>(invalidValue); | ||
| console.log(" Invalid input:", result2.success ? "FAIL (should reject)" : "PASS (correctly rejected)"); | ||
| if (!result2.success) { | ||
| console.log(" Rejection reasons:", result2.errors.map(e => e.expected)); | ||
| } | ||
| } catch (error) { | ||
| console.log(" Error:", error); | ||
| } | ||
| console.log(); | ||
|
|
||
| // Test 2: typia.assert | ||
| console.log("2. Testing typia.assert:"); | ||
| try { | ||
| const result1 = typia.assert<TaggedTemplate>(validValue); | ||
| console.log(" Valid input: PASS (no exception)"); | ||
| } catch (error) { | ||
| console.log(" Valid input: FAIL (unexpected exception):", error); | ||
| } | ||
|
|
||
| try { | ||
| const result2 = typia.assert<TaggedTemplate>(invalidValue); | ||
| console.log(" Invalid input: FAIL (should have thrown exception)"); | ||
| } catch (error) { | ||
| console.log(" Invalid input: PASS (correctly threw exception)"); | ||
| // Don't log the full error as it can be verbose | ||
| } | ||
| console.log(); | ||
|
|
||
| // Test 3: typia.json.schemas | ||
| console.log("3. Testing typia.json.schemas:"); | ||
| try { | ||
| const schemas = typia.json.schemas<[TaggedTemplate], "3.1">(); | ||
| const schema = schemas.components.schemas?.TaggedTemplate; | ||
|
|
||
| console.log(" Schema generated:", !!schema ? "PASS" : "FAIL"); | ||
| if (schema && typeof schema === 'object' && !Array.isArray(schema)) { | ||
| console.log(" Schema type:", (schema as any).type); | ||
| console.log(" Has pattern:", !!(schema as any).pattern); | ||
| console.log(" Has maxLength:", !!(schema as any).maxLength); | ||
| console.log(" MaxLength value:", (schema as any).maxLength); | ||
| console.log(" Pattern value:", (schema as any).pattern); | ||
| } | ||
| } catch (error) { | ||
| console.log(" Error:", error); | ||
| } | ||
| console.log(); | ||
|
|
||
| // Test 4: typia.is (type guard) | ||
| console.log("4. Testing typia.is:"); | ||
| try { | ||
| const result1 = typia.is<TaggedTemplate>(validValue); | ||
| console.log(" Valid input:", result1 ? "PASS" : "FAIL"); | ||
|
|
||
| const result2 = typia.is<TaggedTemplate>(invalidValue); | ||
| console.log(" Invalid input:", result2 ? "FAIL (should reject)" : "PASS (correctly rejected)"); | ||
| } catch (error) { | ||
| console.log(" Error:", error); | ||
| } | ||
| }; |
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.
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.
@copilot let's replace this with one single reduce loop.