shelving 1.149.2 → 1.150.1

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/api/Endpoint.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { getResponse } from "../util/http.js";
2
- import { UNDEFINED } from "../util/validate.js";
2
+ import { UNDEFINED, getValid } from "../util/validate.js";
3
3
  /**
4
4
  * An abstract API resource definition, used to specify types for e.g. serverless functions.
5
5
  *
@@ -42,7 +42,9 @@ export class Endpoint {
42
42
  // Validate the payload against this endpoint's payload type.
43
43
  const payload = this.payload.validate(unsafePayload);
44
44
  // Call the callback with the validated payload to get the result.
45
- const result = await callback(payload, request);
45
+ const unsafeResult = await callback(payload, request);
46
+ // Validate the result against this endpoint's result type.
47
+ const result = getValid(unsafeResult, this.result);
46
48
  // Convert the result to a `Response` object.
47
49
  return getResponse(result);
48
50
  }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.149.2",
14
+ "version": "1.150.1",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -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: ALLOW_STRING({ a: "A", b: "B", c: "C" }),
10
+ group: CHOICE({ a: "A", b: "B", c: "C" }),
11
11
  tags: ARRAY(STRING),
12
12
  odd: BOOLEAN,
13
13
  even: BOOLEAN,
@@ -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>;
@@ -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
- }