typia icon indicating copy to clipboard operation
typia copied to clipboard

Loosening restrictions on tuple types "Array contains Val"

Open douglasg14b opened this issue 1 year ago • 4 comments

I have read through the docs before coming here, if I missed something important my apologies

Question

I need to validate a tuple type: [Status] where:

  1. the actual array only need to contain a Status anywhere within the array regardless of it's index.
  2. The array must have a length > 1
  3. There can be as many or no elements before of after Status

Essentially Must contain a Status, anywhere is the validation requirement. Which sounds deceptively simple, but is turning out to be rather difficult to actually get generated.

I'm struggling to do this with typia:

  • a Status[] is an arrya of only Status
  • A [...unknown[], Status] is an array with Status as the last element
  • A [...unknown[], Status, ...unknown[]] is an invalid type
  • [...unknown[], Status] | [Status, ...unknown[]
    • Doesn't produce a correct validator: ['stuff', 'SUCCESS'] fails validation
    • tuplePredicators expect the array to be of length 1 or 2 only. And only expects a Status at [0]?
  • A string[] | [Status] is a valid type but results in nonsensible intersection
  • A Array<Status> & Array<unknown> results in nonsensible intersection (This wouldn't make sense for typing anyways)

How do I do this?

I do recognize the asinine-ness of this value, but... legacy system modeling

douglasg14b avatar Sep 26 '24 01:09 douglasg14b

Also interestingly:

// This will generate
export type InArray<TVal> =
    | [TVal]
    | [unknown, TVal]
    | [TVal, unknown];

// This will fail with: TypeError: Cannot read properties of undefined (reading 'any')
export type InArray<TVal> =
    | [TVal]
    | [unknown, TVal]
    | [TVal, unknown]
    | [TVal, unknown, unknown]
    | [unknown, TVal, unknown]
    | [unknown, unknown, TVal];

douglasg14b avatar Sep 26 '24 01:09 douglasg14b

Also interestingly:

// This will generate
export type InArray<TVal> =
    | [TVal]
    | [unknown, TVal]
    | [TVal, unknown];

// This will fail with: TypeError: Cannot read properties of undefined (reading 'any')
export type InArray<TVal> =
    | [TVal]
    | [unknown, TVal]
    | [TVal, unknown]
    | [TVal, unknown, unknown]
    | [unknown, TVal, unknown]
    | [unknown, unknown, TVal];

I'm testing on it but error is not occured. Which value occurs the error?

https://typia.io/playground/?script=JYWwDg9gTgLgBDAnmYBDOAzKERwERIqp4DcAUGYQKZwAqAaqgDZwC8cAdgK4gBGVUclQAekWAmQ0AkhwCCUKKkRsycNXAA+cANoNmAXVXqt2rhwDWHCAHcOAGjqMmh9Zp16mDs5ZscXx9ycvCytbYJ9bfzUTb1D7R2ZwuKi3UxDfJIyE53IyAGMIDgBneGAitgkiADo8qCpUGCopIoAeGXlFRAA+AAoASnIC4ogmKiqmCABzHqMynu08Qqo8BwAmBwBmfT67Mj6gA

samchon avatar Sep 26 '24 02:09 samchon

About the rest elements type in the first or middle position in the tuple, I haven't known the spec.

By the way, I may need more investigation times because it is actually not possible in the JSON schema specification.

If neccessary, I may prohibit the type due to the JSON schema reason.

samchon avatar Sep 26 '24 02:09 samchon

Thanks for the reply @samchon !! I really appreciate it ^_^

Here's the stacktrace/error for the above:

> pnpm typia generate --input ./src/validators/templates --output ./src/validators/generated --project tsconfig.json

----------------------------------------
 Typia Generate Wizard
----------------------------------------
TypeError: Cannot read properties of undefined (reading 'any')
    at Metadata.covers (/home/douglas/programming/[redacted]/node_modules/.pnpm/[email protected][email protected]/node_modules/typia/lib/schemas/metadata/Metadata.js:371:20)
    at /home/douglas/programming/[redacted]/node_modules/.pnpm/[email protected][email protected]/node_modules/typia/lib/schemas/metadata/Metadata.js:415:75
    at Array.every (<anonymous>)
    at /home/douglas/programming/[redacted]/node_modules/.pnpm/[email protected][email protected]/node_modules/typia/lib/schemas/metadata/Metadata.js:415:34
    at Array.some (<anonymous>)
    at _loop_4 (/home/douglas/programming/[redacted]/node_modules/.pnpm/[email protected][email protected]/node_modules/typia/lib/schemas/metadata/Metadata.js:411:30)
    at Metadata.covers (/home/douglas/programming/[redacted]/node_modules/.pnpm/[email protected][email protected]/node_modules/typia/lib/schemas/metadata/Metadata.js:423:35)
    at /home/douglas/programming/[redacted]/node_modules/.pnpm/[email protected][email protected]/node_modules/typia/lib/factories/internal/metadata/iterate_metadata_sort.js:146:48
    at Array.sort (<anonymous>)
    at /home/douglas/programming/[redacted]/node_modules/.pnpm/[email protected][email protected]/node_modules/typia/lib/factories/internal/metadata/iterate_metadata_sort.js:141:29

douglasg14b avatar Sep 26 '24 13:09 douglasg14b