-
Notifications
You must be signed in to change notification settings - Fork 10
CON-1508: Decorator based arguments validation approach #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export function IsNotEmpty( | ||
target: any, | ||
methodName: string, | ||
parameterIndex: number | ||
) { | ||
if (!target?.validations) { | ||
target.validations = []; | ||
} | ||
|
||
target.validations.push(`${methodName}:${parameterIndex}:${IsNotEmpty.name}`); | ||
} | ||
|
||
function IsNotEmptyValidate( | ||
methodName: string, | ||
argumentIndex: number, | ||
argumentValue: any | ||
) { | ||
if (argumentValue === '' || argumentValue === null || argumentValue === undefined) { | ||
throw new Error(`Invalid empty argument at index ${argumentIndex} with "${argumentValue}" value in "${methodName}" method`); | ||
} | ||
} | ||
Comment on lines
+13
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actual validation logic |
||
|
||
export function Validate(target: any, propertyName: string, descriptor: TypedPropertyDescriptor<Function>) { | ||
let method = descriptor.value!; | ||
|
||
descriptor.value = function (...args: Array<any>) { | ||
if (target?.validations?.length) { | ||
for (const validation of target.validations) { | ||
const [methodName, argumentIndex, validatorName] = validation.split(":"); | ||
|
||
if (method.name === methodName) { | ||
switch (validatorName) { | ||
case IsNotEmpty.name: | ||
const argumentValue = args[argumentIndex]; | ||
|
||
IsNotEmptyValidate(methodName, argumentIndex, argumentValue); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
Comment on lines
+28
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Walk through registered validations and execute them |
||
} | ||
|
||
return method.apply(this, arguments); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { SmartlingBaseApi } from "../base/index"; | |
import { SmartlingAuthApi } from "../auth/index"; | ||
import { Logger } from "../logger"; | ||
import { ProjectDto } from "./dto/project-dto"; | ||
import { IsNotEmpty, Validate } from "../decorators"; | ||
|
||
export class SmartlingProjectsApi extends SmartlingBaseApi { | ||
constructor(smartlingApiBaseUrl: string, authApi: SmartlingAuthApi, logger: Logger) { | ||
|
@@ -10,7 +11,8 @@ export class SmartlingProjectsApi extends SmartlingBaseApi { | |
this.entrypoint = `${smartlingApiBaseUrl}/projects-api/v2/projects`; | ||
} | ||
|
||
async getProjectDetails(projectId: string): Promise<ProjectDto> { | ||
@Validate | ||
async getProjectDetails(@IsNotEmpty projectId: string): Promise<ProjectDto> { | ||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dimitrystd it's just an example of how it can be used (SUBM api class is in private sdk) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks interesting. Just two concerns:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would need to refactor here and there to support |
||
return await this.makeRequest( | ||
"get", | ||
`${this.entrypoint}/${projectId}` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registers validation for argument (it's not possible to get value of the argument in an argument decorator which is weird)