Policies
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
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.
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:
Last updated