Skip to content

Commit

Permalink
Add toJSON methods to every type
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Sep 3, 2018
1 parent be25d99 commit 09ad649
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm install standardschema --save
## Usage

```ts
import { fromJSON, AnyType, IntegerType, StringType, ... } from 'standardschema'
import { fromJSON } from 'standardschema'

const model = fromJSON({
'@type': 'Object',
Expand All @@ -29,7 +29,7 @@ const model = fromJSON({
]
})

model.isAssignable(new IntegerType()) //=> false
model.isAssignable(fromJSON({ '@type': 'Integer' })) //=> false
```

## License
Expand Down
2 changes: 0 additions & 2 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
AnyType,
StringType,
IntegerType,
ObjectType,
ListType,
fromJSON,
FloatType
} from './index'
Expand Down
62 changes: 61 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prop } from 'functools'
import { prop, invoke } from 'functools'
import { zip, map } from 'iterative'

export class AnyType {
Expand All @@ -10,6 +10,10 @@ export class AnyType {
return new UnknownType()
}

toJSON () {
return { '@type': 'Any' }
}

static fromJSON(data: any) {
return new AnyType()
}
Expand All @@ -20,6 +24,10 @@ export class UnknownType extends AnyType {
return other instanceof UnknownType
}

toJSON () {
return { '@type': 'Unknown' }
}

static fromJSON(data: any) {
return new UnknownType()
}
Expand All @@ -30,6 +38,10 @@ export class NullType extends AnyType {
return other instanceof NullType
}

toJSON () {
return { '@type': 'Null' }
}

static fromJSON(data: any) {
return new NullType()
}
Expand All @@ -40,6 +52,10 @@ export class BooleanType extends AnyType {
return other instanceof BooleanType
}

toJSON () {
return { '@type': 'Boolean' }
}

static fromJSON(data: any) {
return new BooleanType()
}
Expand All @@ -50,6 +66,10 @@ export class StringType extends AnyType {
return other instanceof StringType
}

toJSON () {
return { '@type': 'String' }
}

static fromJSON(data: any) {
return new StringType()
}
Expand All @@ -60,6 +80,10 @@ export class NumberType extends AnyType {
return other instanceof NumberType
}

toJSON () {
return { '@type': 'Number' }
}

static fromJSON(data: any) {
return new NumberType()
}
Expand All @@ -70,6 +94,10 @@ export class IntegerType extends NumberType {
return other instanceof IntegerType
}

toJSON () {
return { '@type': 'Integer' }
}

static fromJSON(data: any) {
return new IntegerType()
}
Expand All @@ -80,6 +108,10 @@ export class FloatType extends NumberType {
return other instanceof FloatType
}

toJSON () {
return { '@type': 'Float' }
}

static fromJSON(data: any) {
return new FloatType()
}
Expand All @@ -100,6 +132,10 @@ export class ListType<T extends AnyType> extends AnyType {
return this.items // TODO: Enable out-of-bounds indexes with min/max items.
}

toJSON () {
return { '@type': 'List', items: this.items.toJSON() }
}

static fromJSON(data: any) {
return new ListType(fromJSON(data.items))
}
Expand All @@ -117,6 +153,15 @@ export class PropertyType<V extends AnyType> extends UnknownType {
return this.value.isAssignable(other.value)
}

toJSON () {
return {
'@type': 'Property',
key: this.key,
value: this.value.toJSON(),
required: this.required
}
}

static fromJSON(data: any) {
return new PropertyType(
String(data.key),
Expand Down Expand Up @@ -161,6 +206,13 @@ export class ObjectType extends AnyType {
return super.getProperty(key)
}

toJSON() {
return {
'@type': 'Object',
properties: Array.from(map(this.properties.values(), invoke('toJSON')))
}
}

static fromJSON(data: any) {
return new ObjectType(data.properties.map(fromJSON))
}
Expand All @@ -171,6 +223,10 @@ export class DateType extends AnyType {
return other instanceof DateType
}

toJSON() {
return { '@type': 'Date' }
}

static fromJSON(data: any) {
return new DateType()
}
Expand All @@ -181,6 +237,10 @@ export class DateTimeType extends DateType {
return other instanceof DateTimeType
}

toJSON() {
return { '@type': 'DateTime' }
}

static fromJSON(data: any) {
return new DateTimeType()
}
Expand Down

0 comments on commit 09ad649

Please sign in to comment.