decoders 2.0.0-beta10 → 2.0.0-beta11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Decoder.d.ts CHANGED
@@ -11,8 +11,9 @@ export type DecodeFn<T, I = unknown> = (
11
11
  ) => DecodeResult<T>;
12
12
 
13
13
  export interface Decoder<T> {
14
- decode(blob: unknown): DecodeResult<T>;
15
14
  verify(blob: unknown, formatterFn?: (ann: Annotation) => string): T;
15
+ value(blob: unknown): T | undefined;
16
+ decode(blob: unknown): DecodeResult<T>;
16
17
  refine<N extends T>(predicate: (value: T) => value is N, msg: string): Decoder<N>;
17
18
  refine(predicate: (value: T) => boolean, msg: string): Decoder<T>;
18
19
  reject(rejectFn: (value: T) => string | Annotation | null): Decoder<T>;
package/Decoder.js CHANGED
@@ -24,7 +24,7 @@ function noThrow(fn) {
24
24
  * The function receives three arguments:
25
25
  *
26
26
  * 1. `blob` - the raw/unknown input (aka your external data)
27
- * 2. `ok` - Call `ok(value)` to accept the input and return `value`
27
+ * 2. `ok` - Call `ok(value)` to accept the input and return ``value``
28
28
  * 3. `err` - Call `err(message)` to reject the input and use "message" in the
29
29
  * annotation
30
30
  *
@@ -70,6 +70,19 @@ function define(decodeFn) {
70
70
  throw _err;
71
71
  }
72
72
  }
73
+ /**
74
+ * Verified the (raw/untrusted/unknown) input and either accepts or rejects
75
+ * it. When accepted, returns the decoded `T` value directly. Otherwise
76
+ * returns `undefined`.
77
+ *
78
+ * Use this when you're not interested in programmatically handling the
79
+ * error message.
80
+ */
81
+
82
+
83
+ function value(blob) {
84
+ return decode(blob).value;
85
+ }
73
86
  /**
74
87
  * Accepts any value the given decoder accepts, and on success, will call
75
88
  * the given function **on the decoded result**. If the transformation
@@ -187,6 +200,7 @@ function define(decodeFn) {
187
200
 
188
201
  return Object.freeze({
189
202
  verify: verify,
203
+ value: value,
190
204
  decode: decode,
191
205
  transform: transform,
192
206
  refine: refine,
package/Decoder.js.flow CHANGED
@@ -32,8 +32,9 @@ export type DecodeFn<T, I = mixed> = (
32
32
  export type DecoderType<D> = $Call<<T>(Decoder<T>) => T, D>;
33
33
 
34
34
  export type Decoder<T> = {|
35
- decode(blob: mixed): DecodeResult<T>,
36
35
  verify(blob: mixed, formatterFn?: (Annotation) => string): T,
36
+ value(blob: mixed): T | void,
37
+ decode(blob: mixed): DecodeResult<T>,
37
38
  refine(predicateFn: (value: T) => boolean, errmsg: string): Decoder<T>,
38
39
  reject(rejectFn: (value: T) => string | Annotation | null): Decoder<T>,
39
40
  transform<V>(transformFn: (value: T) => V): Decoder<V>,
@@ -60,7 +61,7 @@ function noThrow<T, V>(fn: (value: T) => V): (T) => DecodeResult<V> {
60
61
  * The function receives three arguments:
61
62
  *
62
63
  * 1. `blob` - the raw/unknown input (aka your external data)
63
- * 2. `ok` - Call `ok(value)` to accept the input and return `value`
64
+ * 2. `ok` - Call `ok(value)` to accept the input and return ``value``
64
65
  * 3. `err` - Call `err(message)` to reject the input and use "message" in the
65
66
  * annotation
66
67
  *
@@ -98,6 +99,18 @@ export function define<T>(decodeFn: DecodeFn<T>): Decoder<T> {
98
99
  }
99
100
  }
100
101
 
102
+ /**
103
+ * Verified the (raw/untrusted/unknown) input and either accepts or rejects
104
+ * it. When accepted, returns the decoded `T` value directly. Otherwise
105
+ * returns `undefined`.
106
+ *
107
+ * Use this when you're not interested in programmatically handling the
108
+ * error message.
109
+ */
110
+ function value(blob: mixed): T | void {
111
+ return decode(blob).value;
112
+ }
113
+
101
114
  /**
102
115
  * Accepts any value the given decoder accepts, and on success, will call
103
116
  * the given function **on the decoded result**. If the transformation
@@ -211,6 +224,7 @@ export function define<T>(decodeFn: DecodeFn<T>): Decoder<T> {
211
224
 
212
225
  return Object.freeze({
213
226
  verify,
227
+ value,
214
228
  decode,
215
229
  transform,
216
230
  refine,
package/Decoder.mjs CHANGED
@@ -17,7 +17,7 @@ function noThrow(fn) {
17
17
  * The function receives three arguments:
18
18
  *
19
19
  * 1. `blob` - the raw/unknown input (aka your external data)
20
- * 2. `ok` - Call `ok(value)` to accept the input and return `value`
20
+ * 2. `ok` - Call `ok(value)` to accept the input and return ``value``
21
21
  * 3. `err` - Call `err(message)` to reject the input and use "message" in the
22
22
  * annotation
23
23
  *
@@ -63,6 +63,19 @@ export function define(decodeFn) {
63
63
  throw _err;
64
64
  }
65
65
  }
66
+ /**
67
+ * Verified the (raw/untrusted/unknown) input and either accepts or rejects
68
+ * it. When accepted, returns the decoded `T` value directly. Otherwise
69
+ * returns `undefined`.
70
+ *
71
+ * Use this when you're not interested in programmatically handling the
72
+ * error message.
73
+ */
74
+
75
+
76
+ function value(blob) {
77
+ return decode(blob).value;
78
+ }
66
79
  /**
67
80
  * Accepts any value the given decoder accepts, and on success, will call
68
81
  * the given function **on the decoded result**. If the transformation
@@ -180,6 +193,7 @@ export function define(decodeFn) {
180
193
 
181
194
  return Object.freeze({
182
195
  verify: verify,
196
+ value: value,
183
197
  decode: decode,
184
198
  transform: transform,
185
199
  refine: refine,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decoders",
3
- "version": "2.0.0-beta10",
3
+ "version": "2.0.0-beta11",
4
4
  "description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
5
5
  "license": "MIT",
6
6
  "repository": {