import { registerDecorator , ValidationOptions } from 'class-validator' ; import * as moment from 'moment' ; export function validateRnippBirthdate ( value ) { if ( typeof value !== 'string' ) { return false ; } const isFullDate = moment ( value , 'YYYY-MM-DD' , true ) . isValid ( ) ; const isPresumeDay = moment ( value , 'YYYY-MM' , true ) . isValid ( ) ; const isPresumeMonth = moment ( value , 'YYYY' , true ) . isValid ( ) ; /**
     * Explanation on "présumés nés": @see https://www.legislation.cnav.fr/Pages/texte.aspx?Nom=CR_CN_2006013_07022006#1
     */ const isPresume = isPresumeDay || isPresumeMonth ; return isFullDate || isPresume ; } param validationOptions export function IsRnippBirthdate ( validationOptions ? ) { return ( object , propertyName ) => { registerDecorator ( { name : 'isRnippBirthdate' , target : object . constructor , propertyName : propertyName , constraints : [ ] , options : validationOptions , validator : { validate : validateRnippBirthdate , } , } ) ; } ; }