this repo has no description
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.valueAsNumber = exports.valueAsString = exports.valueAsBoolean = void 0;
4const optional_1 = require("../../types/optional");
5const validation = require("../validation");
6/**
7 * Attempt to coerce the given value to a boolean.
8 *
9 * @see asBoolean
10 * @param value - the value to coerce
11 * @param policy - determines when validation errors are added to the current validation context
12 * @param path - an optional string appended to validation errors to identify where this value originated
13 * @returns a boolean if the value was a boolean or coercible to a boolean, otherwise null
14 */
15function valueAsBoolean(value, policy = "coercible", path) {
16 if (!(0, optional_1.isSome)(value)) {
17 return value;
18 }
19 if (typeof value === "boolean") {
20 return value;
21 }
22 // Handle string coercion
23 if (typeof value === "string") {
24 if (value === "true") {
25 return true;
26 }
27 else if (value === "false") {
28 return false;
29 }
30 }
31 // Else coerce.
32 const coercedValue = Boolean(value);
33 switch (policy) {
34 case "strict": {
35 validation.context("asBoolean", () => {
36 validation.unexpectedType("coercedValue", "boolean", value, path);
37 });
38 break;
39 }
40 case "coercible": {
41 if ((0, optional_1.isNothing)(coercedValue)) {
42 validation.context("asBoolean", () => {
43 validation.unexpectedType("coercedValue", "boolean", value, path);
44 });
45 return null;
46 }
47 break;
48 }
49 case "none":
50 default: {
51 break;
52 }
53 }
54 return coercedValue;
55}
56exports.valueAsBoolean = valueAsBoolean;
57/**
58 * Attempt to coerce the given value to a string.
59 *
60 * @see asString
61 * @param value - the value to coerce
62 * @param policy - determines when validation errors are added to the current validation context
63 * @param path - an optional string appended to validation errors to identify where this value originated
64 * @returns a string if the value was a string or coercible to a string, otherwise null
65 */
66function valueAsString(value, policy = "coercible", path) {
67 if (!(0, optional_1.isSome)(value)) {
68 return value;
69 }
70 if (typeof value === "string") {
71 return value;
72 }
73 // We don't consider arbitrary objects as convertable to strings even through they will result in some value
74 const coercedValue = typeof value === "object" ? null : String(value);
75 switch (policy) {
76 case "strict": {
77 validation.context("asString", () => {
78 validation.unexpectedType("coercedValue", "string", value, path);
79 });
80 break;
81 }
82 case "coercible": {
83 if ((0, optional_1.isNothing)(coercedValue)) {
84 validation.context("asString", () => {
85 validation.unexpectedType("coercedValue", "string", value, path);
86 });
87 }
88 break;
89 }
90 case "none":
91 default: {
92 break;
93 }
94 }
95 return coercedValue;
96}
97exports.valueAsString = valueAsString;
98/**
99 * Attempt to coerce the given value to a number.
100 *
101 * @see asNumber
102 * @param value - the value to coerce
103 * @param policy - determines when validation errors are added to the current validation context
104 * @param path - an optional string appended to validation errors to identify where this value originated
105 * @returns a number if the value was a number or coercible to a number, otherwise null
106 */
107function valueAsNumber(value, policy = "coercible", path) {
108 if (!(0, optional_1.isSome)(value)) {
109 return value;
110 }
111 if (typeof value === "number") {
112 return value;
113 }
114 const coercedValue = Number(value);
115 switch (policy) {
116 case "strict": {
117 validation.context("asNumber", () => {
118 validation.unexpectedType("coercedValue", "number", value, path);
119 });
120 break;
121 }
122 case "coercible": {
123 if (isNaN(coercedValue)) {
124 validation.context("asNumber", () => {
125 validation.unexpectedType("coercedValue", "number", value, path);
126 });
127 return null;
128 }
129 break;
130 }
131 case "none":
132 default: {
133 break;
134 }
135 }
136 return coercedValue;
137}
138exports.valueAsNumber = valueAsNumber;
139//# sourceMappingURL=coercion.js.map