You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The validator has public methods for asynchronous validation (validate and validateOrReject) and for synchronous validation (validateSync).
Unfortunately the synchronous method ignores all async validations while the asynchronous methods always return a pending promise.
It is therefore not possible to write a function which conditionally returns synchronously if possible and a promise if necessary.
Proposed solution
Add a method validateSyncIfPossible to class Validator which returns immediately with ValidationError[] if there are no awaiting promises and otherwise returns with Promise<ValidationError[]>.
validateSyncIfPossible(objectOrSchemaName: object |string,objectOrValidationOptions: object |ValidationOptions,maybeValidatorOptions?: ValidatorOptions): ValidationError[]|Promise<ValidationError[]>{// same as coreValidate and validateSyncconst object =typeofobjectOrSchemaName==='string' ? (objectOrValidationOptionsasobject) : objectOrSchemaName;constoptions=typeofobjectOrSchemaName==='string' ? maybeValidatorOptions : (objectOrValidationOptionsasValidationOptions);constschema=typeofobjectOrSchemaName==='string' ? objectOrSchemaName : undefined;constexecutor=newValidationExecutor(this,options);constvalidationErrors: ValidationError[]=[];executor.execute(object,schema,validationErrors);// return a promise only if there are awaiting promises; otherwise return synchronouslyreturnexecutor.awaitingPromises.length===0
? executor.stripEmptyErrors(validationErrors)
: Promise.all(executor.awaitingPromises).then(()=>executor.stripEmptyErrors(validationErrors));}
The text was updated successfully, but these errors were encountered:
Description
The validator has public methods for asynchronous validation (
validate
andvalidateOrReject
) and for synchronous validation (validateSync
).Unfortunately the synchronous method ignores all async validations while the asynchronous methods always return a pending promise.
It is therefore not possible to write a function which conditionally returns synchronously if possible and a promise if necessary.
Proposed solution
Add a method
validateSyncIfPossible
to classValidator
which returns immediately withValidationError[]
if there are no awaiting promises and otherwise returns withPromise<ValidationError[]>
.The text was updated successfully, but these errors were encountered: