typia icon indicating copy to clipboard operation
typia copied to clipboard

Validation of string template is not complete

Open shimaore opened this issue 1 year ago • 7 comments

📝 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

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>()

shimaore avatar Aug 06 '24 11:08 shimaore

Don't know how to support that feature.

It's okay to challenge this issue by yourself.

samchon avatar Aug 06 '24 13:08 samchon

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.

AlexRMU avatar Aug 08 '24 09:08 AlexRMU

How about challenge to the regular expression enhancement?

samchon avatar Aug 08 '24 11:08 samchon

How about challenge to the regular expression enhancement?

I don't understand.

AlexRMU avatar Aug 08 '24 11:08 AlexRMU

It means I'm waiting your contribution.

samchon avatar Aug 08 '24 11:08 samchon

But in what way? Just expressions are not enough here.

AlexRMU avatar Aug 08 '24 11:08 AlexRMU

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

shimaore avatar Aug 08 '24 12:08 shimaore

No contribution for a long time, so close it.

samchon avatar Dec 02 '24 17:12 samchon