Policies
SugoiJS provides policy that can be used for guarding any function.
Policies give you the ability to apply arguments check on a function with zero effort.
SugoiJS provides policy which can be used for guarding any function.
The Policies are used by two simple steps:
@Policy(policyId?:string)
This decorator registers the function as policy validator, which will later be used as guardian policy:
policyId?: string - This ID will be used as an alias for calling this function, default is ${class name}.${function name}
@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.
Predefined policies:
@sugoi\core provides predefined policy for validating function arguments:
failedResponseCode: number - The code will be stored under the exception if the value does not meet the criteria.
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 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