Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an instance of Error, not arbitary object #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This library uses the [combinator pattern](https://wiki.haskell.org/Combinator_p
* ["combinators"](modules/_combinators_.md)
* ["decoder"](modules/_decoder_.md)
* ["index"](modules/_index_.md)
* ["isEqual"](modules/_isequal_.md)
* ["result"](modules/_result_.md)

---
Expand Down
111 changes: 50 additions & 61 deletions docs/classes/_decoder_.decoder.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@mojotech/json-type-validation](../README.md) > ["decoder"](../modules/_decoder_.md) > [Decoder](../classes/_decoder_.decoder.md)
[yuyaryshev-json-type-validation](../README.md) > ["decoder"](../modules/_decoder_.md) > [Decoder](../classes/_decoder_.decoder.md)

# Class: Decoder

Expand Down Expand Up @@ -384,101 +384,90 @@ ___

### `<Static>` constant

▸ **constant**(value: *`true`*): [Decoder](_decoder_.decoder.md)<`true`>
▸ **constant**T(value: *`T`*): [Decoder](_decoder_.decoder.md)<`T`>

▸ **constant**(value: *`false`*): [Decoder](_decoder_.decoder.md)<`false`>
▸ **constant**T,U(value: *`U`*): [Decoder](_decoder_.decoder.md)<`U`>

▸ **constant**A(value: *`A`*): [Decoder](_decoder_.decoder.md)<`A`>
▸ **constant**T,U(value: *`U`*): [Decoder](_decoder_.decoder.md)<`U`>

Decoder primitive that only matches on exact values.
▸ **constant**T(value: *`T`*): [Decoder](_decoder_.decoder.md)<`T`>

Note that `constant('string to match')` returns a `Decoder<string>` which fails if the input is not equal to `'string to match'`. In many cases this is sufficient, but in some situations typescript requires that the decoder type be a type-literal. In such a case you must provide the type parameter, which looks like `constant<'string to match'>('string to match')`.
Decoder primitive that only matches on exact values.

Providing the type parameter is only necessary for type-literal strings and numbers, as detailed by this table:
For primitive values and shallow structures of primitive values `constant` will infer an exact literal type:

```
| Decoder | Type |
| ---------------------------- | ---------------------|
| constant(true) | Decoder<true> |
| constant(false) | Decoder<false> |
| constant(null) | Decoder<null> |
| constant('alaska') | Decoder<string> |
| constant<'alaska'>('alaska') | Decoder<'alaska'> |
| constant(50) | Decoder<number> |
| constant<50>(50) | Decoder<50> |
| constant([1,2,3]) | Decoder<number[]> |
| constant<[1,2,3]>([1,2,3]) | Decoder<[1,2,3]> |
| constant({x: 't'}) | Decoder<{x: string}> |
| constant<{x: 't'}>({x: 't'}) | Decoder<{x: 't'}> |
| Decoder | Type |
| ---------------------------- | ------------------------------|
| constant(true) | Decoder<true> |
| constant(false) | Decoder<false> |
| constant(null) | Decoder<null> |
| constant(undefined) | Decoder<undefined> |
| constant('alaska') | Decoder<'alaska'> |
| constant(50) | Decoder<50> |
| constant([1,2,3]) | Decoder<[1,2,3]> |
| constant({x: 't'}) | Decoder<{x: 't'}> |
```

One place where this happens is when a type-literal is in an interface:
Inference breaks on nested structures, which require an annotation to get the literal type:

```
interface Bear {
kind: 'bear';
isBig: boolean;
}
| Decoder | Type |
| -----------------------------|-------------------------------|
| constant([1,[2]]) | Decoder<(number|number[])[]> |
| constant<[1,[2]]>([1,[2]]) | Decoder<[1,[2]]> |
| constant({x: [1]}) | Decoder<{x: number[]}> |
| constant<{x: [1]}>({x: [1]}) | Decoder<{x: [1]}> |
```

const bearDecoder1: Decoder<Bear> = object({
kind: constant('bear'),
isBig: boolean()
});
// Type 'Decoder<{ kind: string; isBig: boolean; }>' is not assignable to
// type 'Decoder<Bear>'. Type 'string' is not assignable to type '"bear"'.
**Type parameters:**

const bearDecoder2: Decoder<Bear> = object({
kind: constant<'bear'>('bear'),
isBig: boolean()
});
// no compiler errors
```
#### T : `string` &#124; `number` &#124; `boolean` &#124; `[]`

Another is in type-literal unions:
**Parameters:**

```
type animal = 'bird' | 'bear';

