Skip to content

Commit

Permalink
Issue #52: replace lodash with native/sublibs (#59)
Browse files Browse the repository at this point in the history
* replace lodash with native/sublibs

* forgot this

* corrections

* remove lodash types
  • Loading branch information
mikeguta authored Mar 27, 2021
1 parent a7144f8 commit 56c329e
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 88 deletions.
1 change: 0 additions & 1 deletion __tests__/customValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator'
import * as _ from 'lodash'

import { validationMetadatasToSchemas } from '../src'

Expand Down
4 changes: 2 additions & 2 deletions __tests__/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
MaxLength,
MetadataStorage,
} from 'class-validator'
import * as _ from 'lodash'
import _get from 'lodash.get'

import { JSONSchema, validationMetadatasToSchemas } from '../src'

Expand Down Expand Up @@ -42,7 +42,7 @@ class User {
empty?: string
}

const metadata = _.get(getFromContainer(MetadataStorage), 'validationMetadatas')
const metadata = _get(getFromContainer(MetadataStorage), 'validationMetadatas')
const schemas = validationMetadatasToSchemas(metadata)

describe('decorators', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/defaultConverters.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tslint:disable:object-literal-sort-keys
import * as validator from 'class-validator'
import * as _ from 'lodash'
import _get from 'lodash.get'

import { validationMetadatasToSchemas } from '../src'

Expand Down Expand Up @@ -139,7 +139,7 @@ class User {
@validator.ArrayUnique() arrayUnique: any[]
}

const metadata = _.get(
const metadata = _get(
validator.getFromContainer(validator.MetadataStorage),
'validationMetadatas'
)
Expand Down
7 changes: 2 additions & 5 deletions __tests__/inheritedProperties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
MetadataStorage,
MinLength,
} from 'class-validator'
import * as _ from 'lodash'
import _get from 'lodash.get'

import { JSONSchema, validationMetadatasToSchemas } from '../src'

Expand Down Expand Up @@ -65,10 +65,7 @@ class User extends BaseContent {
@Contains('hello') welcome: string
}

const metadatas = _.get(
getFromContainer(MetadataStorage),
'validationMetadatas'
)
const metadatas = _get(getFromContainer(MetadataStorage), 'validationMetadatas')

describe('Inheriting validation decorators', () => {
it('inherits and merges validation decorators from parent class', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
MetadataStorage,
ValidateNested,
} from 'class-validator'
import * as _ from 'lodash'
import _get from 'lodash.get'

import { validationMetadatasToSchemas } from '../src'

Expand All @@ -33,7 +33,7 @@ class Post {
user: User
}

const metadata = _.get(getFromContainer(MetadataStorage), 'validationMetadatas')
const metadata = _get(getFromContainer(MetadataStorage), 'validationMetadatas')
const defaultSchemas = validationMetadatasToSchemas(metadata)

describe('options', () => {
Expand Down
46 changes: 45 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@
"singleQuote": true
},
"dependencies": {
"lodash": "^4.17.20",
"lodash.groupby": "^4.6.0",
"lodash.merge": "^4.6.2",
"openapi3-ts": "^2.0.0",
"reflect-metadata": "^0.1.13",
"tslib": "^2.0.3"
},
"devDependencies": {
"@team-griffin/install-self-peers": "^1.1.1",
"@types/jest": "^26.0.19",
"@types/lodash": "^4.14.166",
"@types/lodash.get": "^4.4.6",
"@types/lodash.groupby": "^4.6.6",
"@types/lodash.merge": "^4.6.6",
"@types/node": "^14.14.7",
"@types/prettier": "^2.1.6",
"@types/reflect-metadata": "^0.1.0",
"codecov": "^3.8.1",
"jest": "^26.6.3",
"lodash.get": "^4.4.2",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"ts-jest": "^26.4.4",
Expand Down
42 changes: 24 additions & 18 deletions src/defaultConverters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// tslint:disable:no-submodule-imports
import * as cv from 'class-validator'
import { ValidationMetadata } from 'class-validator/types/metadata/ValidationMetadata'
import * as _ from 'lodash'
import { SchemaObject } from 'openapi3-ts'
import 'reflect-metadata'

Expand All @@ -18,13 +17,13 @@ export type SchemaConverter = (

export const defaultConverters: ISchemaConverters = {
[cv.ValidationTypes.CUSTOM_VALIDATION]: (meta, options) => {
if (_.isFunction(meta.target)) {
if (typeof meta.target === 'function') {
const type = getPropType(meta.target.prototype, meta.propertyName)
return targetToSchema(type, options)
}
},
[cv.ValidationTypes.NESTED_VALIDATION]: (meta, options) => {
if (_.isFunction(meta.target)) {
if (typeof meta.target === 'function') {
const typeMeta = options.classTransformerMetadataStorage
? options.classTransformerMetadataStorage.findTypeMetadata(
meta.target,
Expand Down Expand Up @@ -77,14 +76,18 @@ export const defaultConverters: ISchemaConverters = {
type: 'string',
},
[cv.IS_IN]: (meta) => {
const [head, ...rest] = meta.constraints[0].map(constraintToSchema)
if (head && _.every(rest, { type: head.type })) {
const [head, ...rest]: SchemaObject[] = meta.constraints[0].map(
constraintToSchema
)
if (head && rest.every((item) => item.type === head.type)) {
return { ...head, enum: meta.constraints[0] }
}
},
[cv.IS_NOT_IN]: (meta) => {
const [head, ...rest] = meta.constraints[0].map(constraintToSchema)
if (head && _.every(rest, { type: head.type })) {
const [head, ...rest]: SchemaObject[] = meta.constraints[0].map(
constraintToSchema
)
if (head && rest.every((item) => item.type === head.type)) {
return { not: { ...head, enum: meta.constraints[0] } }
}
},
Expand Down Expand Up @@ -311,11 +314,11 @@ export const defaultConverters: ISchemaConverters = {
type: 'string',
},
[cv.ARRAY_CONTAINS]: (meta) => {
const schemas = meta.constraints[0].map(constraintToSchema)
if (schemas.length > 0 && _.every(schemas, 'type')) {
const schemas: SchemaObject[] = meta.constraints[0].map(constraintToSchema)
if (schemas.length > 0 && schemas.every((s) => s && s.type)) {
return {
not: {
anyOf: _.map(schemas, (d, i) => ({
anyOf: schemas.map((d, i) => ({
items: {
not: {
...d,
Expand All @@ -330,12 +333,12 @@ export const defaultConverters: ISchemaConverters = {
return { items: {}, type: 'array' }
},
[cv.ARRAY_NOT_CONTAINS]: (meta) => {
const schemas = meta.constraints[0].map(constraintToSchema)
if (schemas.length > 0 && _.every(schemas, 'type')) {
const schemas: SchemaObject[] = meta.constraints[0].map(constraintToSchema)
if (schemas.length > 0 && schemas.every((s) => s && s.type)) {
return {
items: {
not: {
anyOf: _.map(schemas, (d, i) => ({
anyOf: schemas.map((d, i) => ({
...d,
enum: [meta.constraints[0][i]],
})),
Expand Down Expand Up @@ -375,18 +378,21 @@ function getPropType(target: object, property: string) {
function constraintToSchema(primitive: any): SchemaObject | void {
const primitives = ['string', 'number', 'boolean']
const type = typeof primitive
if (_.includes(primitives, type)) {
if (primitives.includes(type)) {
return { type: type as 'string' | 'number' | 'boolean' }
}
}

function targetToSchema(type: any, options: IOptions): SchemaObject | void {
if (_.isFunction(type)) {
if (_.isString(type.prototype) || _.isSymbol(type.prototype)) {
if (typeof type === 'function') {
if (
type.prototype === String.prototype ||
type.prototype === Symbol.prototype
) {
return { type: 'string' }
} else if (_.isNumber(type.prototype)) {
} else if (type.prototype === Number.prototype) {
return { type: 'number' }
} else if (_.isBoolean(type.prototype)) {
} else if (type.prototype === Boolean.prototype) {
return { type: 'boolean' }
}

Expand Down
Loading

0 comments on commit 56c329e

Please sign in to comment.