-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c184bc9
commit 0e5ee18
Showing
4 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { IsNotEmpty, ValidationError, isInt, isString, validateSync } from 'class-validator'; | ||
import { OneOf } from './one-of.validator.decorator.js'; | ||
|
||
describe('OneOf decorator', () => { | ||
class Test { | ||
@OneOf(isInt, isString) | ||
@IsNotEmpty() | ||
public test?: number | string; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
public constructor(test: any) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
this.test = test; | ||
} | ||
} | ||
|
||
it('should validate int', () => { | ||
const errors: ValidationError[] = validateSync(new Test(5)); | ||
|
||
expect(errors.length).toBe(0); | ||
}); | ||
|
||
it('should validate string', () => { | ||
const errors: ValidationError[] = validateSync(new Test('TEST')); | ||
|
||
expect(errors.length).toBe(0); | ||
}); | ||
|
||
it('should fail on boolean', () => { | ||
const errors: ValidationError[] = validateSync(new Test(true)); | ||
|
||
expect(errors.length).toBeGreaterThan(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ValidateBy, buildMessage } from 'class-validator'; | ||
|
||
type Predicate<T = unknown> = (val: unknown) => val is T; | ||
|
||
/** | ||
* class-validator decorator that tries to apply predicates until one matches | ||
* | ||
* @example | ||
* @OneOf(isInt, isString) | ||
* public intOrString: number | string; | ||
*/ | ||
export function OneOf(...predicates: Predicate[]): PropertyDecorator { | ||
return ValidateBy({ | ||
name: 'one-of-' + predicates.map((p: Predicate) => p.name).join('-'), | ||
validator: { | ||
validate: (value: unknown) => predicates.some((p: Predicate) => p(value)), | ||
defaultMessage: buildMessage( | ||
(eachPrefix: string) => | ||
eachPrefix + '$property must match one of ' + predicates.map((p: Predicate) => p.name).join(), | ||
), | ||
}, | ||
}); | ||
} |