shelving 1.185.2 → 1.185.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.185.2",
3
+ "version": "1.185.3",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,7 +26,7 @@ export class ChoiceSchema extends Schema {
26
26
  validate(unsafeValue = this.value) {
27
27
  if (typeof unsafeValue === "string" && isOption(this.options, unsafeValue))
28
28
  return unsafeValue;
29
- throw unsafeValue ? "Unknown value" : "Required";
29
+ throw unsafeValue ? `Unknown ${this.one}` : "Required";
30
30
  }
31
31
  // Implement iterable.
32
32
  *[Symbol.iterator]() {
@@ -18,7 +18,7 @@ export interface DateSchemaOptions extends SchemaOptions {
18
18
  readonly step?: number | undefined;
19
19
  }
20
20
  export declare class DateSchema extends Schema<string> {
21
- readonly value: PossibleDate;
21
+ readonly value: PossibleDate | undefined;
22
22
  readonly min: Date | undefined;
23
23
  readonly max: Date | undefined;
24
24
  readonly input: DateInputType;
@@ -8,7 +8,7 @@ export class DateSchema extends Schema {
8
8
  max;
9
9
  input;
10
10
  step;
11
- constructor({ one = "date", min, max, value = "now", input = "date", step, ...options }) {
11
+ constructor({ one = "date", min, max, value, input = "date", step, ...options }) {
12
12
  super({ one, title: "Date", value, ...options });
13
13
  this.min = getDate(min);
14
14
  this.max = getDate(max);
@@ -18,13 +18,13 @@ export class DateSchema extends Schema {
18
18
  validate(value = this.value) {
19
19
  const date = getDate(value);
20
20
  if (!date)
21
- throw value ? "Invalid date" : "Required";
22
- const rounded = typeof this.step === "number" ? new Date(roundStep(date.getTime(), this.step)) : date;
23
- if (this.min && rounded < this.min)
21
+ throw value ? `Invalid ${this.one} format` : "Required";
22
+ const stepped = typeof this.step === "number" ? new Date(roundStep(date.getTime(), this.step)) : date;
23
+ if (this.min && stepped < this.min)
24
24
  throw `Minimum ${this.format(this.min)}`;
25
- if (this.max && rounded > this.max)
25
+ if (this.max && stepped > this.max)
26
26
  throw `Maximum ${this.format(this.max)}`;
27
- return this.stringify(rounded);
27
+ return this.stringify(stepped);
28
28
  }
29
29
  stringify(value) {
30
30
  return requireDateString(value);
@@ -13,9 +13,9 @@ export class FileSchema extends StringSchema {
13
13
  const path = super.validate(unsafeValue);
14
14
  const extension = getFileExtension(path);
15
15
  if (!extension)
16
- throw "Must be file name with extension";
16
+ throw `Must have extension`;
17
17
  if (this.types && !isProp(this.types, extension))
18
- throw "Invalid file type";
18
+ throw `Invalid extension`;
19
19
  return path;
20
20
  }
21
21
  }
@@ -9,14 +9,14 @@ export interface NumberSchemaOptions extends SchemaOptions {
9
9
  }
10
10
  /** Schema that defines a valid number. */
11
11
  export declare class NumberSchema extends Schema<number> {
12
- readonly value: number;
12
+ readonly value: number | undefined;
13
13
  readonly min: number;
14
14
  readonly max: number;
15
15
  readonly step: number | undefined;
16
16
  constructor({ one, title, min, max, step, value, ...options }: NumberSchemaOptions);
17
- validate(unsafeValue?: unknown): number;
17
+ validate(value?: unknown): number;
18
18
  }
19
- /** Valid number, e.g. `2048.12345` or `0` zero. */
19
+ /** Valid number, e.g. `2048.12345` or `0` zero and a default value of zero. */
20
20
  export declare const NUMBER: NumberSchema;
21
21
  /** Valid optional number, e.g. `2048.12345` or `0` zero, or `null` */
22
22
  export declare const NULLABLE_NUMBER: import("./NullableSchema.js").NullableSchema<number>;
@@ -7,38 +7,38 @@ export class NumberSchema extends Schema {
7
7
  min;
8
8
  max;
9
9
  step;
10
- constructor({ one = "number", title = "Number", min = Number.NEGATIVE_INFINITY, max = Number.POSITIVE_INFINITY, step, value = 0, ...options }) {
10
+ constructor({ one = "number", title = "Number", min = Number.NEGATIVE_INFINITY, max = Number.POSITIVE_INFINITY, step, value, ...options }) {
11
11
  super({ one, title, value, ...options });
12
12
  this.min = min;
13
13
  this.max = max;
14
14
  this.step = step;
15
15
  }
16
- validate(unsafeValue = this.value) {
17
- const optionalNumber = getNumber(unsafeValue);
18
- if (typeof optionalNumber !== "number")
19
- throw "Must be number";
20
- const roundedNumber = typeof this.step === "number" ? roundStep(optionalNumber, this.step) : optionalNumber;
21
- if (roundedNumber < this.min)
22
- throw !optionalNumber ? "Required" : `Minimum ${formatNumber(this.min)}`;
23
- if (roundedNumber > this.max)
16
+ validate(value = this.value) {
17
+ const number = getNumber(value);
18
+ if (typeof number !== "number")
19
+ throw value ? `Must be ${this.one}` : "Required";
20
+ const stepped = typeof this.step === "number" ? roundStep(number, this.step) : number;
21
+ if (stepped < this.min)
22
+ throw !number ? "Required" : `Minimum ${formatNumber(this.min)}`;
23
+ if (stepped > this.max)
24
24
  throw `Maximum ${formatNumber(this.max)}`;
25
- return roundedNumber;
25
+ return stepped;
26
26
  }
27
27
  }
28
- /** Valid number, e.g. `2048.12345` or `0` zero. */
28
+ /** Valid number, e.g. `2048.12345` or `0` zero and a default value of zero. */
29
29
  export const NUMBER = new NumberSchema({ title: "Number" });
30
30
  /** Valid optional number, e.g. `2048.12345` or `0` zero, or `null` */
31
31
  export const NULLABLE_NUMBER = NULLABLE(NUMBER);
32
32
  /** Valid integer number, e.g. `2048` or `0` zero. */
33
- export const INTEGER = new NumberSchema({ step: 1, min: Number.MIN_SAFE_INTEGER, max: Number.MAX_SAFE_INTEGER, value: 0 });
33
+ export const INTEGER = new NumberSchema({ step: 1, min: Number.MIN_SAFE_INTEGER, max: Number.MAX_SAFE_INTEGER });
34
34
  /** Valid positive integer number, e.g. `1,2,3` (not including zero). */
35
- export const POSITIVE_INTEGER = new NumberSchema({ step: 1, min: 1, max: Number.MAX_SAFE_INTEGER, value: 1 });
35
+ export const POSITIVE_INTEGER = new NumberSchema({ step: 1, min: 1, max: Number.MAX_SAFE_INTEGER });
36
36
  /** Valid non-negative integer number, e.g. `0,1,2,3` (including zero). */
37
- export const NON_NEGATIVE_INTEGER = new NumberSchema({ step: 1, min: 0, max: Number.MAX_SAFE_INTEGER, value: 0 });
37
+ export const NON_NEGATIVE_INTEGER = new NumberSchema({ step: 1, min: 0, max: Number.MAX_SAFE_INTEGER });
38
38
  /** Valid negative integer number, e.g. `-1,-2,-3` (not including zero). */
39
- export const NEGATIVE_INTEGER = new NumberSchema({ step: 1, min: Number.MIN_SAFE_INTEGER, max: -1, value: -1 });
39
+ export const NEGATIVE_INTEGER = new NumberSchema({ step: 1, min: Number.MIN_SAFE_INTEGER, max: -1 });
40
40
  /** Valid non-positive integer number, e.g. `0,-1,-2,-3` (including zero). */
41
- export const NON_POSITIVE_INTEGER = new NumberSchema({ step: 1, min: Number.MIN_SAFE_INTEGER, max: 0, value: 0 });
41
+ export const NON_POSITIVE_INTEGER = new NumberSchema({ step: 1, min: Number.MIN_SAFE_INTEGER, max: 0 });
42
42
  /** Valid optional integer number, e.g. `2048` or `0` zero, or `null` */
43
43
  export const NULLABLE_INTEGER = NULLABLE(INTEGER);
44
44
  /** Valid Unix timestamp (including milliseconds). */
@@ -47,7 +47,6 @@ export const TIMESTAMP = new NumberSchema({
47
47
  step: 1,
48
48
  min: Number.MIN_SAFE_INTEGER,
49
49
  max: Number.MAX_SAFE_INTEGER,
50
- value: 0,
51
50
  });
52
51
  /** Valid Unix timestamp (including milliseconds). */
53
52
  export const NULLABLE_TIMESTAMP = NULLABLE_INTEGER;
@@ -35,8 +35,8 @@ export declare class StringSchema extends Schema<string> {
35
35
  readonly multiline: boolean;
36
36
  readonly match: RegExp | undefined;
37
37
  readonly case: "upper" | "lower" | undefined;
38
- constructor({ min, max, value, multiline, match, case: _case, input, ...options }: StringSchemaOptions);
39
- validate(unsafeValue?: unknown): string;
38
+ constructor({ one, min, max, value, multiline, match, case: _case, input, ...options }: StringSchemaOptions);
39
+ validate(value?: unknown): string;
40
40
  /** Sanitize the string by removing unwanted characters. */
41
41
  sanitize(str: string): string;
42
42
  }
@@ -23,8 +23,8 @@ export class StringSchema extends Schema {
23
23
  multiline;
24
24
  match;
25
25
  case;
26
- constructor({ min = 0, max = Number.POSITIVE_INFINITY, value = "", multiline = false, match, case: _case, input = "text", ...options }) {
27
- super({ value, ...options });
26
+ constructor({ one = "string", min = 0, max = Number.POSITIVE_INFINITY, value = "", multiline = false, match, case: _case, input = "text", ...options }) {
27
+ super({ one, value, ...options });
28
28
  this.min = min;
29
29
  this.max = max;
30
30
  this.multiline = multiline;
@@ -32,18 +32,18 @@ export class StringSchema extends Schema {
32
32
  this.case = _case;
33
33
  this.input = input;
34
34
  }
35
- validate(unsafeValue = this.value) {
36
- const possibleString = typeof unsafeValue === "number" ? unsafeValue.toString() : unsafeValue;
37
- if (typeof possibleString !== "string")
38
- throw "Must be string";
39
- const saneString = this.sanitize(possibleString);
40
- if (saneString.length < this.min)
41
- throw possibleString.length ? `Minimum ${this.min} characters` : "Required";
42
- if (saneString.length > this.max)
35
+ validate(value = this.value) {
36
+ const str = typeof value === "number" ? value.toString() : value;
37
+ if (typeof str !== "string")
38
+ throw value ? `Must be ${this.one}` : "Required";
39
+ const sane = this.sanitize(str);
40
+ if (this.match && !this.match.test(sane))
41
+ throw str.length ? `Invalid ${this.one}` : "Required";
42
+ if (sane.length < this.min)
43
+ throw str.length ? `Minimum ${this.min} characters` : "Required";
44
+ if (sane.length > this.max)
43
45
  throw `Maximum ${this.max} characters`;
44
- if (this.match && !this.match.test(saneString))
45
- throw saneString ? "Invalid format" : "Required";
46
- return saneString;
46
+ return sane;
47
47
  }
48
48
  /** Sanitize the string by removing unwanted characters. */
49
49
  sanitize(str) {
@@ -25,9 +25,9 @@ export class URISchema extends StringSchema {
25
25
  const str = super.validate(unsafeValue);
26
26
  const uri = getURI(str);
27
27
  if (!uri)
28
- throw str ? "Invalid format" : "Required";
28
+ throw str ? `Invalid ${this.one} format` : "Required";
29
29
  if (this.schemes && !this.schemes.includes(uri.protocol))
30
- throw "Invalid URI scheme";
30
+ throw `Invalid ${this.one} scheme`;
31
31
  return uri.href;
32
32
  }
33
33
  }
@@ -28,9 +28,9 @@ export class URLSchema extends StringSchema {
28
28
  const str = super.validate(unsafeValue);
29
29
  const url = getURL(str, this.base);
30
30
  if (!url)
31
- throw str ? "Invalid format" : "Required";
31
+ throw str ? `Invalid ${this.one} format` : "Required";
32
32
  if (this.schemes && !this.schemes.includes(url.protocol))
33
- throw "Invalid URL scheme";
33
+ throw `Invalid ${this.one} scheme`;
34
34
  return url.href;
35
35
  }
36
36
  }