@jskit-ai/resource-core 0.1.187 → 0.1.188

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": "@jskit-ai/resource-core",
3
- "version": "0.1.187",
3
+ "version": "0.1.188",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1"
@@ -27,6 +27,6 @@
27
27
  }
28
28
  },
29
29
  "peerDependencies": {
30
- "@jskit-ai/kernel": "0.1.245"
30
+ "@jskit-ai/kernel": "0.1.246"
31
31
  }
32
32
  }
@@ -1,7 +1,10 @@
1
1
  import { deepFreeze } from "@jskit-ai/kernel/shared/support/deepFreeze";
2
2
  import { normalizeObject, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
3
+ import {
4
+ isJsonRestSchemaInstance,
5
+ normalizeSingleSchemaDefinition
6
+ } from "@jskit-ai/kernel/shared/validators";
3
7
 
4
- const SCHEMA_DEFINITION_MODES = new Set(["create", "replace", "patch"]);
5
8
  const OPERATION_SCHEMA_SECTION_NAMES = Object.freeze([
6
9
  "body",
7
10
  "query",
@@ -10,53 +13,10 @@ const OPERATION_SCHEMA_SECTION_NAMES = Object.freeze([
10
13
  "output"
11
14
  ]);
12
15
 
13
- function isJsonRestSchemaInstance(value) {
14
- return Boolean(value) &&
15
- typeof value === "object" &&
16
- typeof value.create === "function" &&
17
- typeof value.replace === "function" &&
18
- typeof value.patch === "function" &&
19
- typeof value.toJsonSchema === "function";
20
- }
21
-
22
- function isSchemaDefinitionLike(value) {
23
- return Boolean(value) &&
24
- typeof value === "object" &&
25
- !Array.isArray(value) &&
26
- (
27
- Object.hasOwn(value, "schema") ||
28
- Object.hasOwn(value, "mode")
29
- );
30
- }
31
-
32
- function resolveSchemaDefinitionMode(mode, {
33
- context = "schema definition.mode",
34
- defaultMode = "patch"
35
- } = {}) {
36
- const fallbackMode = normalizeText(defaultMode).toLowerCase() || "patch";
37
- const normalizedMode = normalizeText(mode).toLowerCase() || fallbackMode;
38
-
39
- if (!SCHEMA_DEFINITION_MODES.has(normalizedMode)) {
40
- throw new TypeError(`${context} must be one of: create, replace, patch.`);
41
- }
42
-
43
- return normalizedMode;
44
- }
45
-
46
16
  function createSchemaDefinition(schema, mode = "patch", {
47
17
  context = "schema definition"
48
18
  } = {}) {
49
- if (!isJsonRestSchemaInstance(schema)) {
50
- throw new TypeError(`${context}.schema must be a json-rest-schema schema instance.`);
51
- }
52
-
53
- return Object.freeze({
54
- schema,
55
- mode: resolveSchemaDefinitionMode(mode, {
56
- context: `${context}.mode`,
57
- defaultMode: "patch"
58
- })
59
- });
19
+ return normalizeSingleSchemaDefinition({ schema, mode }, { context, defaultMode: "patch" });
60
20
  }
61
21
 
62
22
  function normalizeSchemaDefinitionLike(value, {
@@ -67,27 +27,8 @@ function normalizeSchemaDefinitionLike(value, {
67
27
  return value;
68
28
  }
69
29
 
70
- if (isJsonRestSchemaInstance(value)) {
71
- return createSchemaDefinition(value, defaultMode, { context });
72
- }
73
-
74
- if (!isSchemaDefinitionLike(value)) {
75
- throw new TypeError(
76
- `${context} must be a json-rest-schema schema instance or schema definition object.`
77
- );
78
- }
79
-
80
- const source = normalizeObject(value);
81
-
82
- if (!Object.hasOwn(source, "schema")) {
83
- throw new TypeError(`${context}.schema is required.`);
84
- }
85
-
86
- return createSchemaDefinition(
87
- source.schema,
88
- Object.hasOwn(source, "mode") ? source.mode : defaultMode,
89
- { context }
90
- );
30
+ const definition = isJsonRestSchemaInstance(value) ? { schema: value, mode: defaultMode } : value;
31
+ return normalizeSingleSchemaDefinition(definition, { context, defaultMode });
91
32
  }
92
33
 
93
34
  function resolveDefaultOperationSchemaMode(sectionName, operation = {}) {
@@ -1,6 +1,6 @@
1
1
  import assert from "node:assert/strict";
2
2
  import test from "node:test";
3
- import { createSchema } from "@jskit-ai/kernel/shared/validators";
3
+ import { createSchema, normalizeSingleSchemaDefinition } from "@jskit-ai/kernel/shared/validators";
4
4
  import {
5
5
  createSchemaDefinition,
6
6
  defineResource,
@@ -91,7 +91,7 @@ test("defineResource rejects invalid operation schema section values eagerly", (
91
91
  output: true
92
92
  }
93
93
  }
94
- }), /operations\.view\.output must be a json-rest-schema schema instance or schema definition object/);
94
+ }), /operations\.view\.output must be a schema definition object/);
95
95
 
96
96
  assert.throws(() => defineResource({
97
97
  namespace: "assistantConfig",
@@ -101,5 +101,28 @@ test("defineResource rejects invalid operation schema section values eagerly", (
101
101
  body: "invalid"
102
102
  }
103
103
  }
104
- }), /operations\.patch\.body must be a json-rest-schema schema instance or schema definition object/);
104
+ }), /operations\.patch\.body must be a schema definition object/);
105
+ });
106
+
107
+ test("resource schema definitions follow the kernel's definition rules", () => {
108
+ const schema = createSchema({ ok: { type: "boolean", required: true } });
109
+
110
+ assert.deepEqual(
111
+ normalizeSchemaDefinitionLike({ schema, mode: "replace" }, { context: "output" }),
112
+ normalizeSingleSchemaDefinition({ schema, mode: "replace" }, { context: "output" })
113
+ );
114
+ assert.deepEqual(normalizeSchemaDefinitionLike(schema, { defaultMode: "create" }), { schema, mode: "create" });
115
+ assert.equal(normalizeSchemaDefinitionLike(null), null);
116
+
117
+ const kernelModeError = (() => {
118
+ try {
119
+ normalizeSingleSchemaDefinition({ schema, mode: "upsert" }, { context: "body" });
120
+ } catch (error) {
121
+ return error.message;
122
+ }
123
+ return "";
124
+ })();
125
+ assert.ok(kernelModeError);
126
+ assert.throws(() => createSchemaDefinition(schema, "upsert", { context: "body" }), { message: kernelModeError });
127
+ assert.throws(() => normalizeSchemaDefinitionLike({ schema, mode: "upsert" }, { context: "body" }), { message: kernelModeError });
105
128
  });