Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read parent object in custom tag #991

Closed
sergio-milu opened this issue Mar 5, 2024 · 8 comments
Closed

Read parent object in custom tag #991

sergio-milu opened this issue Mar 5, 2024 · 8 comments
Assignees
Labels
invalid This doesn't seem right question Further information is requested wontfix This will not be worked on

Comments

@sergio-milu
Copy link

sergio-milu commented Mar 5, 2024

Hi, I'm trying to read the parent object (input) in a custom validation tag.

I think I found a workaround for it, using input without the $ as prefix, but this works right now, maybe not in the future if you change that variable name. Is this possible right now? I think this is useful, maybe the validate callback could receive the parent object as arg.

Current

type TestFeature<
  Value extends 'test'
> = typia.tags.TagBase<{
  kind: 'hasEHRFeature';
  target: 'string';
  value: Value;
  validate: `(function () {
  const hasFeature = require('@utils').hasFeature;
  return hasFeature(input.system, '${Value}') && typeof $input === 'string'
})()`;
}>; 

Proposal

type TestFeature<
  Value extends 'test'
> = typia.tags.TagBase<{
  kind: 'hasEHRFeature';
  target: 'string';
  value: Value;
  validate: `(function (input /* AS ARG HERE */ ) {
  const hasFeature = require('@utils').hasFeature;
  return hasFeature(input.system, '${Value}') && typeof $input === 'string'
})()`;
}>; 

thanks

@samchon
Copy link
Owner

samchon commented Mar 5, 2024

Not possible to get parent object info.

If it requires, it is out of validation library scope.

@samchon samchon self-assigned this Mar 5, 2024
@samchon samchon added the wontfix This will not be worked on label Mar 5, 2024
@sergio-milu
Copy link
Author

sergio-milu commented Mar 5, 2024

Not possible to get parent object info.

If it requires, it is out of validation library scope.

well, actually it is possible, because that code is working and I'm reading parent object info.

I'm asking this cause this feature exists in nestjs/swagger and IMO is useful, so why should it be out of the scope of the validation library something that is working right now (even is not documented) and is useful for validation purposes (validate some field based on another field)?

@samchon
Copy link
Owner

samchon commented Mar 5, 2024

The parent object, it is just your domestic issue. The parent can be sometimes array, sometimes tuple, and even possible to does not exist due to standalone utilization.

I do not have plan to support such private and type unsafe story. If you need to solve your domain problem, then you have to do it by yourself.

@samchon
Copy link
Owner

samchon commented Mar 5, 2024

Also, the reason why you could utilize the input variable of object, it is due to transformed JS code is using the variable name for each object type. It is free that you are utilizing it, but I do not have plan to introduce it as a regular feature.

For reference, if some object type is extremely simple, it would be inlined, so that input variable may not mean the actual parent object.

@samchon samchon added invalid This doesn't seem right question Further information is requested labels Mar 5, 2024
@sergio-milu
Copy link
Author

so, with typia there's no way to validate one property based in another one at same level?

interface Test {
    protocol: string;
    port: number & Protocol<'protocol'>
}

in this contrived exampled, validated that port is 443 is protocol is https, could this be achieved?

@samchon
Copy link
Owner

samchon commented Mar 5, 2024

Not possible.

@samchon samchon closed this as not planned Won't fix, can't repro, duplicate, stale Mar 5, 2024
@sergio-milu
Copy link
Author

sergio-milu commented Mar 6, 2024

Not possible.

and why custom tags only supports this values Target extends "bigint" | "number" | "string" | "array",?

my questions is I can use an array of objects, but not a plain object, os there any special reason behind that?

@samchon
Copy link
Owner

samchon commented Mar 6, 2024

In the object case, it actually defined as actual property type in the compiler API.

I can separate it manually, but did not it due to no use case yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants