Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Update lisk-validators to return single error and compatible type check #7156

Closed
Tracked by #7210
shuse2 opened this issue May 6, 2022 · 0 comments · Fixed by #7347
Closed
Tracked by #7210

Update lisk-validators to return single error and compatible type check #7156

shuse2 opened this issue May 6, 2022 · 0 comments · Fixed by #7347

Comments

@shuse2
Copy link
Collaborator

shuse2 commented May 6, 2022

Description

Currently, when using validate function,

const errors = validator.validate(schema, params)
if (errors.length > 0) {
	throw new LiskValidationError(errors);
}

validator.validate<SomeType>(schema, params);
where error thrown is LiskValidationError, and params should be SomeType after this function
https://github.dev/LiskHQ/lisk-sdk/blob/00595476a406f9413e77c7381803425d5d22cec9/elements/lisk-validator/src/lisk_validator.ts#L72-L87

public validate<T = Record<string, unknown>>(schema: object, data: unknown): asserts data is T {
	if (!this._validator.validate(schema, data)) {
		throw new LiskValidationError(convertErrorsToLegacyFormat(this._validator.errors as LiskErrorObject[]));
	}
}
public validateSchema(schema: object): asserts schema is SchemaObject {
	if (!this._validator.validateSchema(schema)) {
		throw new LiskValidationError(convertErrorsToLegacyFormat(this._validator.errors as LiskErrorObject[]));
	}
}

Motivation

  • We should have type guard on the validate function so after this function, type is ensured
  • It should not be required to check error length

Additional Information

  • All usages needs to be updated
@shuse2 shuse2 moved this to Backlog in Lisk SDK Version 6.0.0 May 6, 2022
@milenagojkovic milenagojkovic added this to the Sprint 74 milestone Jul 18, 2022
@sitetester sitetester self-assigned this Jul 21, 2022
@milenagojkovic milenagojkovic moved this from Backlog to In Progress in Lisk SDK Version 6.0.0 Jul 26, 2022
@milenagojkovic milenagojkovic moved this from In Progress to Pending Review in Lisk SDK Version 6.0.0 Jul 28, 2022
ishantiw added a commit that referenced this issue Aug 1, 2022
Update lisk-validators to return single error and compatible type check - Closes #7156
Repository owner moved this from Pending Review to Done in Lisk SDK Version 6.0.0 Aug 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants