A ValSan is a class-based validator and sanitizer that processes input through three phases: normalize → validate → sanitize.
import { ValSan, ValidationResult } from 'valsan';
class MyValSan extends ValSan<string, string> {
override example = 'look_no_spaces';
override rules() {
return {
word_has_spaces: {
code: 'word_has_spaces',
user: {
helperText: 'No spaces',
errorMessage: 'No spaces allowed',
},
},
};
}
async normalize(input: string) {
return input?.trim().toLowerCase();
}
async validate(input: string) {
if (input.includes(' ')) {
return this.fail([this.rules().word_has_spaces]);
}
return this.pass();
}
async sanitize(input: string) {
return input;
}
}- Use
Sanitizerfor pure transformations (e.g.TrimSanitizer) - Use
Validatorfor pure validation (e.g.MinLengthValidator) - Use
ValSanfor classes that both validate and transform (e.g.StringToNumberValSan)
When creating new primitives, follow these guidelines:
- Choose the right postfix based on the primary behavior
- Be specific in the prefix (e.g.,
MinLengthnot justLength) - Export with the same name as the class
- Document the behavior clearly in JSDoc comments
- Add an example by overriding the
exampleproperty