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

Close #560 - support custom validation tags #567

Merged
merged 1 commit into from Mar 31, 2023
Merged

Conversation

samchon
Copy link
Owner

@samchon samchon commented Mar 31, 2023

From now on, typia supports custom validation tags like below.

If you want to feel it earlier, run npm install --save typia@next command.

import typia from "typia";

export interface TagCustom {
    /**
     * Regular feature supported by typia
     *
     * @format uuid
     */
    id: string;

    /**
     * Custom feature composed with "$" + number
     *
     * @dollar
     */
    dolloar: string;

    /**
     * Custom feature composed with string + "abcd"
     *
     * @postfix abcd
     */
    postfix: string;

    /**
     * Custom feature meaning x^y
     *
     * @powerOf 10
     */
    log: number;
}

typia.addValidationTag("dollar")("string")(
    () => (value: string) =>
        value.startsWith("$") &&
        !isNaN(Number(value.substring(1).split(",").join(""))),
);

typia.addValidationTag("postfix")("string")(
    (text: string) => (value: string) => value.endsWith(text),
);

typia.addValidationTag("powerOf")("number")((text: string) => {
    const denominator: number = Math.log(Number(text));
    return (value: number) => {
        value = Math.log(value) / denominator;
        return value === Math.floor(value);
    };
});

@samchon samchon added the enhancement New feature or request label Mar 31, 2023
@samchon samchon self-assigned this Mar 31, 2023
@samchon samchon added this to In progress in v3.7 Update via automation Mar 31, 2023
Copy link
Owner Author

@samchon samchon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working well, but need to resolve #455 for detailed error reporting.

@samchon samchon merged commit 7aa335e into v3.7 Mar 31, 2023
2 of 3 checks passed
v3.7 Update automation moved this from In progress to Done Mar 31, 2023
@samchon samchon deleted the features/custom branch April 1, 2023 06:54
@samchon
Copy link
Owner Author

samchon commented Apr 1, 2023

Changed API spec like below (#569):

typia.customValidators.insert("powerOf")("number")(
    (text: string) => {
        const denominator: number = Math.log(Number(text));
        return (value: number) => {
            value = Math.log(value) / denominator;
            return value === Math.floor(value);
        };
    }
);
typia.customValidators.insert("dollar")("string")(
    () => (value: string) => value.startsWith("$"),
);

interface TagCustom {
   /**
    * @powerOf 10
    */
   powerOf: number;

   /**
    * @dollar
    */
   dollar: string;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

1 participant