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

How to use custom validations in encore.ts #1646

Open
criskell opened this issue Dec 11, 2024 · 3 comments
Open

How to use custom validations in encore.ts #1646

criskell opened this issue Dec 11, 2024 · 3 comments

Comments

@criskell
Copy link

How can I use my own validation rules based on the ideas in this PR? #1621

What do you think about this way?

declare const customValidator: (input: string) => boolean;

type X = {
  y: Min<6>;
  z: CustomRule<typeof customValidator>;
};
@marcuskohlberg
Copy link
Member

Hey, great catch we had missed publishing the docs for this new feature. Fixed now.
Here you go: https://encore.dev/docs/ts/primitives/validation#value-based-validation-rules

@criskell
Copy link
Author

criskell commented Jan 7, 2025

@marcuskohlberg Actually, I was talking about custom validations, where I can pass a custom function at the type level, when the existing validations are not suitable for my use case.

For example, in the deepkit framework this is possible:
https://github.com/deepkit/deepkit-framework/blob/fb1263dbd9fbaae0bd9ecbf40f840698e4523e0c/website/src/pages/documentation/runtime-types/validation.md?plain=1#L316-L329

@marcuskohlberg marcuskohlberg reopened this Jan 8, 2025
@marcuskohlberg
Copy link
Member

Ah, makes sense. You can create custom validation by composing the built-in rules in the type definition: https://encore.dev/docs/ts/primitives/validation#combining-rules

For custom rules beyond what's possible using the built-in rules, you can do this yourself inside the endpoint handler.

For context, is there a use case where the built-in rules aren't sufficient?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants