libshv-js 3.2.2 → 3.2.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libshv-js",
3
- "version": "3.2.2",
3
+ "version": "3.2.4",
4
4
  "description": "Typescript implementation of libshv",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/src/rpcvalue.ts CHANGED
@@ -6,6 +6,7 @@ class Int<T = number> {
6
6
  this.value = u.value;
7
7
  return;
8
8
  }
9
+
9
10
  if (!Number.isInteger(u)) {
10
11
  throw new TypeError('Value for Int must a positive integral number');
11
12
  }
@@ -18,10 +19,15 @@ class Int<T = number> {
18
19
  }
19
20
  }
20
21
 
21
- class UInt {
22
- readonly value: number;
22
+ class UInt<T extends number = number> {
23
+ readonly value: T;
24
+
25
+ constructor(u: T | UInt<T>) {
26
+ if (u instanceof UInt) {
27
+ this.value = u.value;
28
+ return;
29
+ }
23
30
 
24
- constructor(u: number) {
25
31
  if (u < 0 || !Number.isInteger(u)) {
26
32
  throw new Error(`Invalid value '${u}' for UInt must a positive integral number`);
27
33
  }
package/src/zod.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import {z, type ZodType} from 'zod';
2
- import {type IMap, Int, isIMap, isShvMap, type RpcValue, type ShvMap, UInt} from './rpcvalue';
2
+ import {Int, shvMapType, UInt} from './rpcvalue';
3
3
 
4
- export const map = <T extends Record<string, ZodType<any, any, any>>>(schema: T) => z.custom<ShvMap<{[K in keyof T]: z.infer<T[K]>}>>((data: RpcValue) => isShvMap(data) && z.object(schema).safeParse(data).success);
5
- export const recmap = <T extends ZodType<any, any, any>>(schema: T) => z.custom<ShvMap<Record<string, z.infer<T>>>>((data: RpcValue) => isShvMap(data) && z.record(z.string(), schema).safeParse(data).success);
6
- export const imap = <T extends Record<string, ZodType<any, any, any>>>(schema: T) => z.custom<IMap<{[K in keyof T]: z.infer<T[K]>}>>((data: RpcValue) => isIMap(data) && z.object(schema).safeParse(data).success);
7
- export const int = () => z.custom<Int>((data: RpcValue) => data instanceof Int);
8
- export const uint = () => z.custom<UInt>((data: RpcValue) => data instanceof UInt);
4
+ export const map = <T extends Record<string, ZodType<any, any, any>>>(schema: T) => z.object(schema).and(z.object({[shvMapType]: z.literal('map')}));
5
+ export const recmap = <T extends ZodType<any, any, any>>(schema: T) => z.record(z.string(), schema).and(z.object({[shvMapType]: z.literal('map')}));
6
+ export const imap = <T extends Record<string, ZodType<any, any, any>>>(schema: T) => z.object(schema).and(z.object({[shvMapType]: z.literal('imap')}));
7
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments -- Zod needs the default argument, otherwise it'll infer as Int<unknown>
8
+ export const int = () => z.instanceof(Int<number>);
9
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments -- Zod needs the default argument, otherwise it'll infer as UInt<unknown>
10
+ export const uint = () => z.instanceof(UInt<number>);
9
11
 
10
12
  export * from 'zod';