shelving 1.149.1 → 1.150.0
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/ChoiceSchema.d.ts +19 -0
- package/schema/ChoiceSchema.js +21 -0
- package/schema/index.d.ts +1 -1
- package/schema/index.js +1 -1
- package/test/basics.js +2 -2
- package/util/validate.js +4 -2
- package/schema/AllowSchema.d.ts +0 -32
- package/schema/AllowSchema.js +0 -41
package/package.json
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SchemaOptions } from "./Schema.js";
|
|
2
|
+
import { Schema } from "./Schema.js";
|
|
3
|
+
export type ChoiceOptions<K extends string> = {
|
|
4
|
+
readonly [KK in K]: unknown;
|
|
5
|
+
};
|
|
6
|
+
/** Allowed options for `ChoiceSchema` */
|
|
7
|
+
export interface ChoiceSchemaOptions<K extends string> extends Omit<SchemaOptions, "value"> {
|
|
8
|
+
/** Specify correct options using a dictionary of entries. */
|
|
9
|
+
options: ChoiceOptions<K>;
|
|
10
|
+
}
|
|
11
|
+
/** Choose from an allowed set of values. */
|
|
12
|
+
export declare class ChoiceSchema<K extends string> extends Schema<K> {
|
|
13
|
+
readonly value: string;
|
|
14
|
+
readonly options: ChoiceOptions<K>;
|
|
15
|
+
constructor({ options, ...rest }: ChoiceSchemaOptions<K>);
|
|
16
|
+
validate(unsafeValue?: unknown): K;
|
|
17
|
+
}
|
|
18
|
+
/** Choose from an allowed set of values. */
|
|
19
|
+
export declare function CHOICE<K extends string>(options: ChoiceOptions<K>): ChoiceSchema<K>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ValueFeedback } from "../feedback/Feedback.js";
|
|
2
|
+
import { requireFirst } from "../util/array.js";
|
|
3
|
+
import { getKeys, isProp } from "../util/object.js";
|
|
4
|
+
import { Schema } from "./Schema.js";
|
|
5
|
+
/** Choose from an allowed set of values. */
|
|
6
|
+
export class ChoiceSchema extends Schema {
|
|
7
|
+
options;
|
|
8
|
+
constructor({ options, ...rest }) {
|
|
9
|
+
super({ value: requireFirst(getKeys(options)), ...rest });
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
validate(unsafeValue = this.value) {
|
|
13
|
+
if (typeof unsafeValue === "string" && isProp(this.options, unsafeValue))
|
|
14
|
+
return unsafeValue;
|
|
15
|
+
throw new ValueFeedback("Unknown value", unsafeValue);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Choose from an allowed set of values. */
|
|
19
|
+
export function CHOICE(options) {
|
|
20
|
+
return new ChoiceSchema({ options });
|
|
21
|
+
}
|
package/schema/index.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ export * from "./ThroughSchema.js";
|
|
|
3
3
|
export * from "./NullableSchema.js";
|
|
4
4
|
export * from "./OptionalSchema.js";
|
|
5
5
|
export * from "./RequiredSchema.js";
|
|
6
|
-
export * from "./AllowSchema.js";
|
|
7
6
|
export * from "./ArraySchema.js";
|
|
8
7
|
export * from "./BooleanSchema.js";
|
|
8
|
+
export * from "./ChoiceSchema.js";
|
|
9
9
|
export * from "./ColorSchema.js";
|
|
10
10
|
export * from "./DataSchema.js";
|
|
11
11
|
export * from "./DateSchema.js";
|
package/schema/index.js
CHANGED
|
@@ -5,9 +5,9 @@ export * from "./NullableSchema.js";
|
|
|
5
5
|
export * from "./OptionalSchema.js";
|
|
6
6
|
export * from "./RequiredSchema.js";
|
|
7
7
|
// Field schemas.
|
|
8
|
-
export * from "./AllowSchema.js";
|
|
9
8
|
export * from "./ArraySchema.js";
|
|
10
9
|
export * from "./BooleanSchema.js";
|
|
10
|
+
export * from "./ChoiceSchema.js";
|
|
11
11
|
export * from "./ColorSchema.js";
|
|
12
12
|
export * from "./DataSchema.js";
|
|
13
13
|
export * from "./DateSchema.js";
|
package/test/basics.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { ALLOW_STRING } from "../schema/AllowSchema.js";
|
|
2
1
|
import { ARRAY } from "../schema/ArraySchema.js";
|
|
3
2
|
import { BOOLEAN } from "../schema/BooleanSchema.js";
|
|
3
|
+
import { CHOICE } from "../schema/ChoiceSchema.js";
|
|
4
4
|
import { DATA } from "../schema/DataSchema.js";
|
|
5
5
|
import { NUMBER } from "../schema/NumberSchema.js";
|
|
6
6
|
import { STRING } from "../schema/StringSchema.js";
|
|
7
7
|
export const BASIC_SCHEMA = DATA({
|
|
8
8
|
str: STRING,
|
|
9
9
|
num: NUMBER,
|
|
10
|
-
group:
|
|
10
|
+
group: CHOICE({ a: "A", b: "B", c: "C" }),
|
|
11
11
|
tags: ARRAY(STRING),
|
|
12
12
|
odd: BOOLEAN,
|
|
13
13
|
even: BOOLEAN,
|
package/util/validate.js
CHANGED
|
@@ -14,8 +14,10 @@ export function getValid(value, validator, ErrorConstructor = ValueError, caller
|
|
|
14
14
|
return validator.validate(value);
|
|
15
15
|
}
|
|
16
16
|
catch (thrown) {
|
|
17
|
-
if (thrown instanceof Feedback)
|
|
18
|
-
|
|
17
|
+
if (thrown instanceof Feedback) {
|
|
18
|
+
const { message, ...rest } = thrown;
|
|
19
|
+
throw new ErrorConstructor(message, { ...rest, cause: thrown, caller });
|
|
20
|
+
}
|
|
19
21
|
throw thrown;
|
|
20
22
|
}
|
|
21
23
|
}
|
package/schema/AllowSchema.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { Entry } from "../util/entry.js";
|
|
2
|
-
import type { ImmutableMap, PossibleMap, PossibleStringMap } from "../util/map.js";
|
|
3
|
-
import type { SchemaOptions } from "./Schema.js";
|
|
4
|
-
import { Schema } from "./Schema.js";
|
|
5
|
-
/** Allowed options for `AllowSchama` */
|
|
6
|
-
export interface AllowSchemaOptions<K, T> extends Omit<SchemaOptions, "value"> {
|
|
7
|
-
/** Specify correct options using a `Map` or iterable set of entries. */
|
|
8
|
-
allow: PossibleMap<K, T>;
|
|
9
|
-
}
|
|
10
|
-
/** Define a valid value from an allowed set of values. */
|
|
11
|
-
export declare class AllowSchema<K, T> extends Schema<K> implements Iterable<Entry<K, T>> {
|
|
12
|
-
readonly value: K;
|
|
13
|
-
readonly allow: ImmutableMap<K, T>;
|
|
14
|
-
constructor(options: AllowSchemaOptions<K, T>);
|
|
15
|
-
validate(unsafeValue?: unknown): K;
|
|
16
|
-
/** Iterate over the the allowed options in `[key, value]` format. */
|
|
17
|
-
[Symbol.iterator](): Iterator<Entry<K, T>>;
|
|
18
|
-
}
|
|
19
|
-
/** Allowed options for `AllowStringSchama` */
|
|
20
|
-
export type AllowStringSchemaOptions<K extends string, T> = Omit<SchemaOptions, "value"> & {
|
|
21
|
-
/** Specify correct options using a `Map`, iterable set of entries, or an object with string keys. */
|
|
22
|
-
allow: PossibleStringMap<K, T>;
|
|
23
|
-
};
|
|
24
|
-
/** Define a valid string value from an allowed set of string values. */
|
|
25
|
-
export declare class AllowStringSchema<K extends string, T> extends AllowSchema<K, T> {
|
|
26
|
-
constructor({ allow, ...options }: AllowStringSchemaOptions<K, T>);
|
|
27
|
-
validator(unsafeValue?: unknown): K;
|
|
28
|
-
}
|
|
29
|
-
/** Valid value from an allowed set of values. */
|
|
30
|
-
export declare function ALLOW<K, T>(allow: PossibleMap<K, T>): AllowSchema<K, T>;
|
|
31
|
-
/** Valid string from an allowed set of values. */
|
|
32
|
-
export declare function ALLOW_STRING<K extends string, T>(allow: PossibleStringMap<K, T>): AllowSchema<K, T>;
|
package/schema/AllowSchema.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { ValueFeedback } from "../feedback/Feedback.js";
|
|
2
|
-
import { requireFirst } from "../util/array.js";
|
|
3
|
-
import { formatValue } from "../util/format.js";
|
|
4
|
-
import { getMap, isMapItem } from "../util/map.js";
|
|
5
|
-
import { Schema } from "./Schema.js";
|
|
6
|
-
/** Define a valid value from an allowed set of values. */
|
|
7
|
-
export class AllowSchema extends Schema {
|
|
8
|
-
allow;
|
|
9
|
-
constructor(options) {
|
|
10
|
-
const allow = getMap(options.allow);
|
|
11
|
-
const value = requireFirst(allow.keys());
|
|
12
|
-
super({ value, ...options });
|
|
13
|
-
this.allow = allow;
|
|
14
|
-
}
|
|
15
|
-
validate(unsafeValue = this.value) {
|
|
16
|
-
if (isMapItem(this.allow, unsafeValue))
|
|
17
|
-
return unsafeValue;
|
|
18
|
-
throw new ValueFeedback("Unknown value", unsafeValue);
|
|
19
|
-
}
|
|
20
|
-
/** Iterate over the the allowed options in `[key, value]` format. */
|
|
21
|
-
[Symbol.iterator]() {
|
|
22
|
-
return this.allow[Symbol.iterator]();
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
/** Define a valid string value from an allowed set of string values. */
|
|
26
|
-
export class AllowStringSchema extends AllowSchema {
|
|
27
|
-
constructor({ allow, ...options }) {
|
|
28
|
-
super({ allow: getMap(allow), ...options });
|
|
29
|
-
}
|
|
30
|
-
validator(unsafeValue = this.value) {
|
|
31
|
-
return super.validate(formatValue(unsafeValue));
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/** Valid value from an allowed set of values. */
|
|
35
|
-
export function ALLOW(allow) {
|
|
36
|
-
return new AllowSchema({ allow });
|
|
37
|
-
}
|
|
38
|
-
/** Valid string from an allowed set of values. */
|
|
39
|
-
export function ALLOW_STRING(allow) {
|
|
40
|
-
return new AllowStringSchema({ allow });
|
|
41
|
-
}
|