pema 0.12.0 → 0.12.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.
@@ -8,12 +8,13 @@ import Storable from "#Storable";
8
8
  import type { JSONValue } from "@rcompat/type";
9
9
  export type ParsedJSON = Parsed<JSONValue>;
10
10
  type JSONInput = ParsedJSON | undefined;
11
- type JSONInfer<S extends JSONInput> = S extends ParsedJSON ? Infer<S> : JSONValue;
12
- export default class JSONType<S extends JSONInput = undefined> extends Storable<"json", JSONInfer<S>> {
11
+ type JSONInfer<S extends JSONInput, D extends boolean> = D extends true ? Record<string, JSONValue> : S extends ParsedJSON ? Infer<S> : JSONValue;
12
+ export default class JSONType<S extends JSONInput = undefined, D extends boolean = false> extends Storable<"json", JSONInfer<S, D>> {
13
13
  #private;
14
- constructor(inner?: S);
14
+ constructor(inner?: S, dictionary?: D);
15
15
  get name(): "json";
16
16
  get datatype(): "json";
17
+ dict(this: JSONType<undefined, false>): JSONType<undefined, true>;
17
18
  optional(): OptionalType<this>;
18
19
  default(value: (() => Infer<this>) | Infer<this>): DefaultType<this, Infer<this>>;
19
20
  parse(u: unknown, options?: ParseOptions): Infer<this>;
@@ -21,6 +22,7 @@ export default class JSONType<S extends JSONInput = undefined> extends Storable<
21
22
  type: "json";
22
23
  datatype: "json";
23
24
  of?: Serialized;
25
+ dictionary?: true;
24
26
  };
25
27
  }
26
28
  export {};
@@ -29,9 +29,11 @@ function json_with_undefined_object_values(x, ancestors = new Set()) {
29
29
  }
30
30
  export default class JSONType extends Storable {
31
31
  #inner;
32
- constructor(inner) {
32
+ #dictionary;
33
+ constructor(inner, dictionary = false) {
33
34
  super();
34
35
  this.#inner = inner;
36
+ this.#dictionary = dictionary;
35
37
  }
36
38
  get name() {
37
39
  return "json";
@@ -39,6 +41,9 @@ export default class JSONType extends Storable {
39
41
  get datatype() {
40
42
  return "json";
41
43
  }
44
+ dict() {
45
+ return new JSONType(undefined, true);
46
+ }
42
47
  optional() {
43
48
  return new OptionalType(this);
44
49
  }
@@ -47,6 +52,9 @@ export default class JSONType extends Storable {
47
52
  }
48
53
  parse(u, options = {}) {
49
54
  const x = resolve(u);
55
+ if (this.#dictionary && !is.dict(x)) {
56
+ throw E.invalid_type(x, "dictionary", options);
57
+ }
50
58
  // delegate to inner schema for typed validation
51
59
  if (is.defined(this.#inner))
52
60
  return this.#inner.parse(x, options);
@@ -60,6 +68,7 @@ export default class JSONType extends Storable {
60
68
  type: "json",
61
69
  datatype: "json",
62
70
  ...is.defined(this.#inner) && { of: this.#inner.toJSON() },
71
+ ...this.#dictionary && { dictionary: true },
63
72
  };
64
73
  }
65
74
  }
@@ -73,6 +73,7 @@ type StructuralSerialized = {
73
73
  type: "json";
74
74
  datatype: "json";
75
75
  of?: Serialized;
76
+ dictionary?: true;
76
77
  } | {
77
78
  type: "omit";
78
79
  properties: Dict<Serialized>;
package/package.json CHANGED
@@ -48,5 +48,5 @@
48
48
  "test": "proby"
49
49
  },
50
50
  "type": "module",
51
- "version": "0.12.0"
51
+ "version": "0.12.1"
52
52
  }