Comparing version 0.0.1-18 to 0.0.1-19
@@ -23,3 +23,9 @@ "use strict"; | ||
_decode(medium, unpacked, path) { | ||
let value = medium.requireCodec(this.symbol).decode(unpacked); | ||
let value; | ||
try { | ||
value = medium.requireCodec(this.symbol).decode(unpacked); | ||
} | ||
catch (error) { | ||
return buildCodecErrorResult(error, path); | ||
} | ||
let issues = this._diagnose(value, path); | ||
@@ -36,4 +42,8 @@ return [issues.length === 0 ? value : undefined, issues]; | ||
} | ||
let unpacked = medium.requireCodec(this.symbol).encode(value); | ||
return [unpacked, []]; | ||
try { | ||
return [medium.requireCodec(this.symbol).encode(value), []]; | ||
} | ||
catch (error) { | ||
return buildCodecErrorResult(error, path); | ||
} | ||
} | ||
@@ -43,3 +53,9 @@ /** @internal */ | ||
let symbol = this.symbol; | ||
let value = from.requireCodec(symbol).decode(unpacked); | ||
let value; | ||
try { | ||
value = from.requireCodec(symbol).decode(unpacked); | ||
} | ||
catch (error) { | ||
return buildCodecErrorResult(error, path); | ||
} | ||
let issues = this._diagnose(value, path); | ||
@@ -49,4 +65,8 @@ if (issues.length > 0) { | ||
} | ||
let transformedUnpacked = to.requireCodec(symbol).encode(value); | ||
return [transformedUnpacked, issues]; | ||
try { | ||
return [to.requireCodec(symbol).encode(value), issues]; | ||
} | ||
catch (error) { | ||
return buildCodecErrorResult(error, path); | ||
} | ||
} | ||
@@ -74,2 +94,13 @@ /** @internal */ | ||
exports.atomic = atomic; | ||
function buildCodecErrorResult(error, path) { | ||
return [ | ||
undefined, | ||
[ | ||
{ | ||
path, | ||
message: typeof error === 'string' ? error : error.message, | ||
}, | ||
], | ||
]; | ||
} | ||
//# sourceMappingURL=atomic-type.js.map |
{ | ||
"name": "x-value", | ||
"version": "0.0.1-18", | ||
"version": "0.0.1-19", | ||
"repository": "https://github.com/vilic/x-value.git", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -29,3 +29,3 @@ [![NPM version](https://img.shields.io/npm/v/x-value?color=%23cb3837&style=flat-square)](https://www.npmjs.com/package/x-value) | ||
foo: x.string, | ||
bar: x.optional(x.number), | ||
bar: x.number.optional(), | ||
}); | ||
@@ -47,2 +47,14 @@ | ||
); | ||
interface R { | ||
type: 'recursive'; | ||
child: R; | ||
} | ||
const R = x.recursive<R>(R => | ||
x.object({ | ||
type: x.literal('recursive'), | ||
child: R, | ||
}), | ||
); | ||
``` | ||
@@ -62,3 +74,3 @@ | ||
// Or with refined or nominal type. | ||
// Or with refined or nominal type: | ||
const Email = x.string.refine<`${string}@${string}`>(value => | ||
@@ -68,2 +80,5 @@ value.includes('@'), | ||
const Email = x.string.refine<Nominal<'email'>>(value => value.includes('@')); | ||
// Or just nominal type without extra constraints: | ||
const Email = x.string.nominal<'email'>(); | ||
``` | ||
@@ -70,0 +85,0 @@ |
@@ -62,4 +62,10 @@ import type { | ||
): [unknown, TypeIssue[]] { | ||
let value = medium.requireCodec(this.symbol).decode(unpacked); | ||
let value: unknown; | ||
try { | ||
value = medium.requireCodec(this.symbol).decode(unpacked); | ||
} catch (error: any) { | ||
return buildCodecErrorResult(error, path); | ||
} | ||
let issues = this._diagnose(value, path); | ||
@@ -85,5 +91,7 @@ | ||
let unpacked = medium.requireCodec(this.symbol).encode(value); | ||
return [unpacked, []]; | ||
try { | ||
return [medium.requireCodec(this.symbol).encode(value), []]; | ||
} catch (error: any) { | ||
return buildCodecErrorResult(error, path); | ||
} | ||
} | ||
@@ -100,4 +108,10 @@ | ||
let value = from.requireCodec(symbol).decode(unpacked); | ||
let value: unknown; | ||
try { | ||
value = from.requireCodec(symbol).decode(unpacked); | ||
} catch (error: any) { | ||
return buildCodecErrorResult(error, path); | ||
} | ||
let issues = this._diagnose(value, path); | ||
@@ -109,5 +123,7 @@ | ||
let transformedUnpacked = to.requireCodec(symbol).encode(value); | ||
return [transformedUnpacked, issues]; | ||
try { | ||
return [to.requireCodec(symbol).encode(value), issues]; | ||
} catch (error: any) { | ||
return buildCodecErrorResult(error, path); | ||
} | ||
} | ||
@@ -145,1 +161,16 @@ | ||
} | ||
function buildCodecErrorResult( | ||
error: string | Error, | ||
path: TypePath, | ||
): [undefined, TypeIssue[]] { | ||
return [ | ||
undefined, | ||
[ | ||
{ | ||
path, | ||
message: typeof error === 'string' ? error : error.message, | ||
}, | ||
], | ||
]; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
197252
4182
254