Skip to content

Commit

Permalink
supports union type & null type
Browse files Browse the repository at this point in the history
  • Loading branch information
robturtle committed Dec 10, 2019
1 parent 184c108 commit 1af5c4c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.8.0

- supports union types
- supports null type

So we can express the type `string | null`

## 1.7.3

- export `Int` branded type
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ Since its main purpose is for JSON validation, only a subset of interface syntax
2. Other acceptable interfaces
3. Classes
4. Literal types (i.e. `interface Address { pos: { lat: number; lng: number; } }`, here `Address.pos` is a literal type)
5. Array type of 1-4
5. Union types
6. null type
7. Array type of 1-5

Also

1. The fields in the interface CAN be marked as optional.
2. Generic types are NOT supported.
3. Union types are NOT supported yet (PR, feature request welcomed if you really need this to work with a bad-designed API)
4. `null`, `any`, `unknown` are illegal.
5. Recursive types are NOT supported.
6. intersection types are NOT supported YET. (it's useful to implement custom validations)
3. `any`, `unknown` are illegal.
4. Recursive types are NOT supported.
5. intersection types are NOT supported YET.

### You need declare schemas in topological order

Expand Down
11 changes: 6 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

rm -rf lib/

cp ./node_modules/ts-transformer-interface/transformer.ts ./transform-interface.ts
cp ./node_modules/ts-transformer-interface/runtime-schema.ts ./runtime-schema.ts
cp ./node_modules/ts-transformer-decoder-cast/transformer.ts ./transform-request.ts
cp ./node_modules/ts-transformer-interface/transformer.d.ts ./transform-interface.d.ts
cp ./node_modules/ts-transformer-interface/transformer.js ./transform-interface.js

cp ./node_modules/ts-transformer-interface/runtime-schema.d.ts ./runtime-schema.d.ts
cp ./node_modules/ts-transformer-interface/runtime-schema.js ./runtime-schema.js

cat runtime-schema.ts | sed 's/index.d/index/' > replaced
mv replaced runtime-schema.ts
cp ./node_modules/ts-transformer-decoder-cast/transformer.ts ./transform-request.ts

tsc
npx pretty-quick
Expand Down
10 changes: 10 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const TypeC: t.Type<runtime.Type> = t.recursion('TypeC', () =>
ParameterizedTypeC,
GenericTypeC,
LiteralTypeC,
UnionTypeC,
]),
);

Expand All @@ -52,6 +53,10 @@ const ParameterizedTypeC: t.Type<runtime.ParameterizedType> = t.type({
typeArgumentType: TypeC,
});

const UnionTypeC: t.Type<runtime.UnionType> = t.type({
union: t.array(TypeC),
});

const PropertyC: t.Type<runtime.Property> = t.type({
name: t.string,
type: TypeC,
Expand Down Expand Up @@ -326,13 +331,18 @@ export class Decoder implements ICaster {
if (LiteralTypeC.is(type)) {
return t.type(this.buildCasters(type.props));
}
if (UnionTypeC.is(type)) {
return type.union.map(t => this.buildTypeCaster(t)).reduce((t1, t2) => t.union([t1, t2]));
}
switch (type) {
case 'string':
return t.string;
case 'number':
return t.number;
case 'boolean':
return t.boolean;
case 'null':
return t.null;
default:
throw new Error(`illegal decoder type ${JSON.stringify(type)}`);
}
Expand Down
34 changes: 34 additions & 0 deletions test/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,37 @@ const decoded188 = dec.decode(
if (JSON.stringify(good188) !== JSON.stringify(decoded188)) {
throw new Error('nested Array NOT WORKING!!!');
}

// union type
console.log('union types');
interface House {
owner: Customer | null;
}
dec.register(schema<House>());

const house = {
owner: {
firstName: 'Yang',
lastName: 'Liu',
},
};

const decodedHouse = dec.decode('House', house, e => {
throw e;
});
if (!decodedHouse) {
throw new Error('Union type NOT WORKING!');
} else {
console.log(decodedHouse);
}

const house2 = { owner: null };
const decodedHouse2 = dec.decode('House', house2, e => {
throw e;
});
if (!decodedHouse2) {
throw new Error('Union type (with null) NOT WORKING!');
} else {
console.log(decodedHouse2);
}
console.log('-'.repeat(40));

0 comments on commit 1af5c4c

Please sign in to comment.