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

Extraction of Sub-Validation Functions from Composite Typia Validation Functions #926

Open
wesselvdv opened this issue Jan 16, 2024 · 2 comments
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed question Further information is requested

Comments

@wesselvdv
Copy link

Feature Request

  • A description of the problem you're trying to solve:
    Currently, in Typia, when validating a composite object (like an object that conforms to an interface consisting of multiple nested interfaces), there is no way to extract or destructure individual validation functions for the nested interfaces.

  • An overview of the suggested solution:
    The proposed solution is to enhance Typia to allow for the extraction or destruction of individual validation functions from a transpiled function created for a composite interface. This feature would enable developers to extract the validation functions for the nested interfaces and use them independently, leading to more modular and efficient code.

  • Examples of how the suggestion would work in various places:

    • Code examples showing the expected behavior:

      import typia from "typia";
      
      interface Employee {
          // Employee fields
      }
      
      interface Product {
          // Product fields
      }
      
      interface EmployeeProductAssignment {
          employee: Employee;
          product: Product;
      }
      
      const isAssignment = typia.createIs<EmployeeProductAssignment>();
      
      // Proposed method to extract sub-validation functions
      const { isEmployee, isProduct } = isAssignment.destructure();
      
      const employee = { /* employee data */ };
      const product = { /* product data */ };
      
      // Now we can use these individual functions instead of regenerating them
      const isEmployeeValid = isEmployee(employee);
      const isProductValid = isProduct(product);

      In this example, isEmployee and isProduct are extracted from isEmployeeProductAssignment. These individual functions can be used to validate Employee and Product objects separately, enhancing the modularity and efficiency.

@samchon
Copy link
Owner

samchon commented Jan 17, 2024

No idea how to.

typia is generating nested validator functions per each object types. Your idea seems good, but I don't have idea how to declare the type definition. As you know, current typia.createIs() function returns just boolean returing function type. To accomplish this goal, need to make a special TMP type (IsFunction<T>) like below.

https://typia.io/playground/?script=JYWwDg9gTgLgBDAnmYBDANHA3g1BzAZzgF84AzKCEOAIiRVRoG4AoF4AOxgFMozUAxtzgBJALLcQAI17YWcBXGAATAFxwCMKJzxwAZLkIA6AGLQQqGAB4aAV1sqaAPlaK4k1MAA26zdo66BjD4BKbmljYe3s6uivjc6hy20rLybopBIUYAKsjcNg5cAMwATM5waen6hqFinKDJViUADE4VVQqZxgCiAB4CXrYEwABu3GKovQ0gVgCMza2xCmCoUNxc6uKSMlBwAD5wSV5eS3ACABbeymscmxIpUADaALqn5xBSUsDcBJsAEh8pIgXqxiGxODw+IJhCIAZ9EHI3BxUCAEhotDpQWwAPTYgC0BLxLFxcAAagBBAAyIgAIuTsiIAPIAOWJ+MJLHoaCMAjWlm4IgIVi2DycAAoAJSsIA

Of course, in the IsFunction type, it must have not only input is T returning function type, but also nested function types per each object types. In your example case, nested function isEmployee() and isProduct() must be automatically declared by the EmployeeProductAssignment<T> type. In my knowledge, it seems not possible in the current TypeScript spec, but not exact.

If this feature be possible, it would be really revolutionary feature for convenience. Will you research about it?

// CURRENT
export function createIs<T>(): (input: unknown): input is T;

// MUST BE
export function createIs<T>(): (input: unknown): IsFunction<T>;

// FOR EXAMPLE
type IsFunction<EmployeeProductAssignment> := ((input: unknown) => input is EmployeeProductAssignment) & {
  isEmployee: (input: unknown) => input is Employee;
  isProduct: (input: unknown) => input is Product;
};

@samchon samchon self-assigned this Jan 17, 2024
@samchon samchon added help wanted Extra attention is needed good first issue Good for newcomers question Further information is requested labels Jan 17, 2024
@wesselvdv
Copy link
Author

Yes, I see your point. I reckon this is not possible with Typescript atm. How about we make it bit more manual, so you can basically "extract" a validator:

import typia from "typia";

interface Employee {
    // Employee fields
}

interface Product {
    // Product fields
}

interface EmployeeProductAssignment {
    employee: Employee;
    product: Product;
}

const isAssignment = typia.createIs<EmployeeProductAssignment>();

// Proposed method to extract sub-validation functions
const { isEmployee, isProduct } = isAssignment.getValidators<{ isEmployee: Employee, isProduct: Product }>();

const employee = { /* employee data */ };
const product = { /* product data */ };

// Now we can use these individual functions instead of regenerating them
const isEmployeeValid = isEmployee(employee);
const isProductValid = isProduct(product);

This way you circumvent the need for "auto" typing all nested validators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants