@etsoo/smarterp-core 1.0.86 → 1.0.87

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,39 @@
1
+ import { CoreUtils } from "../src";
2
+
3
+ test("Test for validateJson with string schema", async () => {
4
+ const schema =
5
+ '{"type":"object","properties":{"cc":{"type":"array","format":"email","uniqueItems":true},"bcc":{"type":"array","format":"email","uniqueItems":true}}}';
6
+
7
+ let [valid, errors] = await CoreUtils.validateJson(schema, "{}");
8
+ expect(valid).toBeTruthy();
9
+
10
+ [valid, errors] = await CoreUtils.validateJson(schema, {
11
+ cc: ["a@b.com", "a@b.com"]
12
+ });
13
+ expect(valid).toBeFalsy();
14
+ expect(errors?.[0].keyword).toBe("uniqueItems");
15
+ });
16
+
17
+ test("Test for validateJson with object schema", async () => {
18
+ const schema = {
19
+ type: "object",
20
+ properties: {
21
+ cc: {
22
+ type: "array",
23
+ format: "email",
24
+ uniqueItems: true
25
+ },
26
+ bcc: {
27
+ type: "array",
28
+ format: "email",
29
+ uniqueItems: true
30
+ }
31
+ }
32
+ };
33
+
34
+ let [valid, errors] = await CoreUtils.validateJson(schema, {
35
+ cc: "abc"
36
+ });
37
+ expect(valid).toBeFalsy();
38
+ expect(errors?.[0].keyword).toBe("format");
39
+ });
@@ -1,4 +1,4 @@
1
- import { ErrorObject } from "ajv";
1
+ import type { ErrorObject } from "ajv";
2
2
  /**
3
3
  * Core utilities
4
4
  */
@@ -17,7 +17,7 @@ export declare namespace CoreUtils {
17
17
  * @param input JSON input to validate
18
18
  * @returns Result
19
19
  */
20
- function validateJson(schema: any, input: any): Promise<[
20
+ function validateJson(schema: string | object, input: string | object | null | undefined): Promise<[
21
21
  boolean,
22
22
  ErrorObject<string, Record<string, any>, unknown>[] | null | undefined
23
23
  ]>;
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.CoreUtils = void 0;
7
- const ajv_1 = __importDefault(require("ajv"));
7
+ const ajv_formats_1 = __importDefault(require("ajv-formats"));
8
8
  /**
9
9
  * Core utilities
10
10
  */
@@ -24,6 +24,7 @@ var CoreUtils;
24
24
  };
25
25
  }
26
26
  CoreUtils.avatarStyles = avatarStyles;
27
+ let ajv = null;
27
28
  /**
28
29
  * Validate JSON input against a schema
29
30
  * 验证 JSON 输入是否符合架构
@@ -32,9 +33,18 @@ var CoreUtils;
32
33
  * @returns Result
33
34
  */
34
35
  async function validateJson(schema, input) {
35
- const ajv = new ajv_1.default();
36
- const v = await ajv.compileAsync(schema);
37
- return [v(input), v.errors];
36
+ if (ajv == null) {
37
+ const AjvClass = (await import("ajv")).Ajv;
38
+ ajv = new AjvClass({
39
+ allErrors: true,
40
+ strictTypes: false
41
+ });
42
+ (0, ajv_formats_1.default)(ajv);
43
+ }
44
+ return [
45
+ ajv.validate(typeof schema === "string" ? JSON.parse(schema) : schema, typeof input === "string" ? JSON.parse(input) : input),
46
+ ajv.errors
47
+ ];
38
48
  }
39
49
  CoreUtils.validateJson = validateJson;
40
50
  })(CoreUtils || (exports.CoreUtils = CoreUtils = {}));
@@ -141,7 +141,7 @@ export declare class OrgApi extends EntityApi {
141
141
  * @param payload Payload
142
142
  * @returns Result
143
143
  */
144
- readApiSchema(service: CoreApiService, payload?: IApiPayload<string>): Promise<string | undefined>;
144
+ readApiSchema(service: CoreApiService, payload?: IApiPayload<object>): Promise<object | undefined>;
145
145
  /**
146
146
  * Get Antiforgery request token
147
147
  * 获取反伪造请求令牌
@@ -1,4 +1,4 @@
1
- import { ErrorObject } from "ajv";
1
+ import type { ErrorObject } from "ajv";
2
2
  /**
3
3
  * Core utilities
4
4
  */
@@ -17,7 +17,7 @@ export declare namespace CoreUtils {
17
17
  * @param input JSON input to validate
18
18
  * @returns Result
19
19
  */
20
- function validateJson(schema: any, input: any): Promise<[
20
+ function validateJson(schema: string | object, input: string | object | null | undefined): Promise<[
21
21
  boolean,
22
22
  ErrorObject<string, Record<string, any>, unknown>[] | null | undefined
23
23
  ]>;
@@ -1,4 +1,4 @@
1
- import Ajv from "ajv";
1
+ import addFormats from "ajv-formats";
2
2
  /**
3
3
  * Core utilities
4
4
  */
@@ -18,6 +18,7 @@ export var CoreUtils;
18
18
  };
19
19
  }
20
20
  CoreUtils.avatarStyles = avatarStyles;
21
+ let ajv = null;
21
22
  /**
22
23
  * Validate JSON input against a schema
23
24
  * 验证 JSON 输入是否符合架构
@@ -26,9 +27,18 @@ export var CoreUtils;
26
27
  * @returns Result
27
28
  */
28
29
  async function validateJson(schema, input) {
29
- const ajv = new Ajv();
30
- const v = await ajv.compileAsync(schema);
31
- return [v(input), v.errors];
30
+ if (ajv == null) {
31
+ const AjvClass = (await import("ajv")).Ajv;
32
+ ajv = new AjvClass({
33
+ allErrors: true,
34
+ strictTypes: false
35
+ });
36
+ addFormats(ajv);
37
+ }
38
+ return [
39
+ ajv.validate(typeof schema === "string" ? JSON.parse(schema) : schema, typeof input === "string" ? JSON.parse(input) : input),
40
+ ajv.errors
41
+ ];
32
42
  }
33
43
  CoreUtils.validateJson = validateJson;
34
44
  })(CoreUtils || (CoreUtils = {}));
@@ -141,7 +141,7 @@ export declare class OrgApi extends EntityApi {
141
141
  * @param payload Payload
142
142
  * @returns Result
143
143
  */
144
- readApiSchema(service: CoreApiService, payload?: IApiPayload<string>): Promise<string | undefined>;
144
+ readApiSchema(service: CoreApiService, payload?: IApiPayload<object>): Promise<object | undefined>;
145
145
  /**
146
146
  * Get Antiforgery request token
147
147
  * 获取反伪造请求令牌
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/smarterp-core",
3
- "version": "1.0.86",
3
+ "version": "1.0.87",
4
4
  "description": "TypeScript APIs for SmartERP Core",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -60,6 +60,7 @@
60
60
  "@etsoo/toolpad": "^1.0.40",
61
61
  "@mui/material": "^7.2.0",
62
62
  "ajv": "^8.17.1",
63
+ "ajv-formats": "^3.0.1",
63
64
  "react": "^18.3.1",
64
65
  "react-dom": "^18.3.1"
65
66
  }
package/src/CoreUtils.ts CHANGED
@@ -1,4 +1,6 @@
1
- import Ajv, { ErrorObject } from "ajv";
1
+ import type Ajv from "ajv";
2
+ import type { ErrorObject } from "ajv";
3
+ import addFormats from "ajv-formats";
2
4
 
3
5
  /**
4
6
  * Core utilities
@@ -18,6 +20,8 @@ export namespace CoreUtils {
18
20
  };
19
21
  }
20
22
 
23
+ let ajv: Ajv | null = null;
24
+
21
25
  /**
22
26
  * Validate JSON input against a schema
23
27
  * 验证 JSON 输入是否符合架构
@@ -26,16 +30,31 @@ export namespace CoreUtils {
26
30
  * @returns Result
27
31
  */
28
32
  export async function validateJson(
29
- schema: any,
30
- input: any
33
+ schema: string | object,
34
+ input: string | object | null | undefined
31
35
  ): Promise<
32
36
  [
33
37
  boolean,
34
38
  ErrorObject<string, Record<string, any>, unknown>[] | null | undefined
35
39
  ]
36
40
  > {
37
- const ajv = new Ajv();
38
- const v = await ajv.compileAsync(schema);
39
- return [v(input), v.errors];
41
+ if (ajv == null) {
42
+ const AjvClass = (await import("ajv")).Ajv;
43
+
44
+ ajv = new AjvClass({
45
+ allErrors: true,
46
+ strictTypes: false
47
+ });
48
+
49
+ addFormats(ajv);
50
+ }
51
+
52
+ return [
53
+ ajv!.validate(
54
+ typeof schema === "string" ? JSON.parse(schema) : schema,
55
+ typeof input === "string" ? JSON.parse(input) : input
56
+ ),
57
+ ajv!.errors
58
+ ];
40
59
  }
41
60
  }
package/src/OrgApi.ts CHANGED
@@ -221,7 +221,7 @@ export class OrgApi extends EntityApi {
221
221
  * @param payload Payload
222
222
  * @returns Result
223
223
  */
224
- readApiSchema(service: CoreApiService, payload?: IApiPayload<string>) {
224
+ readApiSchema(service: CoreApiService, payload?: IApiPayload<object>) {
225
225
  return this.api.get(
226
226
  `${this.flag}/ReadApiSchema/${service}`,
227
227
  undefined,