Skip to content
Permalink
Browse files

feat: joiValidationError to return errorItems

BREAKING CHANGE: renamed AppValidationError to JoiValidationError
  • Loading branch information...
kirillgroshkov committed Feb 2, 2019
1 parent 51e0fa1 commit 0bcb60399f8f4a56fda0fe7c2167e91f58183aa8
@@ -24,14 +24,14 @@
"@types/jest": "^23.3.5",
"@types/node": "^10.0.0",
"jest": "^23.6.0",
"jest-junit": "^5.2.0",
"jest-junit": "^6.2.1",
"joi": "^14.0.4",
"lru-cache": "^4.1.3",
"lru-cache": "^5.1.1",
"luxon": "^1.4.3",
"prettier": "^1.14.3",
"semantic-release": "^15.10.8",
"ts-jest": "^23.10.4",
"ts-node": "^7.0.1",
"ts-node": "^8.0.2",
"tslint": "^5.11.0",
"typescript": "^3.0.0"
},
@@ -24,8 +24,8 @@ import {
utcOffsetSchema,
verSchema,
} from './validation/joi/joi.shared.schemas'
import { joiValidationService, ValidationResult } from './validation/joi/joi.validation.service'
import { AppValidationError } from './validation/joi/validation.error'
import { JoiValidationError } from './validation/joi/joi.validation.error'
import { JoiValidationResult, joiValidationService } from './validation/joi/joi.validation.service'

export {
GotOptions,
@@ -35,8 +35,8 @@ export {
LUXON_ISO_DATE_FORMAT,
localDateUtil,
localTimeUtil,
AppValidationError,
ValidationResult,
JoiValidationError,
JoiValidationResult,
joiValidationService,
Joi,
ExtendedJoi,
@@ -0,0 +1,3 @@
import { silentConsoleIfRunAll } from '@naturalcycles/js-lib'

silentConsoleIfRunAll()
@@ -1,11 +1,66 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`error should contain errorItems 1`] = `
Object {
"joiValidationErrorItems": Array [
Object {
"context": Object {
"encoding": undefined,
"key": "a2",
"label": "a2",
"limit": 2,
"value": "a",
},
"message": "\\"a2\\" length must be at least 2 characters long",
"path": Array [
"a2",
],
"type": "string.min",
},
],
}
`;

exports[`getValidationResult should still convert 1`] = `
[AppValidationError: objName
[JoiValidationError: objName
{
"a1": " ff ",
"a2" [1]: "a"
}

[1] "a2" length must be at least 2 characters long]
`;

exports[`should fail on invalid values 1`] = `"\\"value\\" is required"`;

exports[`should fail on invalid values 2`] = `
"{
\\"value\\" [1]: -- missing --
}

[1] \\"value\\" must be an object"
`;

exports[`should fail on invalid values 3`] = `"\\"value\\" must be an object"`;

exports[`should fail on invalid values 4`] = `"\\"value\\" must be an object"`;

exports[`should fail on invalid values 5`] = `"\\"value\\" must be an object"`;

exports[`should fail on invalid values 6`] = `"\\"value\\" must be an object"`;

exports[`should fail on invalid values 7`] = `
"{
\\"a1\\" [1]: \\"a\\"
}

[1] \\"a1\\" length must be at least 2 characters long"
`;

exports[`should fail on invalid values 8`] = `
"{
\\"a1\\" [1]: \\"a12345\\"
}

[1] \\"a1\\" length must be less than or equal to 5 characters long"
`;
@@ -0,0 +1,13 @@
import { AppError } from '@naturalcycles/js-lib'
import { ErrorData } from '@naturalcycles/js-lib'
import { ValidationErrorItem } from 'joi'

export interface JoiValidationErrorData extends ErrorData {
joiValidationErrorItems: ValidationErrorItem[]
}

export class JoiValidationError extends AppError {
constructor (message: string, data: JoiValidationErrorData) {
super(message, data)
}
}
@@ -1,6 +1,6 @@
import { stringSchema } from './joi.shared.schemas'
import { JoiValidationError } from './joi.validation.error'
import { joiValidationService } from './joi.validation.service'
import { AppValidationError } from './validation.error'

const schema = {
a1: stringSchema.min(2).max(5),
@@ -30,7 +30,7 @@ const validValues: any[] = [

test('should fail on invalid values', () => {
invalidValues.forEach(v => {
expect(() => joiValidationService.validate(v, schema)).toThrow(AppValidationError)
expect(() => joiValidationService.validate(v, schema)).toThrowErrorMatchingSnapshot()
})
})

@@ -66,7 +66,7 @@ test('getValidationResult should still convert', async () => {
}
const vr = joiValidationService.getValidationResult(v, schema, 'objName')
expect(vr.value.a1).toBe('ff')
expect(vr.error).toBeInstanceOf(AppValidationError)
expect(vr.error).toBeInstanceOf(JoiValidationError)
expect(vr.error).toMatchSnapshot()
})

@@ -79,5 +79,14 @@ test('validationErrorToString', async () => {
expect(joiValidationService.validationErrorToString(undefined as any)).toBeUndefined()
})

test('error should contain errorItems', async () => {
const v = {
a1: ' ff ', // to be converted
a2: 'a', // invalid!
}
const { error } = joiValidationService.getValidationResult(v, schema, 'objName')
expect(error!.data).toMatchSnapshot()
})

// optional
// default values
@@ -1,10 +1,10 @@
import { SchemaLike, ValidationError, ValidationOptions } from 'joi'
import { Joi } from './joi.extensions'
import { AppValidationError } from './validation.error'
import { JoiValidationError } from './joi.validation.error'

export interface ValidationResult<T = any> {
export interface JoiValidationResult<T = any> {
value: T
error?: AppValidationError
error?: JoiValidationError
}

const defaultOptions: ValidationOptions = {
@@ -37,13 +37,18 @@ class JoiValidationService {
objectName?: string,
options: ValidationOptions = {},
): T {
const r = Joi.validate(value, schema, { ...defaultOptions, ...options })
const { value: returnValue, error } = Joi.validate(value, schema, {
...defaultOptions,
...options,
})

if (r.error) {
throw new AppValidationError(this.validationErrorToString(r.error, objectName))
if (error) {
throw new JoiValidationError(this.validationErrorToString(error, objectName), {
joiValidationErrorItems: error.details,
})
}

return r.value
return returnValue
}

/**
@@ -56,15 +61,20 @@ class JoiValidationService {
schema: SchemaLike,
objectName?: string,
options: ValidationOptions = {},
): ValidationResult<T> {
const r = Joi.validate(value, schema, { ...defaultOptions, ...options })
): JoiValidationResult<T> {
const { value: returnValue, error } = Joi.validate(value, schema, {
...defaultOptions,
...options,
})

const vr: ValidationResult<T> = {
value: r.value,
const vr: JoiValidationResult<T> = {
value: returnValue,
}

if (r.error) {
vr.error = new AppValidationError(this.validationErrorToString(r.error, objectName))
if (error) {
vr.error = new JoiValidationError(this.validationErrorToString(error, objectName), {
joiValidationErrorItems: error.details,
})
}

return vr

This file was deleted.

0 comments on commit 0bcb603

Please sign in to comment.
You can’t perform that action at this time.