Open wesselvdv opened 7 months ago
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.
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;
};
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.
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:
interface Employee { // Employee fields }
interface Product { // Product fields }
interface EmployeeProductAssignment { employee: Employee; product: Product; }
const isAssignment = typia.createIs();
// 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);