-
-
Notifications
You must be signed in to change notification settings - Fork 191
Open
Labels
bugSomething isn't workingSomething isn't workingenhancementNew feature or requestNew feature or request
Description
typia string tags works only on strings (except template literal types) !
(validate, assert, json.schemas - failed on template literal types)
import typia, { tags } from 'typia';
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);
}
}
const testValue = 'prefix;"+,df0123456789postfix' as const;
logResult(
'Test1Type',
typia.validate<
tags.MaxLength<10> &
tags.Pattern<'^[a-zA-Z0-9_]+$'> &
`prefix${string}postfix`
>(testValue),
);
logResult(
'Test2Type',
typia.validate<tags.MaxLength<10> & tags.Pattern<'^[a-zA-Z0-9_]+$'> & string>(
testValue,
),
);
logResult(
'Test3Type',
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),
);
logResult(
'Test4Type',
typia.validate<`prefix${tags.MaxLength<10> &
tags.Pattern<'^[a-zA-Z0-9_]+$'> &
string}postfix`>(testValue),
);=>
Test1Type checking =>
Successfully validated.
Test2Type checking =>
Failed!
[
{
path: '$input',
expected: 'string & MaxLength<10>',
value: 'prefix;"+,df0123456789postfix'
}
]
Test3Type checking =>
Successfully validated.
Test4Type checking =>
Successfully validated.=>
so have to change template literal type to string & pattern:
before:
tags.MaxLength<10> &
tags.Pattern<'^[a-zA-Z0-9_]+$'> &
`prefix${string}postfix`after:
tags.MaxLength<10> &
tags.Pattern<'^[a-zA-Z0-9_]+$'> &
tags.Pattern<'^prefix.+postfix$'> &
stringbut in this way certain advantages of compile-time checks are lost
Copilot
Metadata
Metadata
Labels
bugSomething isn't workingSomething isn't workingenhancementNew feature or requestNew feature or request