Skip to content

Commit

Permalink
export Json, JsonRecord, JsonArray codecs from JsonFromString
Browse files Browse the repository at this point in the history
… module, closes #156
  • Loading branch information
gcanti committed Feb 19, 2021
1 parent 29bc8a6 commit 25909c6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 0.5.15

- **Polish**
- export `Json`, `JsonRecord`, `JsonArray` codecs from `JsonFromString` module, closes #156 (@gcanti)

# 0.5.14

- **New Feature**
Expand Down
33 changes: 33 additions & 0 deletions docs/modules/JsonFromString.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ Added in v0.5.14
- [JsonArray (interface)](#jsonarray-interface)
- [JsonRecord (interface)](#jsonrecord-interface)
- [Json (type alias)](#json-type-alias)
- [Json](#json)
- [JsonArray](#jsonarray)
- [JsonFromString](#jsonfromstring)
- [JsonRecord](#jsonrecord)

---

Expand Down Expand Up @@ -53,6 +56,26 @@ export type Json = boolean | number | string | null | JsonArray | JsonRecord
Added in v0.5.14
# Json
**Signature**
```ts
export const Json: t.Type<Json> = ...
```

Added in v0.5.15

# JsonArray

**Signature**

```ts
export const JsonArray: t.Type<JsonArray> = ...
```

Added in v0.5.15

# JsonFromString

**Signature**
Expand All @@ -62,3 +85,13 @@ export const JsonFromString: t.Type<Json, string, string> = ...
```

Added in v0.5.14

# JsonRecord

**Signature**

```ts
export const JsonRecord: t.Type<JsonRecord> = ...
```

Added in v0.5.15
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-ts-types",
"version": "0.5.14",
"version": "0.5.15",
"description": "A collection of codecs and combinators for use with io-ts",
"main": "lib/index.js",
"module": "es6/index.js",
Expand Down
17 changes: 14 additions & 3 deletions src/JsonFromString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ export interface JsonRecord {
*/
export interface JsonArray extends ReadonlyArray<Json> {}

const JsonArray: t.Type<JsonArray> = t.recursion('JsonArray', () => t.readonlyArray(Json))
const JsonRecord: t.Type<JsonRecord> = t.recursion('JsonRecord', () => t.record(t.string, Json))
const Json: t.Type<Json> = t.union([t.boolean, t.number, t.string, t.null, JsonArray, JsonRecord], 'Json')
/**
* @since 0.5.15
*/
export const JsonArray: t.Type<JsonArray> = t.recursion('JsonArray', () => t.readonlyArray(Json))

/**
* @since 0.5.15
*/
export const JsonRecord: t.Type<JsonRecord> = t.recursion('JsonRecord', () => t.record(t.string, Json))

/**
* @since 0.5.15
*/
export const Json: t.Type<Json> = t.union([t.boolean, t.number, t.string, t.null, JsonArray, JsonRecord], 'Json')

/**
* @since 0.5.14
Expand Down
13 changes: 12 additions & 1 deletion test/JsonFromString.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'assert'
import { JsonFromString } from '../src'
import { JsonFromString, JsonArray } from '../src'
import { assertFailure, assertSuccess } from './helpers'
import * as t from 'io-ts'

describe('JSONFromString', () => {
it('is', () => {
Expand Down Expand Up @@ -34,4 +35,14 @@ describe('JSONFromString', () => {
const T = JsonFromString
assert.deepEqual(T.encode({}), '{}')
})

it('#156', () => {
const C = JsonFromString.pipe(
t.type({
a: JsonArray
})
)
assertSuccess(C.decode('{"a":[]}'), { a: [] })
assertFailure(C, '{"a":1}', ['Invalid value 1 supplied to : pipe(JsonFromString, { a: JsonArray })/a: JsonArray'])
})
})

0 comments on commit 25909c6

Please sign in to comment.