Last active
November 11, 2019 21:10
-
-
Save NarHakobyan/98653000d452d98aff6063d8107c7b02 to your computer and use it in GitHub Desktop.
take-if.validator.ts
This file contains hidden or 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
export function TakeIf<T>(func: (data: T) => boolean, validationOptions?: ValidationOptions) { | |
return (object: T, propertyName: string) => { | |
const args: ValidationMetadataArgs = { | |
validationOptions, | |
propertyName, | |
type: ValidationTypes.CONDITIONAL_VALIDATION, | |
target: object.constructor, | |
constraints: [(obj: any, _: any) => { | |
const isOptional = func(obj); | |
if (!isOptional) { | |
delete obj[propertyName]; | |
} | |
return isOptional; | |
}], | |
}; | |
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args)); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@TakeIf((data) => data.isActive === false)