shelving 1.86.5 → 1.86.6
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 +1 -1
- package/schema/ArraySchema.d.ts +2 -2
- package/schema/ArraySchema.js +3 -3
- package/schema/BooleanSchema.d.ts +1 -1
- package/schema/DataSchema.d.ts +1 -1
- package/schema/DateSchema.d.ts +4 -4
- package/schema/DateSchema.js +3 -3
- package/schema/DictionarySchema.d.ts +5 -5
- package/schema/DictionarySchema.js +3 -5
- package/schema/LinkSchema.d.ts +3 -3
- package/schema/LinkSchema.js +1 -3
- package/schema/NumberSchema.d.ts +7 -7
- package/schema/NumberSchema.js +3 -3
- package/schema/Schema.d.ts +8 -6
- package/schema/Schema.js +3 -1
- package/schema/StringSchema.d.ts +10 -10
- package/schema/StringSchema.js +2 -2
- package/schema/TimeSchema.d.ts +5 -5
- package/schema/TimeSchema.js +3 -3
package/package.json
CHANGED
package/schema/ArraySchema.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type ArraySchemaOptions<T> = SchemaOptions & {
|
|
|
6
6
|
readonly value?: ImmutableArray;
|
|
7
7
|
readonly items: Validator<T>;
|
|
8
8
|
readonly min?: number;
|
|
9
|
-
readonly max?: number
|
|
9
|
+
readonly max?: number;
|
|
10
10
|
readonly unique?: boolean;
|
|
11
11
|
};
|
|
12
12
|
/**
|
|
@@ -41,7 +41,7 @@ export declare class ArraySchema<T> extends Schema<ImmutableArray<T>> {
|
|
|
41
41
|
readonly items: Validator<T>;
|
|
42
42
|
readonly unique: boolean;
|
|
43
43
|
readonly min: number;
|
|
44
|
-
readonly max: number
|
|
44
|
+
readonly max: number;
|
|
45
45
|
constructor({ value, items, unique, min, max, ...options }: ArraySchemaOptions<T>);
|
|
46
46
|
validate(unsafeValue?: unknown): ImmutableArray<T>;
|
|
47
47
|
}
|
package/schema/ArraySchema.js
CHANGED
|
@@ -30,7 +30,7 @@ import { Schema } from "./Schema.js";
|
|
|
30
30
|
* schema.validate(["a", null], schema); // Throws Invalids({ "1": Invalid('Must be a string') });
|
|
31
31
|
*/
|
|
32
32
|
export class ArraySchema extends Schema {
|
|
33
|
-
constructor({ value = [], items, unique = false, min = 0, max =
|
|
33
|
+
constructor({ value = [], items, unique = false, min = 0, max = Infinity, ...options }) {
|
|
34
34
|
super(options);
|
|
35
35
|
this.value = value;
|
|
36
36
|
this.items = items;
|
|
@@ -43,9 +43,9 @@ export class ArraySchema extends Schema {
|
|
|
43
43
|
throw new InvalidFeedback("Must be array", { value: unsafeValue });
|
|
44
44
|
const safeArray = validateArray(unsafeValue, this.items);
|
|
45
45
|
const dedupedArray = this.unique ? uniqueArray(safeArray) : safeArray;
|
|
46
|
-
if (
|
|
46
|
+
if (dedupedArray.length < this.min)
|
|
47
47
|
throw new InvalidFeedback(dedupedArray.length ? `Minimum ${this.min} items` : "Required", { value: dedupedArray });
|
|
48
|
-
if (
|
|
48
|
+
if (dedupedArray.length > this.max)
|
|
49
49
|
throw new InvalidFeedback(`Maximum ${this.max} items`, { value: dedupedArray });
|
|
50
50
|
return dedupedArray;
|
|
51
51
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Schema, SchemaOptions } from "./Schema.js";
|
|
2
2
|
/** Allowed options for `BooleanSchema` */
|
|
3
3
|
export type BooleanSchemaOptions = SchemaOptions & {
|
|
4
|
-
readonly value?: boolean;
|
|
4
|
+
readonly value?: boolean | undefined;
|
|
5
5
|
};
|
|
6
6
|
/** Define a valid boolean. */
|
|
7
7
|
export declare class BooleanSchema extends Schema<boolean> {
|
package/schema/DataSchema.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Schema, SchemaOptions } from "./Schema.js";
|
|
|
5
5
|
/** Allowed options for `DataSchema` */
|
|
6
6
|
export type DataSchemaOptions<T extends Data> = SchemaOptions & {
|
|
7
7
|
readonly props: Validators<T>;
|
|
8
|
-
readonly value?: Partial<T
|
|
8
|
+
readonly value?: Partial<T> | undefined;
|
|
9
9
|
};
|
|
10
10
|
/** Validate a data object. */
|
|
11
11
|
export declare class DataSchema<T extends Data> extends Schema<T> {
|
package/schema/DateSchema.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { PossibleDate } from "../util/date.js";
|
|
1
|
+
import { PossibleDate, PossibleOptionalDate } from "../util/date.js";
|
|
2
2
|
import { Schema, SchemaOptions } from "./Schema.js";
|
|
3
3
|
/** Allowed options for `DateSchema` */
|
|
4
4
|
export type DateSchemaOptions = SchemaOptions & {
|
|
5
|
-
readonly value?: PossibleDate;
|
|
6
|
-
readonly min?:
|
|
7
|
-
readonly max?:
|
|
5
|
+
readonly value?: PossibleDate | undefined;
|
|
6
|
+
readonly min?: PossibleOptionalDate | undefined;
|
|
7
|
+
readonly max?: PossibleOptionalDate | undefined;
|
|
8
8
|
};
|
|
9
9
|
/** Define a valid date in YMD format, e.g. `2005-09-12` */
|
|
10
10
|
export declare class DateSchema extends Schema<string> {
|
package/schema/DateSchema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { formatDate,
|
|
1
|
+
import { formatDate, getOptionalDate, getYMD } from "../util/date.js";
|
|
2
2
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
3
3
|
import { Schema } from "./Schema.js";
|
|
4
4
|
import { OPTIONAL } from "./OptionalSchema.js";
|
|
@@ -7,8 +7,8 @@ export class DateSchema extends Schema {
|
|
|
7
7
|
constructor({ value = "now", min = null, max = null, ...options }) {
|
|
8
8
|
super(options);
|
|
9
9
|
this.value = value;
|
|
10
|
-
this.min =
|
|
11
|
-
this.max =
|
|
10
|
+
this.min = getOptionalDate(min);
|
|
11
|
+
this.max = getOptionalDate(max);
|
|
12
12
|
}
|
|
13
13
|
validate(unsafeValue = this.value) {
|
|
14
14
|
const date = getOptionalDate(unsafeValue);
|
|
@@ -4,16 +4,16 @@ import { Schema, SchemaOptions } from "./Schema.js";
|
|
|
4
4
|
/** Allowed options for `DictionarySchema` */
|
|
5
5
|
export type DictionarySchemaOptions<T> = SchemaOptions & {
|
|
6
6
|
readonly items: Validator<T>;
|
|
7
|
-
readonly value?: ImmutableDictionary;
|
|
8
|
-
readonly min?: number |
|
|
9
|
-
readonly max?: number |
|
|
7
|
+
readonly value?: ImmutableDictionary | undefined;
|
|
8
|
+
readonly min?: number | undefined;
|
|
9
|
+
readonly max?: number | undefined;
|
|
10
10
|
};
|
|
11
11
|
/** Validate a dictionary object (whose props are all the same with string keys). */
|
|
12
12
|
export declare class DictionarySchema<T> extends Schema<ImmutableDictionary<T>> {
|
|
13
13
|
readonly value: ImmutableDictionary;
|
|
14
14
|
readonly items: Validator<T>;
|
|
15
|
-
readonly min: number
|
|
16
|
-
readonly max: number
|
|
15
|
+
readonly min: number;
|
|
16
|
+
readonly max: number;
|
|
17
17
|
constructor({ value, items, min, max, ...rest }: DictionarySchemaOptions<T>);
|
|
18
18
|
validate(unsafeValue?: unknown): ImmutableDictionary<T>;
|
|
19
19
|
}
|
|
@@ -4,10 +4,8 @@ import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
|
4
4
|
import { Schema } from "./Schema.js";
|
|
5
5
|
/** Validate a dictionary object (whose props are all the same with string keys). */
|
|
6
6
|
export class DictionarySchema extends Schema {
|
|
7
|
-
constructor({ value = {}, items, min =
|
|
7
|
+
constructor({ value = {}, items, min = 0, max = Infinity, ...rest }) {
|
|
8
8
|
super(rest);
|
|
9
|
-
this.min = null;
|
|
10
|
-
this.max = null;
|
|
11
9
|
this.items = items;
|
|
12
10
|
this.value = value;
|
|
13
11
|
this.min = min;
|
|
@@ -18,9 +16,9 @@ export class DictionarySchema extends Schema {
|
|
|
18
16
|
throw new InvalidFeedback("Must be object", { value: unsafeValue });
|
|
19
17
|
const unsafeEntries = Object.entries(unsafeValue);
|
|
20
18
|
const safeObject = Object.fromEntries(validateEntries(unsafeEntries, this.items));
|
|
21
|
-
if (
|
|
19
|
+
if (unsafeEntries.length < this.min)
|
|
22
20
|
throw new InvalidFeedback(unsafeEntries.length ? `Minimum ${this.min} items` : "Required", { value: safeObject });
|
|
23
|
-
if (
|
|
21
|
+
if (unsafeEntries.length > this.max)
|
|
24
22
|
throw new InvalidFeedback(`Maximum ${this.max} items`, { value: safeObject });
|
|
25
23
|
return safeObject;
|
|
26
24
|
}
|
package/schema/LinkSchema.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { StringSchema, StringSchemaOptions } from "./StringSchema.js";
|
|
2
2
|
/** Allowed options for `LinkSchema` */
|
|
3
3
|
export type LinkSchemaOptions = StringSchemaOptions & {
|
|
4
|
-
readonly schemes?: string[];
|
|
5
|
-
readonly hosts?: string[] |
|
|
4
|
+
readonly schemes?: string[] | undefined;
|
|
5
|
+
readonly hosts?: string[] | undefined;
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
8
|
* Type of `StringSchema` that defines a valid URL.
|
|
@@ -15,7 +15,7 @@ export declare class LinkSchema extends StringSchema {
|
|
|
15
15
|
readonly min = 1;
|
|
16
16
|
readonly max = 512;
|
|
17
17
|
readonly schemes: string[];
|
|
18
|
-
readonly hosts: string[] |
|
|
18
|
+
readonly hosts: string[] | undefined;
|
|
19
19
|
constructor({ schemes, hosts, ...rest }: LinkSchemaOptions);
|
|
20
20
|
validate(unsafeValue: unknown): string;
|
|
21
21
|
}
|
package/schema/LinkSchema.js
CHANGED
|
@@ -9,13 +9,11 @@ import { StringSchema } from "./StringSchema.js";
|
|
|
9
9
|
* - Falsy values are converted to `""` empty string.
|
|
10
10
|
*/
|
|
11
11
|
export class LinkSchema extends StringSchema {
|
|
12
|
-
constructor({ schemes = ["http:", "https:"], hosts
|
|
12
|
+
constructor({ schemes = ["http:", "https:"], hosts, ...rest }) {
|
|
13
13
|
super(rest);
|
|
14
14
|
this.type = "url";
|
|
15
15
|
this.min = 1;
|
|
16
16
|
this.max = 512;
|
|
17
|
-
this.schemes = ["http:", "https:"];
|
|
18
|
-
this.hosts = null;
|
|
19
17
|
this.schemes = schemes;
|
|
20
18
|
this.hosts = hosts;
|
|
21
19
|
}
|
package/schema/NumberSchema.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { Schema, SchemaOptions } from "./Schema.js";
|
|
2
2
|
/** Allowed options for `NumberSchema` */
|
|
3
3
|
export type NumberSchemaOptions = SchemaOptions & {
|
|
4
|
-
readonly value?: number |
|
|
5
|
-
readonly min?: number |
|
|
6
|
-
readonly max?: number |
|
|
7
|
-
readonly step?: number | null;
|
|
4
|
+
readonly value?: number | undefined;
|
|
5
|
+
readonly min?: number | undefined;
|
|
6
|
+
readonly max?: number | undefined;
|
|
7
|
+
readonly step?: number | null | undefined;
|
|
8
8
|
};
|
|
9
9
|
/** Schema that defines a valid number. */
|
|
10
10
|
export declare class NumberSchema extends Schema<number> {
|
|
11
|
-
readonly value: number
|
|
12
|
-
readonly min: number
|
|
13
|
-
readonly max: number
|
|
11
|
+
readonly value: number;
|
|
12
|
+
readonly min: number;
|
|
13
|
+
readonly max: number;
|
|
14
14
|
readonly step: number | null;
|
|
15
15
|
constructor({ value, min, max, step, ...rest }: NumberSchemaOptions);
|
|
16
16
|
validate(unsafeValue?: unknown): number;
|
package/schema/NumberSchema.js
CHANGED
|
@@ -4,7 +4,7 @@ import { Schema } from "./Schema.js";
|
|
|
4
4
|
import { OPTIONAL } from "./OptionalSchema.js";
|
|
5
5
|
/** Schema that defines a valid number. */
|
|
6
6
|
export class NumberSchema extends Schema {
|
|
7
|
-
constructor({ value = 0, min =
|
|
7
|
+
constructor({ value = 0, min = -Infinity, max = Infinity, step = null, ...rest }) {
|
|
8
8
|
super(rest);
|
|
9
9
|
this.value = value;
|
|
10
10
|
this.min = min;
|
|
@@ -16,9 +16,9 @@ export class NumberSchema extends Schema {
|
|
|
16
16
|
if (typeof unsafeNumber !== "number")
|
|
17
17
|
throw new InvalidFeedback("Must be number", { value: unsafeValue });
|
|
18
18
|
const safeNumber = typeof this.step === "number" ? roundStep(unsafeNumber, this.step) : unsafeNumber;
|
|
19
|
-
if (
|
|
19
|
+
if (safeNumber > this.max)
|
|
20
20
|
throw new InvalidFeedback(`Maximum ${formatNumber(this.max)}`, { value: safeNumber });
|
|
21
|
-
if (
|
|
21
|
+
if (safeNumber < this.min)
|
|
22
22
|
throw new InvalidFeedback(`Minimum ${formatNumber(this.min)}`, { value: safeNumber });
|
|
23
23
|
return safeNumber;
|
|
24
24
|
}
|
package/schema/Schema.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ import type { Validatable } from "../util/validate.js";
|
|
|
2
2
|
/** Options allowed by a `Schema` instance. */
|
|
3
3
|
export type SchemaOptions = {
|
|
4
4
|
/** Title of the schema, e.g. for using as the title of a corresponding field. */
|
|
5
|
-
readonly title?: string |
|
|
5
|
+
readonly title?: string | undefined;
|
|
6
6
|
/** Description of the schema, e.g. for using as a description in a corresponding field. */
|
|
7
|
-
readonly description?: string |
|
|
7
|
+
readonly description?: string | undefined;
|
|
8
8
|
/** Placeholder of the schema, e.g. for using as a placeholder in a corresponding field. */
|
|
9
|
-
readonly placeholder?: string |
|
|
9
|
+
readonly placeholder?: string | undefined;
|
|
10
10
|
/** Default value for the schema if `validate()` is called with an `undefined` value. */
|
|
11
11
|
readonly value?: unknown;
|
|
12
12
|
};
|
|
@@ -17,14 +17,16 @@ export type SchemaOptions = {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare abstract class Schema<T extends unknown = unknown> implements Validatable<T> {
|
|
19
19
|
/** Title of the schema, e.g. for using as the title of a corresponding field. */
|
|
20
|
-
readonly title: string |
|
|
20
|
+
readonly title: string | undefined;
|
|
21
21
|
/** Description of the schema, e.g. for using as a description in a corresponding field. */
|
|
22
|
-
readonly description: string |
|
|
22
|
+
readonly description: string | undefined;
|
|
23
23
|
/** Placeholder of the schema, e.g. for using as a placeholder in a corresponding field. */
|
|
24
|
-
readonly placeholder: string |
|
|
24
|
+
readonly placeholder: string | undefined;
|
|
25
25
|
/** Default value for the schema if `validate()` is called with an `undefined` value. */
|
|
26
26
|
readonly value: unknown;
|
|
27
27
|
constructor({ title, description, placeholder, value }: SchemaOptions);
|
|
28
28
|
/** Every schema must implement a `validate()` method. */
|
|
29
29
|
abstract validate(unsafeValue: unknown): T;
|
|
30
30
|
}
|
|
31
|
+
/** Is an unknown value a `Schema` instance? */
|
|
32
|
+
export declare const isSchema: <T extends Schema<unknown>>(v: unknown) => v is T;
|
package/schema/Schema.js
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
* - `validate()` returns `Invalid` if value was not valid.
|
|
5
5
|
*/
|
|
6
6
|
export class Schema {
|
|
7
|
-
constructor({ title
|
|
7
|
+
constructor({ title, description, placeholder, value }) {
|
|
8
8
|
this.title = title;
|
|
9
9
|
this.description = description;
|
|
10
10
|
this.placeholder = placeholder;
|
|
11
11
|
this.value = value;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
/** Is an unknown value a `Schema` instance? */
|
|
15
|
+
export const isSchema = (v) => v instanceof Schema;
|
package/schema/StringSchema.d.ts
CHANGED
|
@@ -5,13 +5,13 @@ export type HtmlInputType = "text" | "password" | "color" | "date" | "email" | "
|
|
|
5
5
|
export type Sanitizer = (str: string) => string;
|
|
6
6
|
/** Options for `StringSchema` */
|
|
7
7
|
export type StringSchemaOptions = SchemaOptions & {
|
|
8
|
-
readonly value?: string;
|
|
9
|
-
readonly type?: HtmlInputType;
|
|
10
|
-
readonly min?: number;
|
|
11
|
-
readonly max?: number |
|
|
12
|
-
readonly match?: RegExp |
|
|
13
|
-
readonly sanitizer?: Sanitizer |
|
|
14
|
-
readonly multiline?: boolean;
|
|
8
|
+
readonly value?: string | undefined;
|
|
9
|
+
readonly type?: HtmlInputType | undefined;
|
|
10
|
+
readonly min?: number | undefined;
|
|
11
|
+
readonly max?: number | undefined;
|
|
12
|
+
readonly match?: RegExp | undefined;
|
|
13
|
+
readonly sanitizer?: Sanitizer | undefined;
|
|
14
|
+
readonly multiline?: boolean | undefined;
|
|
15
15
|
};
|
|
16
16
|
/**
|
|
17
17
|
* Schema that defines a valid string.
|
|
@@ -37,9 +37,9 @@ export declare class StringSchema extends Schema<string> {
|
|
|
37
37
|
readonly value: string;
|
|
38
38
|
readonly type: HtmlInputType;
|
|
39
39
|
readonly min: number;
|
|
40
|
-
readonly max: number
|
|
41
|
-
readonly match: RegExp |
|
|
42
|
-
readonly sanitizer: Sanitizer |
|
|
40
|
+
readonly max: number;
|
|
41
|
+
readonly match: RegExp | undefined;
|
|
42
|
+
readonly sanitizer: Sanitizer | undefined;
|
|
43
43
|
readonly multiline: boolean;
|
|
44
44
|
constructor({ value, type, min, max, match, sanitizer, multiline, ...rest }: StringSchemaOptions);
|
|
45
45
|
validate(unsafeValue?: unknown): string;
|
package/schema/StringSchema.js
CHANGED
|
@@ -22,7 +22,7 @@ import { Schema } from "./Schema.js";
|
|
|
22
22
|
* schema.validate('j'); // Throws 'Minimum 3 chaacters'
|
|
23
23
|
*/
|
|
24
24
|
export class StringSchema extends Schema {
|
|
25
|
-
constructor({ value = "", type = "text", min = 0, max =
|
|
25
|
+
constructor({ value = "", type = "text", min = 0, max = Infinity, match, sanitizer, multiline = false, ...rest }) {
|
|
26
26
|
super(rest);
|
|
27
27
|
this.type = type;
|
|
28
28
|
this.value = value;
|
|
@@ -39,7 +39,7 @@ export class StringSchema extends Schema {
|
|
|
39
39
|
const safeString = this.sanitize(unsafeString);
|
|
40
40
|
if (safeString.length < this.min)
|
|
41
41
|
throw new InvalidFeedback(safeString ? `Minimum ${this.min} characters` : "Required", { value: safeString });
|
|
42
|
-
if (
|
|
42
|
+
if (safeString.length > this.max)
|
|
43
43
|
throw new InvalidFeedback(`Maximum ${this.max} characters`, { value: safeString });
|
|
44
44
|
if (this.match && !this.match.test(safeString))
|
|
45
45
|
throw new InvalidFeedback(safeString ? "Invalid format" : "Required", { value: safeString });
|
package/schema/TimeSchema.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { PossibleTime, Time } from "../util/time.js";
|
|
1
|
+
import { PossibleTime, Time, PossibleOptionalTime } from "../util/time.js";
|
|
2
2
|
import { Schema, SchemaOptions } from "./Schema.js";
|
|
3
3
|
/** Allowed options for `TimeSchama` */
|
|
4
4
|
export type TimeSchemaOptions = SchemaOptions & {
|
|
5
|
-
readonly value?: PossibleTime;
|
|
6
|
-
readonly min?:
|
|
7
|
-
readonly max?:
|
|
8
|
-
readonly step?: number | null;
|
|
5
|
+
readonly value?: PossibleTime | undefined;
|
|
6
|
+
readonly min?: PossibleOptionalTime | undefined;
|
|
7
|
+
readonly max?: PossibleOptionalTime | undefined;
|
|
8
|
+
readonly step?: number | null | undefined;
|
|
9
9
|
};
|
|
10
10
|
/** Define a valid time in 24h hh:mm:ss.fff format, e.g. `23:59` or `24:00 */
|
|
11
11
|
export declare class TimeSchema extends Schema<string> {
|
package/schema/TimeSchema.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
2
2
|
import { roundStep } from "../util/number.js";
|
|
3
|
-
import { getOptionalTime, Time
|
|
3
|
+
import { getOptionalTime, Time } from "../util/time.js";
|
|
4
4
|
import { OPTIONAL } from "./OptionalSchema.js";
|
|
5
5
|
import { Schema } from "./Schema.js";
|
|
6
6
|
/** Define a valid time in 24h hh:mm:ss.fff format, e.g. `23:59` or `24:00 */
|
|
@@ -8,8 +8,8 @@ export class TimeSchema extends Schema {
|
|
|
8
8
|
constructor({ value = "now", min = null, max = null, step = 60, ...options }) {
|
|
9
9
|
super(options);
|
|
10
10
|
this.value = value;
|
|
11
|
-
this.min =
|
|
12
|
-
this.max =
|
|
11
|
+
this.min = getOptionalTime(min);
|
|
12
|
+
this.max = getOptionalTime(max);
|
|
13
13
|
this.step = step;
|
|
14
14
|
}
|
|
15
15
|
validate(unsafeValue = this.value) {
|