Skip to content

Commit

Permalink
fix(type): type any correctly validates.
Browse files Browse the repository at this point in the history
not having a template for any meant array checking breaks.

fixes #336
  • Loading branch information
marcj committed Aug 1, 2022
1 parent e0ff6b5 commit c94175b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/type/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
isBackReferenceType,
isMongoIdType,
isNullable,
isOptional, isPropertyMemberType,
isOptional,
isPropertyMemberType,
isReferenceType,
isType,
isUUIDType,
Expand Down Expand Up @@ -1913,6 +1914,11 @@ export class Serializer {
}

protected registerTypeGuards() {
this.typeGuards.register(1, ReflectionKind.any, (type, state) => {
//if any is part of an union, we use register(20) below. otherwise it would match before anything else.
if (type.parent && type.parent.kind === ReflectionKind.union) return;
state.addSetter('true');
});
//if nothing else matches in a union, any matches anything
this.typeGuards.register(20, ReflectionKind.any, (type, state) => state.addSetter('true'));

Expand Down
45 changes: 45 additions & 0 deletions packages/type/tests/typeguard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ test('enum string', () => {
});

test('array string', () => {
expect(is<string[]>([])).toEqual(true);
expect(is<string[]>(['a'])).toEqual(true);
expect(is<string[]>(['a', 'b'])).toEqual(true);
expect(is<string[]>([1])).toEqual(false);
Expand Down Expand Up @@ -159,6 +160,50 @@ test('literal', () => {
expect(is<true>(false)).toEqual(false);
});

test('any', () => {
expect(is<any>(['a'])).toEqual(true);
expect(is<any>([1])).toEqual(true);
expect(is<any>([true])).toEqual(true);
expect(is<any>([false])).toEqual(true);
expect(is<any>([undefined])).toEqual(true);
expect(is<any>([null])).toEqual(true);
expect(is<any>([{}])).toEqual(true);
expect(is<any>([])).toEqual(true);

expect(is<any>('a')).toEqual(true);
expect(is<any>(1)).toEqual(true);
expect(is<any>(true)).toEqual(true);
expect(is<any>(false)).toEqual(true);
expect(is<any>(undefined)).toEqual(true);
expect(is<any>(null)).toEqual(true);
expect(is<any>({})).toEqual(true);
expect(is<any>([])).toEqual(true);
});

test('array any', () => {
expect(is<any[]>(['a'])).toEqual(true);
expect(is<any[]>([1])).toEqual(true);
expect(is<any[]>([true])).toEqual(true);
expect(is<any[]>([false])).toEqual(true);
expect(is<any[]>([undefined])).toEqual(true);
expect(is<any[]>([null])).toEqual(true);
expect(is<any[]>([{}])).toEqual(true);
expect(is<any[]>([])).toEqual(true);
expect(is<Array<any>>([])).toEqual(true);
expect(is<Array<any>>(['a'])).toEqual(true);

expect(is<any[]>(null)).toEqual(false);
expect(is<any[]>(undefined)).toEqual(false);
expect(is<any[]>(1)).toEqual(false);
expect(is<any[]>(true)).toEqual(false);
expect(is<any[]>({})).toEqual(false);

expect(is<any[]>({length:1})).toEqual(false);
expect(is<any[]>({length:0})).toEqual(false);
expect(is<any[]>({length:null})).toEqual(false);
expect(is<any[]>({length:undefined})).toEqual(false);
});

test('union', () => {
expect(is<string | number>(1)).toEqual(true);
expect(is<string | number>('abc')).toEqual(true);
Expand Down

0 comments on commit c94175b

Please sign in to comment.