Skip to content

Commit d7fc135

Browse files
committed
refactor: standardizes case interface and unitifies tests
1 parent 46f2918 commit d7fc135

27 files changed

+345
-333
lines changed

cases/__snapshots__/class-validator.test.ts.snap

Lines changed: 0 additions & 53 deletions
This file was deleted.

cases/__snapshots__/io-ts.test.ts.snap

Lines changed: 0 additions & 5 deletions
This file was deleted.

cases/__snapshots__/marshal.test.ts.snap

Lines changed: 0 additions & 5 deletions
This file was deleted.

cases/__snapshots__/runtypes.test.ts.snap

Lines changed: 0 additions & 5 deletions
This file was deleted.

cases/__snapshots__/toi.test.ts.snap

Lines changed: 0 additions & 5 deletions
This file was deleted.

cases/__snapshots__/ts-json-validator.test.ts.snap

Lines changed: 0 additions & 5 deletions
This file was deleted.

cases/abstract.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import clone from 'clone'
2+
import { Data } from '../data'
3+
4+
export interface ICase {
5+
/**
6+
* Human readable case name.
7+
*/
8+
name: string
9+
10+
/**
11+
* Validation implementation method.
12+
*
13+
* Method returns a `Data` object or throws when invalid data is provided.
14+
*/
15+
validate(): Promise<Data> | Data
16+
}
17+
18+
export abstract class Case implements ICase {
19+
protected readonly data: Data
20+
21+
public abstract get name(): string
22+
23+
public constructor(data: Data) {
24+
this.data = Object.freeze(clone(data))
25+
}
26+
27+
public abstract validate(): Promise<Data> | Data
28+
}

cases/class-validator.test.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

cases/class-validator.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'reflect-metadata'
22
import { IsNegative, MinLength, ValidateNested } from 'class-validator'
33
import { transformAndValidate, transformAndValidateSync } from 'class-transformer-validator'
4+
import { Case, ICase } from './abstract'
45
import { Data } from '../data'
56

67
type DeeplyNested = Data['deeplyNested']
@@ -29,10 +30,18 @@ class DataType implements Data {
2930
deeplyNested!: DeeplyNestedType
3031
}
3132

32-
export function caseClassValidatorSync(data: Data) {
33-
return transformAndValidateSync(DataType, data)
33+
export class ClassValidatorSyncCase extends Case implements ICase {
34+
name = 'class-transformer-validator-sync'
35+
36+
validate() {
37+
return transformAndValidateSync(DataType, this.data)
38+
}
3439
}
3540

36-
export function caseClassValidatorAsync(data: Data) {
37-
return transformAndValidate(DataType, data)
38-
}
41+
export class ClassValidatorAsyncCase extends Case implements ICase {
42+
name = 'class-transformer-validator-async'
43+
44+
validate() {
45+
return transformAndValidate(DataType, this.data)
46+
}
47+
}

cases/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
export * from './class-validator'
2-
export * from './io-ts'
3-
export * from './json-encode-decode'
4-
export * from './marshal'
5-
export * from './runtypes'
6-
export * from './toi'
7-
export * from './ts-json-validator'
1+
import { ClassValidatorAsyncCase, ClassValidatorSyncCase } from './class-validator'
2+
import { IoTsCase } from './io-ts'
3+
import { MarshalCase } from './marshal'
4+
import { RuntypesCase } from './runtypes'
5+
import { ToiCase } from './toi'
6+
import { TsJsonValidatorCase } from './ts-json-validator'
7+
8+
export default [
9+
ClassValidatorAsyncCase,
10+
ClassValidatorSyncCase,
11+
IoTsCase,
12+
MarshalCase,
13+
RuntypesCase,
14+
ToiCase,
15+
TsJsonValidatorCase
16+
]

0 commit comments

Comments
 (0)