Skip to content
Permalink
Browse files

refactor: "flatten" util classes into functions

BREAKING CHANGE:
similar to js-lib, functions are exported directly, not wrapped into "util classes" anymore.
  • Loading branch information...
kirillgroshkov committed Mar 9, 2019
1 parent 292df26 commit aa93bbe1ed3dd51015cacc847da0a9eefd2e83d2
@@ -12,12 +12,12 @@
"debug": "^4.1.0"
},
"peerDependencies": {
"@naturalcycles/js-lib": ">=2",
"@naturalcycles/js-lib": ">=4",
"got": "^9.3.1",
"joi": "^14.0.4"
},
"devDependencies": {
"@naturalcycles/js-lib": "^3.0.0",
"@naturalcycles/js-lib": "^4.0.0",
"@naturalcycles/semantic-release": "^1.0.0",
"@naturalcycles/shared-module": "^3.0.0",
"@types/jest": "^24.0.6",
@@ -1,5 +1,5 @@
import { processSharedUtil } from './infra/process.shared.util'
import { zipSharedUtil } from './util/zip.shared.util'
import { unzipBuffer, unzipToString, zipBuffer, zipString } from './util/zip.util'
import { ExtendedJoi, Joi } from './validation/joi/joi.extensions'
import {
anyObjectSchema,
@@ -23,7 +23,12 @@ import {
verSchema,
} from './validation/joi/joi.shared.schemas'
import { JoiValidationError } from './validation/joi/joi.validation.error'
import { JoiValidationResult, joiValidationService } from './validation/joi/joi.validation.service'
import {
getValidationResult,
JoiValidationResult,
validate,
validationErrorToString,
} from './validation/joi/joi.validation.util'

export {
// todo: in progress, not exported yet
@@ -36,7 +41,9 @@ export {
// localTimeUtil,
JoiValidationError,
JoiValidationResult,
joiValidationService,
validate,
getValidationResult,
validationErrorToString,
Joi,
ExtendedJoi,
booleanSchema,
@@ -59,5 +66,8 @@ export {
utcOffsetSchema,
ipAddressSchema,
processSharedUtil,
zipSharedUtil,
zipBuffer,
unzipBuffer,
zipString,
unzipToString,
}

This file was deleted.

This file was deleted.

@@ -0,0 +1 @@
export const LUXON_ISO_DATE_FORMAT = 'yyyy-MM-dd'

This file was deleted.

@@ -1,22 +1,22 @@
import { zipSharedUtil } from './zip.shared.util'
import { unzipBuffer, unzipToString, zipBuffer, zipString } from './zip.util'

test('zip/unzip', async () => {
const s = 'abcd1234$%^'

// String
let zippedBuf = await zipSharedUtil.zipString(s)
const unzippedStr = await zipSharedUtil.unzipToString(zippedBuf)
let zippedBuf = await zipString(s)
const unzippedStr = await unzipToString(zippedBuf)
expect(unzippedStr).toBe(s)

const sBuf = Buffer.from(s)
zippedBuf = await zipSharedUtil.zip(sBuf)
const unzippedBuf = await zipSharedUtil.unzip(zippedBuf)
zippedBuf = await zipBuffer(sBuf)
const unzippedBuf = await unzipBuffer(zippedBuf)
expect(unzippedBuf).toEqual(sBuf)
})

test('compatible with java impl', async () => {
const s = 'aa'
const zippedBuf = await zipSharedUtil.zipString(s)
const zippedBuf = await zipString(s)
const bytes: number[] = []
zippedBuf.forEach(c => bytes.push(c))
// console.log(bytes)
@@ -0,0 +1,26 @@
import { promisify } from 'util'
import { ZlibOptions } from 'zlib'
import * as zlib from 'zlib'

const deflate = promisify(zlib.deflate.bind(zlib)) as Function
const inflate = promisify(zlib.inflate.bind(zlib)) as Function

// string > zip
export async function zipBuffer (buf: Buffer, options: ZlibOptions = {}): Promise<Buffer> {
return deflate(buf, options)
}

// zip > buffer
export async function unzipBuffer (buf: Buffer, options: ZlibOptions = {}): Promise<Buffer> {
return inflate(buf, options)
}

// convenience
export async function zipString (s: string, options?: ZlibOptions): Promise<Buffer> {
return zipBuffer(Buffer.from(s), options)
}

// convenience
export async function unzipToString (buf: Buffer, options?: ZlibOptions): Promise<string> {
return (await unzipBuffer(buf, options)).toString()
}
@@ -1,8 +1,8 @@
import { DateTime } from 'luxon'
import { LUXON_ISO_DATE_FORMAT } from '../../util/localDate.util'
import { LUXON_ISO_DATE_FORMAT } from '../../util/time.util'
import { Joi } from './joi.extensions'
import { stringSchema } from './joi.shared.schemas'
import { joiValidationService } from './joi.validation.service'
import { validate } from './joi.validation.util'

test('dateString', async () => {
const schema = {
@@ -82,7 +82,7 @@ test('dividable', async () => {
function shouldBeInvalid (schema: any, values: any[]) {
values.forEach(v => {
try {
joiValidationService.validate(v, schema)
validate(v, schema)
console.log('value', v)
fail('expected to fail on invalid value (see console)')
} catch {}
@@ -92,7 +92,7 @@ function shouldBeInvalid (schema: any, values: any[]) {
function shouldBeValid (schema: any, values: any[]) {
values.forEach(v => {
try {
joiValidationService.validate(v, schema)
validate(v, schema)
} catch (err) {
console.log('value', v)
throw err
@@ -1,7 +1,7 @@
import { Extension, State, StringSchema, ValidationOptions } from 'joi'
import * as JoiLib from 'joi'
import { DateTime } from 'luxon'
import { LUXON_ISO_DATE_FORMAT } from '../../util/localDate.util'
import { LUXON_ISO_DATE_FORMAT } from '../../util/time.util'

export const Joi: ExtendedJoi = JoiLib.defaults(schema => {
if (schema.schemaType === 'string') {
@@ -1,18 +1,18 @@
import { semVerSchema } from './joi.shared.schemas'
import { joiValidationService } from './joi.validation.service'
import { validate } from './joi.validation.util'

test('semVerSchema', async () => {
// invalid
;[undefined, null, '', 3, '1.', '1.5.', '1.5.x', '1.5.e', '1', '1.5', '1.5.3.2'].forEach(v => {
try {
joiValidationService.validate(v, semVerSchema)
validate(v, semVerSchema)
console.log('value', v)
fail('expected to fail on invalid value (see console)')
} catch {}
})

// valid
;['1.0.0', '1.5.3', '2.9.4', '3.0.14', '0.0.14'].forEach(v => {
joiValidationService.validate(v, semVerSchema)
validate(v, semVerSchema)
})
})
@@ -1,5 +1,4 @@
import { AppError } from '@naturalcycles/js-lib'
import { ErrorData } from '@naturalcycles/js-lib'
import { AppError, ErrorData } from '@naturalcycles/js-lib'
import { ValidationErrorItem } from 'joi'

export interface JoiValidationErrorData extends ErrorData {

This file was deleted.

@@ -1,6 +1,6 @@
import { stringSchema } from './joi.shared.schemas'
import { JoiValidationError } from './joi.validation.error'
import { joiValidationService } from './joi.validation.service'
import { getValidationResult, validate, validationErrorToString } from './joi.validation.util'

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

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

test('should pass on valid values', () => {
validValues.forEach(v => {
joiValidationService.validate(v, schema)
validate(v, schema)
})
})

test('should trim strings by default', async () => {
const v = { a1: ' sdf ' }
const v2 = joiValidationService.validate(v, schema)
const v2 = validate(v, schema)

expect(v2.a1).toBe('sdf')
expect(v === v2).toBeFalsy() // object should be cloned
@@ -53,7 +53,7 @@ test('should strip unknown keys', async () => {
a1: 'ff',
unk: 'ddd',
}
const v2 = joiValidationService.validate(v, schema)
const v2 = validate(v, schema)

expect(v2).toEqual({ a1: 'ff' })
expect(v2.unk).toBeUndefined()
@@ -64,27 +64,27 @@ test('getValidationResult should still convert', async () => {
a1: ' ff ', // to be converted
a2: 'a', // invalid!
}
const vr = joiValidationService.getValidationResult(v, schema, 'objName')
const vr = getValidationResult(v, schema, 'objName')
expect(vr.value.a1).toBe('ff')
expect(vr.error).toBeInstanceOf(JoiValidationError)
expect(vr.error).toMatchSnapshot()
})

test('getValidationResult valid', async () => {
const vr = joiValidationService.getValidationResult('asd', stringSchema)
const vr = getValidationResult('asd', stringSchema)
expect(vr.error).toBeUndefined()
})

test('validationErrorToString', async () => {
expect(joiValidationService.validationErrorToString(undefined as any)).toBeUndefined()
expect(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')
const { error } = getValidationResult(v, schema, 'objName')
expect(error!.data).toMatchSnapshot()
})

0 comments on commit aa93bbe

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