@lucania/schema 1.0.3 → 1.0.5
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/build/index.js +12 -5
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -35,7 +35,14 @@
|
|
|
35
35
|
},
|
|
36
36
|
string: {
|
|
37
37
|
boolean: (value) => value === "false" ? false : Boolean(value),
|
|
38
|
-
number: (value) =>
|
|
38
|
+
number: (value) => {
|
|
39
|
+
const numberValue = parseFloat(value);
|
|
40
|
+
if (Number.isNaN(numberValue)) {
|
|
41
|
+
Schema.assert(value.length === 0, `Failed to convert empty string to number.`);
|
|
42
|
+
throw new Schema.Error(`Failed to convert "${value}" to number.`);
|
|
43
|
+
}
|
|
44
|
+
return numberValue;
|
|
45
|
+
},
|
|
39
46
|
bigint: (value) => BigInt(value),
|
|
40
47
|
any: (value) => JSON.parse(value),
|
|
41
48
|
Date: (value) => new Date(value)
|
|
@@ -157,6 +164,9 @@
|
|
|
157
164
|
}
|
|
158
165
|
const result = _validate(schema.type, source, path, originalSchema, originalSource);
|
|
159
166
|
if (result instanceof ValidationError && result.type === "missing") {
|
|
167
|
+
if (!schema.required && source === undefined) {
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
160
170
|
if ("default" in schema) {
|
|
161
171
|
if (typeof schema.default === "function") {
|
|
162
172
|
return _validate(schema.type, schema.default(), path, originalSchema, originalSource);
|
|
@@ -165,10 +175,7 @@
|
|
|
165
175
|
return _validate(schema.type, schema.default, path, originalSchema, originalSource);
|
|
166
176
|
}
|
|
167
177
|
}
|
|
168
|
-
|
|
169
|
-
return result;
|
|
170
|
-
}
|
|
171
|
-
return undefined;
|
|
178
|
+
return result;
|
|
172
179
|
}
|
|
173
180
|
return result;
|
|
174
181
|
}
|