Validation and Required fields

Last updated 5 months ago

Model validation done right

Validation in the lifecycle

if we will take a look again on the update\save lifecycle we will be able to see validation is the 2nd step which occurs.

SugoiJS save and update lifecycle hooks

Handling model validation

1. validate() function (IValidate interface)

If validate function is declared for the model it will get called, the response should be Promise of true or any other value, while true approve the model is valid.

Interface: validate():Promise<true|any>

2. @Required() decorator

If property is decorate with @Required() it will be checked during the validation process.

By default @Required check the value of the property isn't null or empty string.


For allowing empty strings pass the value true as argument, this will set the allowEmptyString argument. For disallow empty string pass false(default).

For example

public body:string;

@Required(condition: ComparableValueType)

For defining the property schema you able to pass ComparableValueType as property.

For example:

public post;

Required API

Get all required fields:

public getMandatoryFields()

Get instance required fields:

public getInstanceMandatoryFields()

Add field to required fields:

public addMandatoryField(field: string, condition?: TComparableSchema);
public addMandatoryField(field: string, allowEmptyString?: boolean);
public addMandatoryField(field: string, condition?: boolean | TComparableSchema)

Remove field from required fields:

public removeMandatoryFields(...fields:string[])

Toggling required fields validation

For toggle the required fields validation on and off you can use:

Instance method

public skipRequiredFieldsValidation(shouldSkip: boolean):void

Query options


This option later will be use for the query:

await User.updateById(id,{name:"testing"},QueryOptions.builder().setSkipRequiredValidation(true));