Created
February 6, 2020 14:43
-
-
Save kanian/8ae7486780dc63384598393f5ca474c0 to your computer and use it in GitHub Desktop.
Retrieves existing constructor parameters metadata and validates the given argument against their respective parameter schema
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
function validateConstructorParams(target: any, args: any[]) { | |
// Retrieve all constructor parameters metadata | |
let existingConstrainedParameters: ConstrainedParameterMap = Reflect.getOwnMetadata( | |
parameterSchemaMetadataKey, | |
target, | |
"constructor" | |
); | |
// For each retrieved metadata,... | |
if (existingConstrainedParameters) { | |
const parameterIndexes = Object.keys(existingConstrainedParameters); | |
for (let parameterIndex of parameterIndexes) { | |
// Validate the argument at a given index against the relevant schema | |
const { error } = existingConstrainedParameters[parameterIndex].validate( | |
args[Number.parseInt(parameterIndex)] | |
); | |
// Bail out with an error if validation fails | |
if (error instanceof Error) { | |
throw error; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment