-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validation to JSON transformer (#830)
Signed-off-by: Moriarty <[email protected]>
- Loading branch information
1 parent
a7754bd
commit 5b9efe3
Showing
29 changed files
with
369 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { ValidationError } from 'class-validator' | ||
|
||
import { AriesFrameworkError } from './AriesFrameworkError' | ||
|
||
export class ClassValidationError extends AriesFrameworkError { | ||
public validationErrors: ValidationError[] | ||
|
||
public validationErrorsToString() { | ||
return this.validationErrors?.map((error) => error.toString(true)).join('\n') ?? '' | ||
} | ||
|
||
public constructor( | ||
message: string, | ||
{ classType, cause, validationErrors }: { classType: string; cause?: Error; validationErrors?: ValidationError[] } | ||
) { | ||
const validationErrorsStringified = validationErrors?.map((error) => error.toString()).join('\n') | ||
super( | ||
`${classType}: ${message} | ||
${validationErrorsStringified}`, | ||
{ cause } | ||
) | ||
this.validationErrors = validationErrors ?? [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ValidationError } from 'class-validator' | ||
|
||
export function isValidationErrorArray(e: ValidationError[] | unknown): boolean { | ||
if (Array.isArray(e)) { | ||
const isErrorArray = e.length > 0 && e.every((err) => err instanceof ValidationError) | ||
return isErrorArray | ||
} | ||
return false | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/core/src/error/__tests__/ValidationErrorUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ValidationError } from 'class-validator' | ||
|
||
import { isValidationErrorArray } from '../ValidationErrorUtils' | ||
|
||
describe('ValidationErrorUtils', () => { | ||
test('returns true for an array of ValidationErrors', () => { | ||
const error = new ValidationError() | ||
const errorArray = [error, error] | ||
const isErrorArray = isValidationErrorArray(errorArray) | ||
expect(isErrorArray).toBeTruthy | ||
}) | ||
|
||
test('returns false for an array of strings', () => { | ||
const errorArray = ['hello', 'world'] | ||
const isErrorArray = isValidationErrorArray(errorArray) | ||
expect(isErrorArray).toBeFalsy | ||
}) | ||
|
||
test('returns false for a non array', () => { | ||
const error = new ValidationError() | ||
const isErrorArray = isValidationErrorArray(error) | ||
expect(isErrorArray).toBeFalsy | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.