Validation of string template is not complete
📝 Summary
- Typia Version: 6.7.2, github head, playground
- Expected behavior: a string template should validate all its components
- Actual behavior: template components are not validated
In a string template containing multiple components, we expect all the components to be checked.
For example in
type A = string & tags.Pattern<"^[0-9a-fA-F]{32}$"> & tags.MinLength<32> & tags.MaxLength<32>
type B = `CHECK${A}`
we expect that a validation of type B would validate that it starts with CHECK and is followed by 32 hex characters.
However currently typia only generates validation for the first component of the template:
"string" === typeof input && RegExp(/^CHECK(.*)/).test(input)
A simpler example
type B = `${A}`
only tests
"string" === typeof input && RegExp(/(.*)/).test(input)
so it appears string templates do not inherit the components tests.
⏯ Playground Link
💻 Code demonstrating the bug
import typia, { tags } from 'typia'
type A = string & tags.Pattern<"^[0-9a-fA-F]{32}$"> & tags.MinLength<32> & tags.MaxLength<32>
type B = `CHECK${A}`
export const isB = typia.createIs<B>()
Don't know how to support that feature.
It's okay to challenge this issue by yourself.
As far as I understand, everything happens here: https://github.com/samchon/typia/blob/cbaa2e79b9bc6ca04b12888263052a4678103aec/src/programmers/internal/check_template.ts https://github.com/samchon/typia/blob/cbaa2e79b9bc6ca04b12888263052a4678103aec/src/programmers/internal/template_to_pattern.ts https://github.com/samchon/typia/blob/cbaa2e79b9bc6ca04b12888263052a4678103aec/src/programmers/internal/metadata_to_pattern.ts https://github.com/samchon/typia/blob/cbaa2e79b9bc6ca04b12888263052a4678103aec/src/utils/PatternUtil.ts
And here are the typia tags: MetadataTemplate["tags"] MetadataAtomic["tags"] MetadataConstantValue["tags"]
You need to create a regular expression for each typia tag. There are already expressions for strings, and the length is easy to check. For numbers, you either need to create very complex expressions or match the expression and process the results, which will be slower than the test. Also in the future, the list of tags will expand and it will be possible to use custom functions.
How about challenge to the regular expression enhancement?
How about challenge to the regular expression enhancement?
I don't understand.
It means I'm waiting your contribution.
But in what way? Just expressions are not enough here.
I've noticed the issue also happens when the tags are on the final (template) type:
type A = string
type B = `B${A}` & tags.Pattern<"^B\\d+$">
gives (Playground)
"string" === typeof input && RegExp(/^B(.*)/).test(input)
while one would have expected something along the lines of
"string" === typeof input && RegExp(/^B(.*)/).test(input) && RegExp(/^B\d+$/).test(input)
This seems more amenable to a fix as a first step. I'll see if I can figure something out from your hints @AlexRMU
No contribution for a long time, so close it.