typia icon indicating copy to clipboard operation
typia copied to clipboard

Validation base on dynamic type

Open machendos opened this issue 11 months ago • 6 comments

I'm retrieving hash maps from Redis in my app. In order to validate value I utilize model map that includes all possible keys and values in Redis. So after retrieving value I want to call validateEquals function and pass type that I recieved dynamically in runtime but in this scenario Typia just never return errors even though they do exist. This flow was working fine with class-validatior but now we decided to go with Typia instead but can't implement this part of functionality. I provide simple code snippet that will show the problem:

enum ModelName {
  A = 'A',
  B = 'B',
}

class ModelA {
  fieldA: number;
}

class ModelB {
  fieldB: number;
}

const ModelSchema: { [key in ModelName]: new (...args: any[]) => any } = {
  [CacheIdentifierPrefix.A]: ModelA,
  [CacheIdentifierPrefix.B]: ModelB,
};

const clientFunction = () => {
  validateFunction(ModelName.A, { wrongValue: 1 });
};

const validateFunction = (modelName: ModelName, value: any) => {
  const model = ModelSchema[modelName];
  console.log(validateEquals<typeof model>(value));
};

clientFunction();

In this case Typia is returning no errors: { success: true, errors: [], data: { wrongValue: 1 } }

machendos avatar Mar 22 '24 05:03 machendos

I can't test your type because do not have the CacheIdentifierPrefix type.

Can you provide me a reproducible repo?

samchon avatar Mar 22 '24 07:03 samchon

Hi, CacheIdentifierPrefix is just ModelName. Sorry for confusing

machendos avatar Mar 24 '24 23:03 machendos

Still not possible to understand what you want.

Is this what you want?

If not, hope to provide the exact reproducible code.

https://typia.io/playground/?script=JYWwDg9gTgLgBDAnmYBDOAzKERwERIqp4DcAUGQKYB2ArrgMKoDGAFpQJIAmNMwGwSlAAKUSgIAecAN5k4cAIJwAvPgV4ANHLgAhFfh2ayAXwo16cALIQeAGwByqEJRnalqgOQKPW+Xs86PiYUzLaoAM7hVjaUtkqy8gKxXAoAhABccHQgAEZC5KZkoRFR1nZ6CZiCtlw6GVn0eVAFFIQuAGIsMNCIADwAKgB8+pXUlADuABQAdLOoUADm4Zmo1IgA2gC6AJSZ-S1FENTh8GWxAMpslCComdJw6wDWlIhwwNTRdo7Om5mdzN0oH1VohhsYRtp1kwrtxePxBCIxJJpgpfp9YgpfA9oexYdQ+ElEeJgBJpjo0WdbDotMZyIdjvAAG6oWzALioGCUdq0agA4BHfSTEAxBxOSiZSnfSgaODM2y0cVwEHbFTDSrMI4nODCuz6SmXdg3dY62JSzbkeQa44QWyUaa2CALSaENDTOVsjmUACiAEdaCzwr02hAMNqRYNJnKFdttgUSEA

samchon avatar Mar 25 '24 03:03 samchon

Something like this – https://typia.io/playground/?script=JYWwDg9gTgLgBDAnmYBDOAzKERwERIqp4DcAUGQKYB2ArrgLIQAmlANgHKoiVwDeZOHACCcALxwA5MMkAaQXABC4qYrlkAvhQDGbVAGd9cJqzaiBQjMHbNhAQgBccOiABGlKOS1ldBoyfZlC0xrNmZFR2d6d09NCkJeADFUbRhoRAAeABUAPhVg6koAdwAKADoK1CgAc30nVGpEAG0AXQBKJyyvHQhqfXgAtgBlbQALShBUJz44JoBrSkQ4YGpjFnYuHhanZNT0jIbEPI18hSbBzcoy4W2102F5IXP1zm4rxVvBxXkNch9e-pwABuqDYwGYqBglEStGoqWAvRUJRAL0uTgub1kwNBtEo9UabXEeWC2gB8BRphUgxG40mTQpGzeLXIQlJfQgbCubAg1RKhDQZRBYIhUIAogBHWig-QZACSfRgDW0lCyyEoGQSEAwcAZbByORKQtxbTaXj+QvBkOhsPhvRKGJ411kfCmcAAjBo2kA

We have two types (ModelA and ModelB). And we have generic function validateFunction, that has to take on input class (or any identifier of class) as well as value to validate. And then we want to validate those value using type that was passed into the function

machendos avatar Mar 25 '24 21:03 machendos

Well, in your type, the ModelSchema[modelName] becomes any type.

By the way, if what you want is the class type checking statement represented by instanceof, typia is not performing it. typia does not distinguish class type, but just validates as pritmitive objects.

The class type checking feature would come with "class transformation" feature, but cannot sure when.

samchon avatar Mar 26 '24 10:03 samchon

No I don't need class type checking(When I validate classes itself). I need check if value that I dynamically provided to the function is satisfy corresponding class definition. So it doesn't necessarily have to be instance of those class. But if class, for instance, have field field1 I want to make sure that value that I passed into also do have this field. In other words, instance has to be implementation of this class. It was easy to do in class-validator, where I was able to pass type(class) that I need to validate for dynamically as function argument

machendos avatar Mar 26 '24 15:03 machendos