const animalDecoder1: Decoder<animal> = union(
constant('bird'),
constant('bear')
);
// Type 'Decoder<string>' is not assignable to type 'Decoder<animal>'.
// Type 'string' is not assignable to type 'animal'.

const animalDecoder2: Decoder<animal> = union(
constant<'bird'>('bird'),
constant<'bear'>('bear')
);
// no compiler errors
```
| Param | Type |
| ------ | ------ |
| value | `T` |

**Returns:** [Decoder](_decoder_.decoder.md)<`T`>

**Type parameters:**

#### T : `string` &#124; `number` &#124; `boolean`

#### U : [`T`, `Array`]
**Parameters:**

| Param | Type |
| ------ | ------ |
| value | `true` |
| value | `U` |

**Returns:** [Decoder](_decoder_.decoder.md)<`true`>
**Returns:** [Decoder](_decoder_.decoder.md)<`U`>

**Type parameters:**

#### T : `string` &#124; `number` &#124; `boolean`

#### U : `Record`<`string`, `T`>
**Parameters:**

| Param | Type |
| ------ | ------ |
| value | `false` |
| value | `U` |

**Returns:** [Decoder](_decoder_.decoder.md)<`false`>
**Returns:** [Decoder](_decoder_.decoder.md)<`U`>

**Type parameters:**

#### A
#### T
**Parameters:**

| Param | Type |
| ------ | ------ |
| value | `A` |
| value | `T` |

**Returns:** [Decoder](_decoder_.decoder.md)<`A`>
**Returns:** [Decoder](_decoder_.decoder.md)<`T`>

___
<a id="dict"></a>
Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/_decoder_.decodererror.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@mojotech/json-type-validation](../README.md) > ["decoder"](../modules/_decoder_.md) > [DecoderError](../interfaces/_decoder_.decodererror.md)
[yuyaryshev-json-type-validation](../README.md) > ["decoder"](../modules/_decoder_.md) > [DecoderError](../interfaces/_decoder_.decodererror.md)

# Interface: DecoderError

Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/_result_.err.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@mojotech/json-type-validation](../README.md) > ["result"](../modules/_result_.md) > [Err](../interfaces/_result_.err.md)
[yuyaryshev-json-type-validation](../README.md) > ["result"](../modules/_result_.md) > [Err](../interfaces/_result_.err.md)

# Interface: Err

Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/_result_.ok.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@mojotech/json-type-validation](../README.md) > ["result"](../modules/_result_.md) > [Ok](../interfaces/_result_.ok.md)
[yuyaryshev-json-type-validation](../README.md) > ["result"](../modules/_result_.md) > [Ok](../interfaces/_result_.ok.md)

# Interface: Ok

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/_combinators_.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@mojotech/json-type-validation](../README.md) > ["combinators"](../modules/_combinators_.md)
[yuyaryshev-json-type-validation](../README.md) > ["combinators"](../modules/_combinators_.md)

# External module: "combinators"

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/_decoder_.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@mojotech/json-type-validation](../README.md) > ["decoder"](../modules/_decoder_.md)
[yuyaryshev-json-type-validation](../README.md) > ["decoder"](../modules/_decoder_.md)

# External module: "decoder"

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/_index_.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@mojotech/json-type-validation](../README.md) > ["index"](../modules/_index_.md)
[yuyaryshev-json-type-validation](../README.md) > ["index"](../modules/_index_.md)

# External module: "index"

Expand Down
34 changes: 34 additions & 0 deletions docs/modules/_isequal_.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[yuyaryshev-json-type-validation](../README.md) > ["isEqual"](../modules/_isequal_.md)

# External module: "isEqual"

## Index

### Functions

* [isEqual](_isequal_.md#isequal)

---

## Functions

<a id="isequal"></a>

### isEqual

▸ **isEqual**T(a: *`T`*, b: *`T`*): `boolean`

**Type parameters:**

#### T
**Parameters:**

| Param | Type |
| ------ | ------ |
| a | `T` |
| b | `T` |

**Returns:** `boolean`

___

2 changes: 1 addition & 1 deletion docs/modules/_result_.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@mojotech/json-type-validation](../README.md) > ["result"](../modules/_result_.md)
[yuyaryshev-json-type-validation](../README.md) > ["result"](../modules/_result_.md)

# External module: "result"

Expand Down
Loading