@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.
Files changed (2) hide show
  1. package/build/index.js +12 -5
  2. 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) => parseFloat(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
- else if (schema.required) {
169
- return result;
170
- }
171
- return undefined;
178
+ return result;
172
179
  }
173
180
  return result;
174
181
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucania/schema",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "A schema module for compile-time and runtime type checking.",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",