Pre-defined polices
Ready to use policies

Function argument validation

@sugoi\core comes with pre-defined policy for validating functions arguments:
1
ValidateSchemaPolicy(failedResponseCode: number = 400, ...policyMeta: TValidateSchemaMeta[])
Copied!
failedResponseCode: number - The code will be stored under the exception if the value does not meet the criteria.
policyMeta: TValidateSchemaMeta - Meta data for validation.
1
{
2
schema: {[prop:string]:ComparableSchema}|ComparableSchema,
3
argIndex?: number, - The argument index which need to validate - default is 0
4
keyInArg?: string - Key in argument (optional)
5
}
Copied!

Example:

Schema -

1
{
2
role:{
3
text:string//with regex /([A-Z])+/i
4
}
5
}
Copied!

Usage -

1
@ValidateSchemaPolicy(400, {
2
schema: {
3
"role": ComparableSchema.ofType(
4
{text: ComparableSchema.ofType(SchemaTypes.STRING).setRegex("([A-Z])+", "i")}
5
)
6
},
7
argIndex: 0
8
})
Copied!

Result:

In case the argument doesn't meet the criteria an exception will thrown with the given failed response code
Last modified 3yr ago