Policies
SugoiJS provides policy which can be use for guarding any function.
Policies gives you the ability to apply argument check on a function with zero effort.
SugoiJS provides policy which can be use for guarding any function.
The Policies use by two simple steps:
@Policy(policyId?:string)
This decorator register the function as policy validator, which will later be used as guardian policy.
policyId?: string - The id which will be use as an alias for calling this function, default is ${class name}.${function name}
@UsePolicy(policy: TPolicy|string, failedResponseCode: number = 400, ...policyMeta: any[])
This decorator set a policy guard on the function it decorated.
policy:TPolicy| string - For setting the refereed policy, use the policy Id from previous section nor anonymous function reference.
failedResponseCode: number - The code which will be under the exception in case the value does not meet the criterias.
policyMeta: any[] - Any further payload data which should pass to the policy.
Pre-defined policies:
@sugoi\core provide pre-defined policy for validating function arguments:
failedResponseCode: number - The code which will be under the exception in case the value does not meet the criterias.
policyMeta: TValidateSchemaMeta - Meta data for validation
Example: Schema -
Usage -
Build your own policies:
Policy can be any function of type TPolicy
TPolicy = (policyData?:{functionArgs: any[], policyMeta: any[]})=>(Promise < (true|any) > | (true|any))
When result is boolean true
means the data is valid, all the other values will be shown on the exception
Policy full example:
Last updated