pema 0.12.0 → 0.13.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.
@@ -3,8 +3,11 @@ import coerce from "#coerce/bigint";
3
3
  import CoerceKey from "#CoerceKey";
4
4
  import NumericType from "#NumericType";
5
5
  import type Storable from "#Storable";
6
- export default class BigUintType<T extends BigUintDataType = "u64", L extends boolean | undefined = undefined> extends NumericType<T, bigint, "BigUintType", L> implements Storable<T> {
6
+ export default class BigUintType<T extends BigUintDataType = "u64", L extends boolean | undefined = undefined, G extends boolean = false> extends NumericType<T, bigint, "BigUintType", L, G> implements Storable<T> {
7
7
  [CoerceKey]: typeof coerce;
8
+ sequence(): BigUintType<T, L, true>;
9
+ default<S extends bigint>(value: (() => S) | S): import("./DefaultType.js").default<this, S>;
10
+ optional(): import("./OptionalType.js").default<this>;
8
11
  get name(): string;
9
12
  }
10
13
  //# sourceMappingURL=BigUintType.d.ts.map
@@ -3,6 +3,21 @@ import CoerceKey from "#CoerceKey";
3
3
  import NumericType from "#NumericType";
4
4
  export default class BigUintType extends NumericType {
5
5
  [CoerceKey] = coerce;
6
+ sequence() {
7
+ return this.with({ sequence: true });
8
+ }
9
+ default(value) {
10
+ if (this.generated === "sequence") {
11
+ throw new TypeError("a sequence cannot also have a default value");
12
+ }
13
+ return super.default(value);
14
+ }
15
+ optional() {
16
+ if (this.generated === "sequence") {
17
+ throw new TypeError("a sequence cannot be optional in stored output");
18
+ }
19
+ return super.optional();
20
+ }
6
21
  get name() {
7
22
  return "bigint";
8
23
  }
@@ -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
  }
@@ -8,21 +8,24 @@ import type Validator from "#Validator";
8
8
  import type { Dict } from "@rcompat/type";
9
9
  type Next<T> = {
10
10
  options?: ParseOptions;
11
+ sequence?: boolean;
11
12
  validators?: Validator<T>[];
12
13
  };
13
- export default abstract class NumericType<Key extends DataKey, T extends bigint | number, Name extends string, M extends Mode = undefined> extends PrimitiveType<T, Name> implements Storable<Key> {
14
+ export default abstract class NumericType<Key extends DataKey, T extends bigint | number, Name extends string, M extends Mode = undefined, G extends boolean = false> extends PrimitiveType<T, Name> implements Storable<Key> {
14
15
  #private;
15
16
  [Loose]: M;
16
- constructor(datatype: Key, mode?: M, validators?: Validator<T>[], options?: ParseOptions);
17
+ constructor(datatype: Key, mode?: M, validators?: Validator<T>[], options?: ParseOptions, sequence?: G);
17
18
  with(next: Next<T>): this;
18
19
  values(anyof: Dict<T>): this;
19
20
  range(from: T, to: T): this;
20
21
  min(from: T): this;
21
22
  max(to: T): this;
22
23
  get datatype(): Key;
24
+ get generated(): G extends true ? "sequence" : undefined;
23
25
  toJSON(): {
24
26
  type: "number" | "bigint";
25
27
  datatype: Key;
28
+ sequence?: true | undefined;
26
29
  };
27
30
  }
28
31
  export {};
@@ -6,15 +6,17 @@ import range from "#validator/range";
6
6
  import values from "#validator/values";
7
7
  export default class NumericType extends PrimitiveType {
8
8
  #datatype;
9
+ #sequence;
9
10
  [Loose];
10
- constructor(datatype, mode = undefined, validators = [], options = {}) {
11
+ constructor(datatype, mode = undefined, validators = [], options = {}, sequence = false) {
11
12
  super(validators, options);
12
13
  this.#datatype = datatype;
14
+ this.#sequence = sequence;
13
15
  this[Loose] = mode;
14
16
  }
15
17
  with(next) {
16
18
  const Constructor = this.constructor;
17
- return new Constructor(this.#datatype, this[Loose], [...this.validators, ...next.validators ?? []], { ...this.options, ...next.options ?? {} });
19
+ return new Constructor(this.#datatype, this[Loose], [...this.validators, ...next.validators ?? []], { ...this.options, ...next.options ?? {} }, (next.sequence ?? this.#sequence));
18
20
  }
19
21
  values(anyof) {
20
22
  return this.with({ validators: [values(anyof)] });
@@ -31,10 +33,14 @@ export default class NumericType extends PrimitiveType {
31
33
  get datatype() {
32
34
  return this.#datatype;
33
35
  }
36
+ get generated() {
37
+ return (this.#sequence ? "sequence" : undefined);
38
+ }
34
39
  toJSON() {
35
40
  return {
36
41
  type: this.name,
37
42
  datatype: this.#datatype,
43
+ ...this.#sequence ? { sequence: true } : {},
38
44
  };
39
45
  }
40
46
  }
@@ -22,6 +22,7 @@ export default class RecordType<Key extends RecordTypeKey, Value extends Parsed<
22
22
  } | {
23
23
  type: "number" | "bigint";
24
24
  datatype: "f64";
25
+ sequence?: true | undefined;
25
26
  } | {
26
27
  type: "symbol";
27
28
  };
@@ -0,0 +1,5 @@
1
+ type Sequence = {
2
+ readonly generated: "sequence";
3
+ };
4
+ export type { Sequence as default };
5
+ //# sourceMappingURL=Sequence.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Sequence.js.map
@@ -31,9 +31,11 @@ type PrimitiveSerialized = ({
31
31
  };
32
32
  type NumberSerialized = {
33
33
  type: "number";
34
+ sequence?: true;
34
35
  } & DT;
35
36
  type BigIntSerialized = {
36
37
  type: "bigint";
38
+ sequence?: true;
37
39
  } & DT;
38
40
  type StructuralSerialized = {
39
41
  type: "newable";
@@ -73,6 +75,7 @@ type StructuralSerialized = {
73
75
  type: "json";
74
76
  datatype: "json";
75
77
  of?: Serialized;
78
+ dictionary?: true;
76
79
  } | {
77
80
  type: "omit";
78
81
  properties: Dict<Serialized>;
@@ -5,7 +5,7 @@ import PartialType from "#PartialType";
5
5
  import type StoreSchema from "#StoreSchema";
6
6
  export default class StoreType<S extends StoreSchema> extends ObjectType<S> {
7
7
  #private;
8
- constructor(properties: S, pk?: string | null);
8
+ constructor(properties: S, pk?: string | null, sequences?: readonly string[]);
9
9
  get name(): string;
10
10
  parse(u: unknown, options?: ParseOptions): Infer<this>;
11
11
  partial(): PartialType<any, undefined>;
@@ -6,9 +6,11 @@ import resolve from "#resolve";
6
6
  import is from "@rcompat/is";
7
7
  export default class StoreType extends ObjectType {
8
8
  #pk;
9
- constructor(properties, pk = null) {
9
+ #sequences;
10
+ constructor(properties, pk = null, sequences = []) {
10
11
  super(properties);
11
12
  this.#pk = pk;
13
+ this.#sequences = new Set(sequences);
12
14
  }
13
15
  get name() {
14
16
  return "store";
@@ -21,7 +23,8 @@ export default class StoreType extends ObjectType {
21
23
  const input = x ?? {};
22
24
  const out = {};
23
25
  for (const k in this.properties) {
24
- if (k === this.#pk && !(k in input))
26
+ const generated = k === this.#pk || this.#sequences.has(k);
27
+ if (generated && !(k in input))
25
28
  continue;
26
29
  const parsed = this.properties[k].parse(input[k], next(k, $options));
27
30
  if (is.defined(parsed))
@@ -4,8 +4,11 @@ import NumericType from "#NumericType";
4
4
  import type ParseOptions from "#ParseOptions";
5
5
  import type Storable from "#Storable";
6
6
  import type UintDataType from "#UintDataType";
7
- export default class UintType<T extends UintDataType, L extends boolean | undefined = undefined> extends NumericType<T, number, "UintType", L> implements Storable<T> {
7
+ export default class UintType<T extends UintDataType, L extends boolean | undefined = undefined, G extends boolean = false> extends NumericType<T, number, "UintType", L, G> implements Storable<T> {
8
8
  [CoerceKey]: typeof coerce;
9
+ sequence(): UintType<T, L, true>;
10
+ default<S extends number>(value: (() => S) | S): import("./DefaultType.js").default<this, S>;
11
+ optional(): import("./OptionalType.js").default<this>;
9
12
  get name(): string;
10
13
  /**
11
14
  * Value is a non-privileged port number (1000 - 65535).
@@ -5,6 +5,21 @@ import integer from "#validator/integer";
5
5
  import port from "#validator/port";
6
6
  export default class UintType extends NumericType {
7
7
  [CoerceKey] = coerce;
8
+ sequence() {
9
+ return this.with({ sequence: true });
10
+ }
11
+ default(value) {
12
+ if (this.generated === "sequence") {
13
+ throw new TypeError("a sequence cannot also have a default value");
14
+ }
15
+ return super.default(value);
16
+ }
17
+ optional() {
18
+ if (this.generated === "sequence") {
19
+ throw new TypeError("a sequence cannot be optional in stored output");
20
+ }
21
+ return super.optional();
22
+ }
8
23
  get name() {
9
24
  return "number";
10
25
  }
@@ -27,6 +27,7 @@ export type { default as PartialType } from "#PartialType";
27
27
  export type { default as PureType } from "#PureType";
28
28
  export type { default as RecordType } from "#RecordType";
29
29
  export type { default as Schema } from "#Schema";
30
+ export type { default as Sequence } from "#Sequence";
30
31
  export type { default as Serialized } from "#Serialized";
31
32
  export type { default as Storable } from "#Storable";
32
33
  export type { default as StoreId } from "#StoreId";
@@ -55,7 +56,7 @@ declare namespace schema {
55
56
  export { async_schema as async };
56
57
  export var _b: import("./BigIntType.js").default<"i64", undefined>;
57
58
  export { _b as bigint };
58
- export var _c: import("./BigUintType.js").default<"u64", undefined>;
59
+ export var _c: import("./BigUintType.js").default<"u64", undefined, false>;
59
60
  export { _c as biguint };
60
61
  export var _d: import("#BlobType").default;
61
62
  export { _d as blob };
@@ -121,17 +122,17 @@ declare namespace schema {
121
122
  export { _6 as symbol };
122
123
  export var _7: <const T extends Schema[]>(...items: T) => import("#TupleType").default<{ [K in keyof T]: NormalizeSchema<T[K]>; }>;
123
124
  export { _7 as tuple };
124
- export var _8: import("./BigUintType.js").default<"u128", undefined>;
125
+ export var _8: import("./BigUintType.js").default<"u128", undefined, false>;
125
126
  export { _8 as u128 };
126
- export var _9: import("#UintType").default<"u16", undefined>;
127
+ export var _9: import("#UintType").default<"u16", undefined, false>;
127
128
  export { _9 as u16 };
128
- export var _10: import("#UintType").default<"u32", undefined>;
129
+ export var _10: import("#UintType").default<"u32", undefined, false>;
129
130
  export { _10 as u32 };
130
- export var _11: import("./BigUintType.js").default<"u64", undefined>;
131
+ export var _11: import("./BigUintType.js").default<"u64", undefined, false>;
131
132
  export { _11 as u64 };
132
- export var _12: import("#UintType").default<"u8", undefined>;
133
+ export var _12: import("#UintType").default<"u8", undefined, false>;
133
134
  export { _12 as u8 };
134
- export var _13: import("#UintType").default<"u32", undefined>;
135
+ export var _13: import("#UintType").default<"u32", undefined, false>;
135
136
  export { _13 as uint };
136
137
  export var _14: import("#UndefinedType").default;
137
138
  export { _14 as undefined };
@@ -154,7 +155,7 @@ declare namespace schema {
154
155
  declare const loose: (<const S extends Schema>(s: S) => NormalizeSchema<S>) & {
155
156
  array: <const S extends Schema>(of: S) => import("#ArrayType").default<NormalizeSchema<S>, true>;
156
157
  bigint: import("./BigIntType.js").default<"i64", true>;
157
- biguint: import("./BigUintType.js").default<"u64", true>;
158
+ biguint: import("./BigUintType.js").default<"u64", true, false>;
158
159
  blob: import("#BlobType").default;
159
160
  boolean: import("#BooleanType").default<true>;
160
161
  constructor: <const C extends import("@rcompat/type").AbstractNewable>(c: C) => import("#ConstructorType").default<C>;
@@ -190,12 +191,12 @@ declare const loose: (<const S extends Schema>(s: S) => NormalizeSchema<S>) & {
190
191
  string: import("#StringType").default;
191
192
  symbol: import("./SymbolType.js").default;
192
193
  tuple: <const T extends Schema[]>(...items: T) => import("#TupleType").default<{ [K in keyof T]: NormalizeSchema<T[K]>; }, true>;
193
- u128: import("./BigUintType.js").default<"u128", true>;
194
- u16: import("#UintType").default<"u16", true>;
195
- u32: import("#UintType").default<"u32", true>;
196
- u64: import("./BigUintType.js").default<"u64", true>;
197
- u8: import("#UintType").default<"u8", true>;
198
- uint: import("#UintType").default<"u32", true>;
194
+ u128: import("./BigUintType.js").default<"u128", true, false>;
195
+ u16: import("#UintType").default<"u16", true, false>;
196
+ u32: import("#UintType").default<"u32", true, false>;
197
+ u64: import("./BigUintType.js").default<"u64", true, false>;
198
+ u8: import("#UintType").default<"u8", true, false>;
199
+ uint: import("#UintType").default<"u32", true, false>;
199
200
  undefined: import("#UndefinedType").default;
200
201
  union: {
201
202
  (): import("#UnionType").default<[], true>;
@@ -208,7 +209,7 @@ declare const loose: (<const S extends Schema>(s: S) => NormalizeSchema<S>) & {
208
209
  declare const strict: (<const S extends Schema>(s: S) => NormalizeSchema<S>) & {
209
210
  array: <const S extends Schema>(of: S) => import("#ArrayType").default<NormalizeSchema<S>, false>;
210
211
  bigint: import("./BigIntType.js").default<"i64", false>;
211
- biguint: import("./BigUintType.js").default<"u64", false>;
212
+ biguint: import("./BigUintType.js").default<"u64", false, false>;
212
213
  blob: import("#BlobType").default;
213
214
  boolean: import("#BooleanType").default<false>;
214
215
  constructor: <const C extends import("@rcompat/type").AbstractNewable>(c: C) => import("#ConstructorType").default<C>;
@@ -244,12 +245,12 @@ declare const strict: (<const S extends Schema>(s: S) => NormalizeSchema<S>) & {
244
245
  string: import("#StringType").default;
245
246
  symbol: import("./SymbolType.js").default;
246
247
  tuple: <const T extends Schema[]>(...items: T) => import("#TupleType").default<{ [K in keyof T]: NormalizeSchema<T[K]>; }, false>;
247
- u128: import("./BigUintType.js").default<"u128", false>;
248
- u16: import("#UintType").default<"u16", false>;
249
- u32: import("#UintType").default<"u32", false>;
250
- u64: import("./BigUintType.js").default<"u64", false>;
251
- u8: import("#UintType").default<"u8", false>;
252
- uint: import("#UintType").default<"u32", false>;
248
+ u128: import("./BigUintType.js").default<"u128", false, false>;
249
+ u16: import("#UintType").default<"u16", false, false>;
250
+ u32: import("#UintType").default<"u32", false, false>;
251
+ u64: import("./BigUintType.js").default<"u64", false, false>;
252
+ u8: import("#UintType").default<"u8", false, false>;
253
+ uint: import("#UintType").default<"u32", false, false>;
253
254
  undefined: import("#UndefinedType").default;
254
255
  union: {
255
256
  (): import("#UnionType").default<[], false>;
@@ -1,8 +1,8 @@
1
1
  import BigUintType from "#BigUintType";
2
2
  declare const u128: {
3
- vanilla: BigUintType<"u128", undefined>;
4
- loose: BigUintType<"u128", true>;
5
- strict: BigUintType<"u128", false>;
3
+ vanilla: BigUintType<"u128", undefined, false>;
4
+ loose: BigUintType<"u128", true, false>;
5
+ strict: BigUintType<"u128", false, false>;
6
6
  };
7
7
  export default u128;
8
8
  //# sourceMappingURL=u128.d.ts.map
@@ -1,8 +1,8 @@
1
1
  import UintType from "#UintType";
2
2
  declare const u16: {
3
- vanilla: UintType<"u16", undefined>;
4
- loose: UintType<"u16", true>;
5
- strict: UintType<"u16", false>;
3
+ vanilla: UintType<"u16", undefined, false>;
4
+ loose: UintType<"u16", true, false>;
5
+ strict: UintType<"u16", false, false>;
6
6
  };
7
7
  export default u16;
8
8
  //# sourceMappingURL=u16.d.ts.map
@@ -1,8 +1,8 @@
1
1
  import UintType from "#UintType";
2
2
  declare const u32: {
3
- vanilla: UintType<"u32", undefined>;
4
- loose: UintType<"u32", true>;
5
- strict: UintType<"u32", false>;
3
+ vanilla: UintType<"u32", undefined, false>;
4
+ loose: UintType<"u32", true, false>;
5
+ strict: UintType<"u32", false, false>;
6
6
  };
7
7
  export default u32;
8
8
  //# sourceMappingURL=u32.d.ts.map
@@ -1,8 +1,8 @@
1
1
  import BigUintType from "#BigUintType";
2
2
  declare const u64: {
3
- vanilla: BigUintType<"u64", undefined>;
4
- loose: BigUintType<"u64", true>;
5
- strict: BigUintType<"u64", false>;
3
+ vanilla: BigUintType<"u64", undefined, false>;
4
+ loose: BigUintType<"u64", true, false>;
5
+ strict: BigUintType<"u64", false, false>;
6
6
  };
7
7
  export default u64;
8
8
  //# sourceMappingURL=u64.d.ts.map
@@ -1,8 +1,8 @@
1
1
  import UintType from "#UintType";
2
2
  declare const u8: {
3
- vanilla: UintType<"u8", undefined>;
4
- loose: UintType<"u8", true>;
5
- strict: UintType<"u8", false>;
3
+ vanilla: UintType<"u8", undefined, false>;
4
+ loose: UintType<"u8", true, false>;
5
+ strict: UintType<"u8", false, false>;
6
6
  };
7
7
  export default u8;
8
8
  //# sourceMappingURL=u8.d.ts.map
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "bugs": "https://github.com/primate-run/primate/issues",
3
3
  "dependencies": {
4
4
  "@rcompat/assert": "^0.16.0",
5
- "@rcompat/cli": "^0.28.0",
5
+ "@rcompat/cli": "^0.29.0",
6
6
  "@rcompat/error": "^0.11.0",
7
7
  "@rcompat/is": "^0.14.0"
8
8
  },
@@ -48,5 +48,5 @@
48
48
  "test": "proby"
49
49
  },
50
50
  "type": "module",
51
- "version": "0.12.0"
51
+ "version": "0.13.0"
52
52
  }