@the-neon/validation
v0.0.14
Published
Validation support for Neon using decorators
Readme
@the-neon/validation
Method argument validation for Neon using decorators.
Install
npm install @the-neon/validationRequires @the-neon/core as a peer dependency.
Usage
import { Validate, Validator } from "@the-neon/validation";Validators
Built-in Validators
Validator.email— Validates email formatValidator.notEmpty— Validates not null/undefined/empty stringValidator.uuid— Validates UUID formatValidator.greaterThanZero— Validates number > 0
Example
class UserService {
@Validate({
email: Validator.email,
name: Validator.notEmpty,
})
async createUser(email: string, name: string) {
// validation runs before the method
}
}
// Throws InputError if validation fails
await userService.createUser("invalid-email", "");Custom Validation Functions
import { Validate, Validator } from "@the-neon/validation";
const customValidator = (value: any) => {
if (!isValid(value)) {
throw new Error("Custom validation failed");
}
};
class MyService {
@Validate({
data: customValidator,
})
async process(data: any) {
// ...
}
}API
@Validate
Decorator that validates method arguments:
@Validate(validators: {
[paramName: string]: Validator | Validator[] | ValidationFunction | ValidationFunction[];
}, functions?: ValidationFunction | ValidationFunction[])Validator
Enum of built-in validators:
Validator.email
Validator.notEmpty
Validator.uuid
Validator.greaterThanZero