pema 0.2.1 → 0.4.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.
@@ -0,0 +1,20 @@
1
+ import GenericType from "#GenericType";
2
+ import type Infer from "#Infer";
3
+ import type ObjectType from "#ObjectType";
4
+ import type Parsed from "#Parsed";
5
+ import type ParseOptions from "#ParseOptions";
6
+ import type Serialized from "#Serialized";
7
+ import type Dict from "@rcompat/type/Dict";
8
+ export default class OmitType<P extends Dict<Parsed<unknown>>, K extends keyof P> extends GenericType<Omit<P, K>, Omit<{
9
+ [Key in keyof P]: Infer<P[Key]>;
10
+ }, K>, "OmitType"> {
11
+ #private;
12
+ constructor(type: ObjectType<P>, keys: K[]);
13
+ get name(): "omit";
14
+ parse(x: unknown, options?: ParseOptions): Infer<this>;
15
+ toJSON(): {
16
+ type: "omit";
17
+ properties: Dict<Serialized>;
18
+ };
19
+ }
20
+ //# sourceMappingURL=OmitType.d.ts.map
@@ -0,0 +1,43 @@
1
+ import GenericType from "#GenericType";
2
+ import ParsedKey from "#ParsedKey";
3
+ import join from "#path/join";
4
+ export default class OmitType extends GenericType {
5
+ #properties;
6
+ constructor(type, keys) {
7
+ super();
8
+ const props = { ...type.properties };
9
+ for (const key of keys) {
10
+ delete props[key];
11
+ }
12
+ this.#properties = props;
13
+ }
14
+ get name() {
15
+ return "omit";
16
+ }
17
+ parse(x, options = {}) {
18
+ if (typeof x !== "object" || x === null) {
19
+ throw new Error("Expected object");
20
+ }
21
+ const result = {};
22
+ const props = this.#properties;
23
+ for (const k in props) {
24
+ const field = props[k];
25
+ const r = field.parse(x[k], {
26
+ ...options, [ParsedKey]: join(options[ParsedKey] ?? "", String(k)),
27
+ });
28
+ if (r !== undefined) {
29
+ result[k] = r;
30
+ }
31
+ }
32
+ return result;
33
+ }
34
+ toJSON() {
35
+ const properties = {};
36
+ const props = this.#properties;
37
+ for (const [k, v] of Object.entries(props)) {
38
+ properties[k] = v.toJSON();
39
+ }
40
+ return { type: "omit", properties };
41
+ }
42
+ }
43
+ //# sourceMappingURL=OmitType.js.map
@@ -62,6 +62,9 @@ type StructuralSerialized = {
62
62
  } | {
63
63
  type: "schema";
64
64
  of: Serialized;
65
+ } | {
66
+ type: "omit";
67
+ properties: Dict<Serialized>;
65
68
  } | {
66
69
  type: "pure";
67
70
  } | {
@@ -1,21 +1,9 @@
1
- import GenericType from "#GenericType";
2
- import type Infer from "#Infer";
3
- import type InferStore from "#InferStore";
4
- import type ParseOptions from "#ParseOptions";
1
+ import ObjectType from "#ObjectType";
5
2
  import PartialType from "#PartialType";
6
- import type Serialized from "#Serialized";
7
3
  import type StoreSchema from "#StoreSchema";
8
- import type Dict from "@rcompat/type/Dict";
9
- export default class StoreType<T extends StoreSchema> extends GenericType<T, InferStore<T>, "StoreType"> {
10
- #private;
11
- constructor(spec: T);
4
+ export default class StoreType<S extends StoreSchema> extends ObjectType<S> {
5
+ constructor(spec: S);
12
6
  get name(): string;
13
- get schema(): T;
14
- partial(): PartialType<T>;
15
- parse(x: unknown, options?: ParseOptions): Infer<this>;
16
- toJSON(): {
17
- type: "object";
18
- properties: Dict<Serialized>;
19
- };
7
+ partial(): PartialType<any>;
20
8
  }
21
9
  //# sourceMappingURL=StoreType.d.ts.map
@@ -1,45 +1,14 @@
1
- import GenericType from "#GenericType";
2
- import ParsedKey from "#ParsedKey";
1
+ import ObjectType from "#ObjectType";
3
2
  import PartialType from "#PartialType";
4
- import join from "#path/join";
5
- export default class StoreType extends GenericType {
6
- #properties;
3
+ export default class StoreType extends ObjectType {
7
4
  constructor(spec) {
8
- super();
9
- this.#properties = spec;
5
+ super(spec);
10
6
  }
11
7
  get name() {
12
8
  return "store";
13
9
  }
14
- get schema() {
15
- return this.#properties;
16
- }
17
10
  partial() {
18
- return new PartialType(this.#properties);
19
- }
20
- parse(x, options = {}) {
21
- const spec = this.#properties;
22
- if (typeof x !== "object" || x === null) {
23
- throw new Error("Expected object");
24
- }
25
- const result = {};
26
- for (const k in spec) {
27
- const r = spec[k].parse(x[k], {
28
- ...options, [ParsedKey]: join(options[ParsedKey] ?? "", String(k)),
29
- });
30
- // exclude undefined (optionals)
31
- if (r !== undefined) {
32
- result[k] = r;
33
- }
34
- }
35
- return x;
36
- }
37
- toJSON() {
38
- const properties = {};
39
- for (const [k, v] of Object.entries(this.#properties)) {
40
- properties[k] = v.toJSON();
41
- }
42
- return { type: "object", properties };
11
+ return new PartialType(this.properties);
43
12
  }
44
13
  }
45
14
  //# sourceMappingURL=StoreType.js.map
@@ -3,5 +3,38 @@ import type Schema from "#Schema";
3
3
  /**
4
4
  * Create a schema.
5
5
  */
6
- export default function schema<const S extends Schema>(s: S): NormalizeSchema<S>;
6
+ declare function schema<const S extends Schema>(s: S): NormalizeSchema<S>;
7
+ declare namespace schema {
8
+ var array: typeof import("#array").default;
9
+ var bigint: import("./BigIntType.js").default<"i64">;
10
+ var biguint: import("./BigUintType.js").default<"u64">;
11
+ var blob: import("./BlobType.js").default;
12
+ var boolean: import("./BooleanType.js").default;
13
+ var constructor: <const C extends import("@rcompat/type/AbstractNewable").default>(constructor: C) => import("./ConstructorType.js").default<C>;
14
+ var date: import("./DateType.js").default;
15
+ var f32: import("./NumberType.js").default<"f32">;
16
+ var f64: import("./NumberType.js").default<"f64">;
17
+ var file: import("./FileType.js").default;
18
+ var i128: import("./BigIntType.js").default<"i128">;
19
+ var i16: import("./IntType.js").default<"i16">;
20
+ var i32: import("./IntType.js").default<"i32">;
21
+ var i64: import("./BigIntType.js").default<"i64">;
22
+ var i8: import("./IntType.js").default<"i8">;
23
+ var int: import("./IntType.js").default<"i32">;
24
+ var number: import("./NumberType.js").default<"f64">;
25
+ var omit: typeof import("#omit").default;
26
+ var record: <const Key extends import("./RecordTypeKey.js").default, const Value extends import("./Parsed.js").default<unknown>>(k: Key, v: Value) => import("./RecordType.js").default<Key, Value>;
27
+ var primary: import("./PrimaryType.js").default;
28
+ var string: import("./StringType.js").default;
29
+ var symbol: import("./SymbolType.js").default;
30
+ var u128: import("./BigUintType.js").default<"u128">;
31
+ var u16: import("./UintType.js").default<"u16">;
32
+ var u32: import("./UintType.js").default<"u32">;
33
+ var u64: import("./BigUintType.js").default<"u64">;
34
+ var u8: import("./UintType.js").default<"u8">;
35
+ var uint: import("./UintType.js").default<"u32">;
36
+ var union: typeof import("#union").default;
37
+ var unknown: import("./UnknownType.js").default;
38
+ }
39
+ export default schema;
7
40
  //# sourceMappingURL=index.d.ts.map
@@ -1,8 +1,69 @@
1
+ import array from "#array";
2
+ import bigint from "#bigint";
3
+ import biguint from "#biguint";
4
+ import blob from "#blob";
5
+ import boolean from "#boolean";
6
+ import constructor from "#constructor";
7
+ import date from "#date";
8
+ import f32 from "#f32";
9
+ import f64 from "#f64";
10
+ import file from "#file";
11
+ import i128 from "#i128";
12
+ import i16 from "#i16";
13
+ import i32 from "#i32";
14
+ import i64 from "#i64";
15
+ import i8 from "#i8";
16
+ import int from "#int";
1
17
  import normalize from "#normalize";
18
+ import number from "#number";
19
+ import omit from "#omit";
20
+ import primary from "#primary";
21
+ import record from "#record";
22
+ import string from "#string";
23
+ import symbol from "#symbol";
24
+ import u128 from "#u128";
25
+ import u16 from "#u16";
26
+ import u32 from "#u32";
27
+ import u64 from "#u64";
28
+ import u8 from "#u8";
29
+ import uint from "#uint";
30
+ import union from "#union";
31
+ import unknown from "#unknown";
2
32
  /**
3
33
  * Create a schema.
4
34
  */
5
- export default function schema(s) {
35
+ function schema(s) {
6
36
  return normalize(s);
7
37
  }
38
+ schema.array = array;
39
+ schema.bigint = bigint;
40
+ schema.biguint = biguint;
41
+ schema.blob = blob;
42
+ schema.boolean = boolean;
43
+ schema.constructor = constructor;
44
+ schema.date = date;
45
+ schema.f32 = f32;
46
+ schema.f64 = f64;
47
+ schema.file = file;
48
+ schema.i128 = i128;
49
+ schema.i16 = i16;
50
+ schema.i32 = i32;
51
+ schema.i64 = i64;
52
+ schema.i8 = i8;
53
+ schema.int = int;
54
+ schema.number = number;
55
+ schema.omit = omit;
56
+ schema.record = record;
57
+ schema.primary = primary;
58
+ schema.string = string;
59
+ schema.symbol = symbol;
60
+ schema.u128 = u128;
61
+ schema.u16 = u16;
62
+ schema.u32 = u32;
63
+ schema.u64 = u64;
64
+ schema.u8 = u8;
65
+ schema.uint = uint;
66
+ schema.union = union;
67
+ schema.unknown = unknown;
68
+ export default schema;
8
69
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ import type ObjectType from "#ObjectType";
2
+ import OmitType from "#OmitType";
3
+ import type Parsed from "#Parsed";
4
+ import type Dict from "@rcompat/type/Dict";
5
+ export default function omit<P extends Dict<Parsed<unknown>>, K extends keyof P>(type: ObjectType<P>, ...keys: K[]): OmitType<P, K>;
6
+ //# sourceMappingURL=omit.d.ts.map
@@ -0,0 +1,5 @@
1
+ import OmitType from "#OmitType";
2
+ export default function omit(type, ...keys) {
3
+ return new OmitType(type, keys);
4
+ }
5
+ //# sourceMappingURL=omit.js.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#OmitType";
2
+ //# sourceMappingURL=OmitType.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#OmitType";
2
+ //# sourceMappingURL=OmitType.js.map
@@ -1,3 +1,4 @@
1
+ export { default as ConstructorType } from "#ConstructorType";
1
2
  export { default as DefaultType } from "#DefaultType";
2
3
  export { default } from "#index";
3
4
  export { default as LiteralType } from "#LiteralType";
@@ -5,4 +6,6 @@ export { default as ObjectType } from "#ObjectType";
5
6
  export { default as OptionalType } from "#OptionalType";
6
7
  export { default as PureType } from "#PureType";
7
8
  export { default as TupleType } from "#TupleType";
9
+ export { default as UndefinedType } from "#UndefinedType";
10
+ export { default as ArrayType } from "#ArrayType";
8
11
  //# sourceMappingURL=index.d.ts.map
@@ -1,3 +1,4 @@
1
+ export { default as ConstructorType } from "#ConstructorType";
1
2
  export { default as DefaultType } from "#DefaultType";
2
3
  export { default } from "#index";
3
4
  export { default as LiteralType } from "#LiteralType";
@@ -5,4 +6,6 @@ export { default as ObjectType } from "#ObjectType";
5
6
  export { default as OptionalType } from "#OptionalType";
6
7
  export { default as PureType } from "#PureType";
7
8
  export { default as TupleType } from "#TupleType";
9
+ export { default as UndefinedType } from "#UndefinedType";
10
+ export { default as ArrayType } from "#ArrayType";
8
11
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pema",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Primate schema validation",
5
5
  "homepage": "https://primate.run/docs/validation",
6
6
  "bugs": "https://github.com/primate-run/primate/issues",