Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8,192 changes: 3,733 additions & 4,459 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/Resolved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import { ValueOf } from "./typings/ValueOf";
*
* Before | After
* ------------------------|---------------------------------------- `Boolean` |
* `boolean` `Number` | `number` `BigInt` | `bigint` `String` | `string` `Class`
* `boolean` `Number` | `number` `BigInt` | `bigint` `String` | `string`
* `Class`
*
* | `interface` Native Class or Others | No change
*
* @author Jeongho Nam - https://github.com/samchon
Expand Down
6 changes: 3 additions & 3 deletions src/TypeGuardError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export namespace TypeGuardError {
* The name of the typia method that threw the error.
*
* @example
* typia.assert, "typia.assertEquals";
* (typia.assert, "typia.assertEquals");
*/
method: string;

Expand All @@ -179,15 +179,15 @@ export namespace TypeGuardError {
* (optional).
*
* @example
* input.age, "input.profile.email";
* (input.age, "input.profile.email");
*/
path?: undefined | string;

/**
* String representation of the expected type at the error location.
*
* @example
* string, "number & ExclusiveMinimum<19>";
* (string, "number & ExclusiveMinimum<19>");
*/
expected: string;

Expand Down
1 change: 1 addition & 0 deletions src/programmers/CheckerProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ export namespace CheckerProgrammer {
expression: props.config.atomist({
explore: props.explore,
entry: check_template({
context: props.context,
templates: props.metadata.templates,
input: props.input,
}),
Expand Down
1 change: 1 addition & 0 deletions src/programmers/internal/check_dynamic_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export const check_dynamic_key = (props: {
conditions.push(
atomist(
check_template({
context: props.context,
templates: props.metadata.templates,
input: props.input,
}),
Expand Down
41 changes: 39 additions & 2 deletions src/programmers/internal/check_template.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import ts from "typescript";

import { ExpressionFactory } from "../../factories/ExpressionFactory";

import { MetadataTemplate } from "../../schemas/metadata/MetadataTemplate";

import { ITypiaContext } from "../../transformers/ITypiaContext";

import { ICheckEntry } from "../helpers/ICheckEntry";
import { template_to_pattern } from "./template_to_pattern";

/** @internal */
export const check_template = (props: {
context: ITypiaContext;
templates: MetadataTemplate[];
input: ts.Expression;
}): ICheckEntry => {
// TYPEOF STRING & TAGS
// TYPEOF STRING
const conditions: ts.Expression[] = [
ts.factory.createStrictEquality(
ts.factory.createStringLiteral("string"),
Expand All @@ -37,10 +42,42 @@ export const check_template = (props: {
: internal.reduce((x, y) => ts.factory.createLogicalOr(x, y)),
);

// VALIDATION TAGS - handle validation tags attached to template literals
const validationConditions: ICheckEntry.ICondition[][] =
check_template_type_tags({
context: props.context,
templates: props.templates,
input: props.input,
});

// COMBINATION
return {
expression: conditions.reduce((x, y) => ts.factory.createLogicalAnd(x, y)),
conditions: [],
conditions: validationConditions,
expected: props.templates.map((tpl) => tpl.getName()).join(" | "),
};
};

/** @internal */
const check_template_type_tags = (props: {
context: ITypiaContext;
templates: MetadataTemplate[];
input: ts.Expression;
}): ICheckEntry.ICondition[][] => {
// Collect validation tags from all templates
const allTags = props.templates.flatMap((tpl) => tpl.tags);

return allTags
.map((row) => row.filter((tag) => !!tag.validate))
.filter((row) => !!row.length)
.map((row) =>
row.map((tag) => ({
Comment on lines +71 to +74
Copy link

@Mlocik97 Mlocik97 Oct 28, 2025

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.

expected: `string & ${tag.name}`,
expression: ExpressionFactory.transpile({
transformer: props.context.transformer,
importer: props.context.importer,
script: tag.validate!,
})(props.input),
})),
);
};
4 changes: 2 additions & 2 deletions src/programmers/json/JsonStringifyProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export namespace JsonStringifyProgrammer {
if (props.metadata.templates.length)
if (AtomicPredicator.template(props.metadata)) {
const partial = Metadata.initialize();
partial.atomics.push(
(partial.atomics.push(
MetadataAtomic.create({ type: "string", tags: [] }),
),
unions.push({
Expand All @@ -278,7 +278,7 @@ export namespace JsonStringifyProgrammer {
...props,
type: "string",
}),
});
}));
}

// CONSTANTS
Expand Down
2 changes: 1 addition & 1 deletion src/tags/JsonSchemaPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { TagBase } from "./TagBase";
* "x-format": "currency",
* "x-example": 19.99
* }>;
* ```
* ```;
*
* @template Schema - Object containing custom properties to add to the JSON
* Schema
Expand Down
2 changes: 1 addition & 1 deletion src/tags/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { TagBase } from "./TagBase";
*
* // Generate Protocol Buffer message
* const message = typia.protobuf.message<User>();
* ```
* ```;
*
* @template N - Field number (positive integer from 1 to 536,870,911, excluding
* 19000-19999)
Expand Down
2 changes: 1 addition & 1 deletion src/tags/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { TagBase } from "./TagBase";
* type UserId = number & Type<"uint32">; // 0 to 4,294,967,295
* type FileSize = bigint & Type<"int64">; // Large file sizes
* type Coordinate = number & Type<"double">; // High precision coordinates
* ```
* ```;
*
* @template Value - The numeric type representation
*/
Expand Down
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)");
};
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);
}
};
Loading
Loading