Policies (Filters)
SugoiJS provides policy that can be used for guarding any function.

Guard your methods

Policies gives you the ability to apply arguments check on a function with zero effort.
SugoiJS provides policy which can be use for guarding any method.
The Policies are used by two simple steps:

Decorate your verification method with - @Policy(policyId?:string)

This decorator registers the function as "policy validator", this will later be used as guardian middleware.
policyId?: string - This ID will be used as an alias for calling this function, default is ${class name}.${function name}

Example

1
@Policy() //register this function as policy using the class name and function name, same as use @Policy("myNumberValidation")
2
static myNumberValidation(
3
policyData:{functionArgs: any[],
4
policyMeta: Array<{argIndexToValidate:number,maxValue:number}>
5
}): true|any{
6
7
// Those are the meta data values
8
// which passed into the decorator itself while using @UsePolicy()
9
const argIndexToValidate = policyMeta[0].argIndexToValidate;
10
const maxValue = policyMeta[0].maxValue;
11
12
if(policyData.functionArgs[argIndexToValidate] < maxValue){
13
return true; //Is valid, continue to the function/next policy or middleware
14
}else{
15
return policyData.functionArgs[argToValidate]; // so we will be able to identify the issue on the exception
16
}
17
}
Copied!
2. @UsePolicy(policy: TPolicy|string, failedResponseCode: number = 400, ...policyMeta: any[])
This decorator sets a policy guard on the function it decorates:
policy:TPolicy| string - For setting the referenced policy, use the policy ID from the previous section or anonymous function reference.
failedResponseCode: number - The code will be stored under the exception if the value does not meet the criteria.
policyMeta: any[] - Any further payload data which should pass to the policy.
1
2
/**
3
* Apply policy by anonymous function
4
* only numbers lower than 5 will log
5
**/
6
@UsePolicy((myNumber: number)=> myNumber < 5)
7
lowerThan5NumberLogger(myNumber){
8
console.log(`number is lower the 5! ${myNumber}`);
9
}
Copied!

Build your own policies:

Policy can be any function of type TPolicy
TPolicy = (policyData?:{functionArgs: any[], policyMeta: any[]})=>(Promise < (true|any) > | (true|any))
When the result is boolean, true means that the data is valid, all other values will be shown on the exception

Policy full example:

1
class Validators{
2
3
@Policy() //register this function as policy using the class name and function name, same as use @Policy("Validators.myNumberValidation")
4
static myNumberValidation(policyData:{functionArgs: any[], policyMeta: {argIndexToValidate:number,maxValue:number}[]}): true|any{
5
const myMeta = policyMeta[0];
6
//those are the meta data values which passed to the decorator itself while using @UsePolicy()
7
const argIndexToValidate = myMeta.argIndexToValidate;
8
const maxValue = myMeta.maxValue;
9
10
if(policyData.functionArgs[argIndexToValidate] < maxValue){
11
return true; //Is valid, continue to the function/next policy
12
}else{
13
return policyData.functionArgs[argToValidate]; //so we will be able to identify the issue on the exception
14
}
15
}
16
}
17
18
@UsePolicy("Validators.myNumberValidation",{argIndexToValidate:0,maxValue:5})
19
lowerThan5NumberLogger(myNumber){
20
console.log(`number is lower the 5! ${myNumber}`);
21
}
Copied!
Last modified 2yr ago