Skip to content

Commit cc89811

Browse files
author
Alex Malkevich
committed
feat(decorator): capture extra metadata in @Property
Get `isRequired` property that will optionally remove `null/undefined` from generated codecs
1 parent 41a3522 commit cc89811

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

projects/gen-io-ts/src/lib/generator.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ThrowReporter } from 'io-ts/lib/ThrowReporter';
33
import { Property } from './decorators';
44
import { genIoType } from './generator';
55

6-
fdescribe('genIoType() function', () => {
6+
describe('genIoType() function', () => {
77
it('should not validate non-annotated class', () => {
88
class MyClass {
99
prop1: string;
@@ -43,4 +43,32 @@ fdescribe('genIoType() function', () => {
4343

4444
expect(() => ThrowReporter.report(res)).toThrow();
4545
});
46+
47+
it('should pass validation on non required props', () => {
48+
class MyClass {
49+
@Property()
50+
prop1: string;
51+
@Property()
52+
prop2: number;
53+
}
54+
55+
const myClassType = genIoType(MyClass);
56+
const res = myClassType.decode({});
57+
58+
expect(() => ThrowReporter.report(res)).not.toThrow();
59+
});
60+
61+
it('should fail validation on required props', () => {
62+
class MyClass {
63+
@Property({ isRequired: true })
64+
prop1: string;
65+
@Property()
66+
prop2: number;
67+
}
68+
69+
const myClassType = genIoType(MyClass);
70+
const res = myClassType.decode({});
71+
72+
expect(() => ThrowReporter.report(res)).toThrow();
73+
});
4674
});

projects/gen-io-ts/src/lib/generator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import { isBuiltinType, isObject, isPrimitive } from './util';
66

77
export function genIoType<T>(type: Type<T>): t.Type<T> {
88
const metadata = resolveMetadataOf(type);
9-
console.log(metadata);
109
return genTypeFor(metadata, type.name);
1110
}
1211

1312
function genTypeFor(obj: any, name?: string): t.Type<any> {
14-
const type = ResolvedTypeMetadata.isResolvedMetadata(obj) ? obj.type : obj;
13+
const type = ResolvedTypeMetadata.isResolvedMetadata(obj) ? obj.meta : obj;
1514
const metadata = ResolvedTypeMetadata.isResolvedMetadata(obj) ? obj : null;
1615
const codec = genCodecType(type, name);
1716

projects/gen-io-ts/src/lib/metadata.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('resolveMetadataOf() function', () => {
99
prop1: boolean;
1010
@Property()
1111
prop2: number;
12-
@Property(new AnyOf('lol', 'd'))
12+
@Property({ type: new AnyOf('lol', 'd') })
1313
prop3: string[];
1414
}
1515
class MyClass {
@@ -21,11 +21,13 @@ describe('resolveMetadataOf() function', () => {
2121

2222
expect(meta).toBeTruthy();
2323
expect(meta).toEqual({
24-
prop4: {
25-
prop1: Boolean,
26-
prop2: Number,
27-
prop3: ['lol', 'd'],
28-
},
24+
prop4: jasmine.objectContaining({
25+
meta: {
26+
prop1: jasmine.objectContaining({ meta: Boolean }),
27+
prop2: jasmine.objectContaining({ meta: Number }),
28+
prop3: jasmine.objectContaining({ meta: ['lol', 'd'] }),
29+
},
30+
}),
2931
} as any);
3032
});
3133
});

projects/gen-io-ts/src/lib/metadata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ export interface TypeMetadata<T extends RuntimeType> {
1212
}
1313

1414
export class ResolvedTypeMetadata<T> implements TypeMetadata<T> {
15+
meta: AsRuntimeType<T> = {} as any;
16+
1517
static isResolvedMetadata(obj: any): obj is ResolvedTypeMetadata<any> {
1618
return !!obj && obj instanceof ResolvedTypeMetadata;
1719
}
1820

19-
meta: AsRuntimeType<T> = {} as any;
2021
constructor(public type: T, public isRequired = false) {}
2122
}
2223

@@ -38,7 +39,6 @@ function resolveMetaRecursive(obj: any) {
3839
}
3940

4041
const metaInfo = getPropertyTypes(obj);
41-
console.log(metaInfo);
4242

4343
if (!metaInfo) {
4444
return Object;

0 commit comments

Comments
 (0)