@jskit-ai/crud-core 0.1.259 → 0.2.1
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/CHANGELOG.md +43 -0
- package/locales/en.json +25 -0
- package/package.json +8 -7
- package/patterns/json-api-resource-package/PATTERN.md +7 -0
- package/patterns/json-api-resource-package/example/packages/books/package.json +2 -2
- package/src/server/defineCrudJsonApiFeature.js +4 -2
- package/src/server/fieldAccess.js +5 -1
- package/src/server/jsonApiModule/actions.js +3 -2
- package/src/server/jsonApiModule/service.js +1 -1
- package/src/server/listQueryValidators.js +27 -6
- package/src/server/resourceRuntime/index.js +4 -3
- package/src/server/serviceMethods.js +4 -4
- package/test/defineCrudJsonApiFeature.test.js +39 -0
- package/test/errorCodes.test.js +117 -0
- package/test/i18nCatalog.test.js +16 -0
- package/test/resourceRuntime.test.js +3 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.0 - 2026-10-29
|
|
4
|
+
|
|
5
|
+
- Translation ready: every user-visible text is a message key with its English in `locales/en.json`, so an app translates it by supplying translation files only. With no translations, texts and behaviour are unchanged. See the guide "Translating a JSKIT app".
|
|
6
|
+
|
|
7
|
+
### Upgrading
|
|
8
|
+
|
|
9
|
+
- A missing document or record now answers `crud.document.notFound` / `crud.record.notFound` instead of `APP_ERROR` (same English and status).
|
|
10
|
+
|
|
11
|
+
### New message keys (9)
|
|
12
|
+
|
|
13
|
+
All keys in `locales/en.json` (exported as `./locales/en.json`) are new in this version.
|
|
14
|
+
|
|
15
|
+
<details><summary>Key list</summary>
|
|
16
|
+
|
|
17
|
+
- `jskit.errors.INVALID_CURSOR`
|
|
18
|
+
- `jskit.crud.record.notFound`
|
|
19
|
+
- `jskit.crud.document.notFound`
|
|
20
|
+
- `jskit.crud.fieldAccess.writeDenied`
|
|
21
|
+
- `jskit.crud.query.includeInvalid`
|
|
22
|
+
- `jskit.crud.query.fieldsInvalid`
|
|
23
|
+
- `jskit.crud.query.fieldsByTypeInvalid`
|
|
24
|
+
- `jskit.crud.query.fieldsKeysInvalid`
|
|
25
|
+
- `jskit.crud.query.fieldsKeysInvalidWithAliases`
|
|
26
|
+
|
|
27
|
+
</details>
|
|
28
|
+
|
|
29
|
+
### Codes (9)
|
|
30
|
+
|
|
31
|
+
Error, field-error and message codes this package sends, with their English. Undotted codes kept their existing values and render from `jskit.errors.<code>`.
|
|
32
|
+
|
|
33
|
+
| Code | English |
|
|
34
|
+
| --- | --- |
|
|
35
|
+
| `INVALID_CURSOR` | Invalid cursor. |
|
|
36
|
+
| `crud.record.notFound` | Record not found. |
|
|
37
|
+
| `crud.document.notFound` | Document not found. |
|
|
38
|
+
| `crud.fieldAccess.writeDenied` | Write access denied for fields: {fields}. |
|
|
39
|
+
| `crud.query.includeInvalid` | include expects a comma-separated string such as "{example}". |
|
|
40
|
+
| `crud.query.fieldsInvalid` | fields expects an object such as {example}. |
|
|
41
|
+
| `crud.query.fieldsByTypeInvalid` | fields expects an object keyed by JSON:API resource type, such as {example}. |
|
|
42
|
+
| `crud.query.fieldsKeysInvalid` | fields keys must be JSON:API resource types. Allowed keys: {allowedTypes}. |
|
|
43
|
+
| `crud.query.fieldsKeysInvalidWithAliases` | fields keys must be JSON:API resource types. Allowed keys: {allowedTypes}. Relationship aliases are invalid keys; {aliasGuidance}. |
|
package/locales/en.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"jskit": {
|
|
3
|
+
"errors": {
|
|
4
|
+
"INVALID_CURSOR": "Invalid cursor."
|
|
5
|
+
},
|
|
6
|
+
"crud": {
|
|
7
|
+
"record": {
|
|
8
|
+
"notFound": "Record not found."
|
|
9
|
+
},
|
|
10
|
+
"document": {
|
|
11
|
+
"notFound": "Document not found."
|
|
12
|
+
},
|
|
13
|
+
"fieldAccess": {
|
|
14
|
+
"writeDenied": "Write access denied for fields: {fields}."
|
|
15
|
+
},
|
|
16
|
+
"query": {
|
|
17
|
+
"includeInvalid": "include expects a comma-separated string such as \"{example}\".",
|
|
18
|
+
"fieldsInvalid": "fields expects an object such as {example}.",
|
|
19
|
+
"fieldsByTypeInvalid": "fields expects an object keyed by JSON:API resource type, such as {example}.",
|
|
20
|
+
"fieldsKeysInvalid": "fields keys must be JSON:API resource types. Allowed keys: {allowedTypes}.",
|
|
21
|
+
"fieldsKeysInvalidWithAliases": "fields keys must be JSON:API resource types. Allowed keys: {allowedTypes}. Relationship aliases are invalid keys; {aliasGuidance}."
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/crud-core",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24.0.0"
|
|
@@ -23,12 +23,13 @@
|
|
|
23
23
|
"./server/crudModuleConfig": "./src/server/crudModuleConfig.js",
|
|
24
24
|
"./server/listQueryValidators": "./src/server/listQueryValidators.js",
|
|
25
25
|
"./server/listFilters": "./src/server/listFilters.js",
|
|
26
|
-
"./server/routeContracts": "./src/server/routeContracts.js"
|
|
26
|
+
"./server/routeContracts": "./src/server/routeContracts.js",
|
|
27
|
+
"./locales/en.json": "./locales/en.json"
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
30
|
-
"@jskit-ai/json-rest-api-core": "0.1
|
|
31
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
30
|
+
"@jskit-ai/database-runtime": "0.1.250",
|
|
31
|
+
"@jskit-ai/json-rest-api-core": "0.2.1",
|
|
32
|
+
"@jskit-ai/resource-crud-core": "0.1.192",
|
|
32
33
|
"json-rest-schema": "^1.0.17",
|
|
33
34
|
"json-rest-api": "2.1.0"
|
|
34
35
|
},
|
|
@@ -49,7 +50,7 @@
|
|
|
49
50
|
}
|
|
50
51
|
},
|
|
51
52
|
"peerDependencies": {
|
|
52
|
-
"@jskit-ai/http-runtime": "0.1
|
|
53
|
-
"@jskit-ai/kernel": "0.1
|
|
53
|
+
"@jskit-ai/http-runtime": "0.2.1",
|
|
54
|
+
"@jskit-ai/kernel": "0.2.1"
|
|
54
55
|
}
|
|
55
56
|
}
|
|
@@ -96,6 +96,13 @@ surface further; do not register unused mutation actions and routes. Use
|
|
|
96
96
|
`routes: false` only for an internal
|
|
97
97
|
capability/action surface that deliberately has no HTTP API.
|
|
98
98
|
|
|
99
|
+
Create, update, and delete publish `<namespace>.record.changed` to the
|
|
100
|
+
`event_scope` realtime audience, which reaches only the room of the action's
|
|
101
|
+
user, workspace, or workspace-user scope. Set `realtimeAudience` to another
|
|
102
|
+
realtime audience when the product needs one, for example `"all_users"` so
|
|
103
|
+
every signed-in user's view of a public resource refreshes. It also accepts a
|
|
104
|
+
function of the action execution.
|
|
105
|
+
|
|
99
106
|
When otherwise-standard CRUD earns product behavior, name the exact
|
|
100
107
|
capabilities in `requires` and provide `decorateService({ service, repository,
|
|
101
108
|
resource, ...dependencies })`. Retain the standard methods, override only the
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"./shared": "./src/shared/index.js"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@jskit-ai/crud-core": "0.1
|
|
12
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
11
|
+
"@jskit-ai/crud-core": "0.2.1",
|
|
12
|
+
"@jskit-ai/resource-crud-core": "0.1.192"
|
|
13
13
|
},
|
|
14
14
|
"jskit": {
|
|
15
15
|
"kind": "runtime",
|
|
@@ -163,7 +163,8 @@ function defineCrudJsonApiFeature({
|
|
|
163
163
|
operationLifecycle = {},
|
|
164
164
|
operationInputs = {},
|
|
165
165
|
actions = {},
|
|
166
|
-
operations = undefined
|
|
166
|
+
operations = undefined,
|
|
167
|
+
realtimeAudience = "event_scope"
|
|
167
168
|
} = {}) {
|
|
168
169
|
if (!resource || typeof resource !== "object" || Array.isArray(resource)) {
|
|
169
170
|
throw new TypeError("defineCrudJsonApiFeature requires resource.");
|
|
@@ -301,7 +302,8 @@ function defineCrudJsonApiFeature({
|
|
|
301
302
|
permissions: permissionOptions
|
|
302
303
|
}),
|
|
303
304
|
scopeInputValidator: normalizedScope.actionInputValidator,
|
|
304
|
-
scopeInputKeys: normalizedScope.inputKeys
|
|
305
|
+
scopeInputKeys: normalizedScope.inputKeys,
|
|
306
|
+
realtimeAudience
|
|
305
307
|
});
|
|
306
308
|
}
|
|
307
309
|
});
|
|
@@ -235,7 +235,11 @@ function createCrudFieldAccessRuntime(resource = {}, { context = "crudFieldAcces
|
|
|
235
235
|
}
|
|
236
236
|
|
|
237
237
|
if (forbiddenFields.length > 0 && writeMode === "throw") {
|
|
238
|
-
|
|
238
|
+
const fields = forbiddenFields.join(", ");
|
|
239
|
+
throw new AppError(403, `Write access denied for fields: ${fields}.`, {
|
|
240
|
+
code: "crud.fieldAccess.writeDenied",
|
|
241
|
+
params: { fields }
|
|
242
|
+
});
|
|
239
243
|
}
|
|
240
244
|
|
|
241
245
|
return filteredPayload;
|
|
@@ -558,7 +558,8 @@ function createCrudJsonApiActions({
|
|
|
558
558
|
operationLifecycle = {},
|
|
559
559
|
operationInputs = {},
|
|
560
560
|
actions: additionalActionDefinitions = {},
|
|
561
|
-
dependencies = {}
|
|
561
|
+
dependencies = {},
|
|
562
|
+
realtimeAudience = "event_scope"
|
|
562
563
|
} = {}) {
|
|
563
564
|
const actionId = (operation) => `crud.${namespace}.${operation}`;
|
|
564
565
|
if (!service || typeof service !== "object") {
|
|
@@ -580,7 +581,7 @@ function createCrudJsonApiActions({
|
|
|
580
581
|
entityId,
|
|
581
582
|
realtime: {
|
|
582
583
|
event: recordChangedEvent,
|
|
583
|
-
audience:
|
|
584
|
+
audience: realtimeAudience
|
|
584
585
|
}
|
|
585
586
|
});
|
|
586
587
|
const lifecycle = normalizeCrudOperationLifecycle(operationLifecycle);
|
|
@@ -3,7 +3,7 @@ import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
|
3
3
|
|
|
4
4
|
function requireDocument(document = null) {
|
|
5
5
|
if (!document) {
|
|
6
|
-
throw new AppError(404, "Document not found.");
|
|
6
|
+
throw new AppError(404, "Document not found.", { code: "crud.document.notFound" });
|
|
7
7
|
}
|
|
8
8
|
return document;
|
|
9
9
|
}
|
|
@@ -22,7 +22,11 @@ const lookupIncludeQueryValidator = Object.freeze({
|
|
|
22
22
|
type: "string",
|
|
23
23
|
required: false,
|
|
24
24
|
messages: {
|
|
25
|
-
default:
|
|
25
|
+
default: {
|
|
26
|
+
code: "crud.query.includeInvalid",
|
|
27
|
+
message: "include expects a comma-separated string such as \"pet,service\".",
|
|
28
|
+
params: { example: "pet,service" }
|
|
29
|
+
}
|
|
26
30
|
}
|
|
27
31
|
}
|
|
28
32
|
}),
|
|
@@ -57,7 +61,11 @@ const jsonApiFieldsetsQueryValidator = Object.freeze({
|
|
|
57
61
|
type: "object",
|
|
58
62
|
required: false,
|
|
59
63
|
messages: {
|
|
60
|
-
default:
|
|
64
|
+
default: {
|
|
65
|
+
code: "crud.query.fieldsInvalid",
|
|
66
|
+
message: "fields expects an object such as {\"bookings\":[\"petId\"],\"pets\":[\"name\"]}.",
|
|
67
|
+
params: { example: "{\"bookings\":[\"petId\"],\"pets\":[\"name\"]}" }
|
|
68
|
+
}
|
|
61
69
|
},
|
|
62
70
|
values: jsonApiFieldsetValueDefinition
|
|
63
71
|
}
|
|
@@ -80,9 +88,18 @@ function createJsonApiFieldsetsQueryValidator({ resource = {} } = {}) {
|
|
|
80
88
|
const aliasGuidance = contract.aliasMappings
|
|
81
89
|
.map((entry) => `use "${entry.resourceType}" instead of "${entry.alias}"`)
|
|
82
90
|
.join("; ");
|
|
83
|
-
const additionalPropertiesMessage =
|
|
84
|
-
|
|
85
|
-
|
|
91
|
+
const additionalPropertiesMessage = aliasGuidance
|
|
92
|
+
? {
|
|
93
|
+
code: "crud.query.fieldsKeysInvalidWithAliases",
|
|
94
|
+
message: `fields keys must be JSON:API resource types. Allowed keys: ${allowedTypes}.` +
|
|
95
|
+
` Relationship aliases are invalid keys; ${aliasGuidance}.`,
|
|
96
|
+
params: { allowedTypes, aliasGuidance }
|
|
97
|
+
}
|
|
98
|
+
: {
|
|
99
|
+
code: "crud.query.fieldsKeysInvalid",
|
|
100
|
+
message: `fields keys must be JSON:API resource types. Allowed keys: ${allowedTypes}.`,
|
|
101
|
+
params: { allowedTypes }
|
|
102
|
+
};
|
|
86
103
|
const fieldsetSchema = createSchema(
|
|
87
104
|
Object.fromEntries(
|
|
88
105
|
contract.resourceTypes.map((resourceType) => [
|
|
@@ -101,7 +118,11 @@ function createJsonApiFieldsetsQueryValidator({ resource = {} } = {}) {
|
|
|
101
118
|
required: false,
|
|
102
119
|
schema: fieldsetSchema,
|
|
103
120
|
messages: {
|
|
104
|
-
default:
|
|
121
|
+
default: {
|
|
122
|
+
code: "crud.query.fieldsByTypeInvalid",
|
|
123
|
+
message: `fields expects an object keyed by JSON:API resource type, such as ${example}.`,
|
|
124
|
+
params: { example }
|
|
125
|
+
},
|
|
105
126
|
additionalProperties: additionalPropertiesMessage
|
|
106
127
|
}
|
|
107
128
|
}
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
resolveCrudIdColumn
|
|
21
21
|
} from "../repositorySupport.js";
|
|
22
22
|
import {
|
|
23
|
+
codedFieldErrors,
|
|
23
24
|
resolveSchemaTransportSchemaDefinition,
|
|
24
25
|
validateSchemaPayload
|
|
25
26
|
} from "@jskit-ai/kernel/shared/validators";
|
|
@@ -360,9 +361,9 @@ async function normalizeRepositoryInputPayload(
|
|
|
360
361
|
});
|
|
361
362
|
return normalizeCrudRepositoryObjectInput(nextPayload);
|
|
362
363
|
} catch (error) {
|
|
363
|
-
const
|
|
364
|
-
if (
|
|
365
|
-
throw createValidationError(
|
|
364
|
+
const fieldErrors = codedFieldErrors(error);
|
|
365
|
+
if (fieldErrors) {
|
|
366
|
+
throw createValidationError(fieldErrors);
|
|
366
367
|
}
|
|
367
368
|
throw error;
|
|
368
369
|
}
|
|
@@ -61,7 +61,7 @@ async function crudServiceGetRecord(runtime, repository, fieldAccess = {}, recor
|
|
|
61
61
|
const resolvedRepository = requireCrudServiceRepository(runtime, repository);
|
|
62
62
|
const record = await resolvedRepository.findById(recordId, options);
|
|
63
63
|
if (!record) {
|
|
64
|
-
throw new AppError(404, "Record not found.");
|
|
64
|
+
throw new AppError(404, "Record not found.", { code: "crud.record.notFound" });
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
return runtime.fieldAccessRuntime.filterReadableRecord(record, fieldAccess, {
|
|
@@ -95,7 +95,7 @@ async function crudServiceUpdateRecord(runtime, repository, fieldAccess = {}, re
|
|
|
95
95
|
const resolvedRepository = requireCrudServiceRepository(runtime, repository);
|
|
96
96
|
const existingRecord = await resolvedRepository.findById(recordId, options);
|
|
97
97
|
if (!existingRecord) {
|
|
98
|
-
throw new AppError(404, "Record not found.");
|
|
98
|
+
throw new AppError(404, "Record not found.", { code: "crud.record.notFound" });
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
const writablePayload = await runtime.fieldAccessRuntime.enforceWritablePayload(payload, fieldAccess, {
|
|
@@ -112,7 +112,7 @@ async function crudServiceUpdateRecord(runtime, repository, fieldAccess = {}, re
|
|
|
112
112
|
existingRecord
|
|
113
113
|
});
|
|
114
114
|
if (!record) {
|
|
115
|
-
throw new AppError(404, "Record not found.");
|
|
115
|
+
throw new AppError(404, "Record not found.", { code: "crud.record.notFound" });
|
|
116
116
|
}
|
|
117
117
|
return runtime.fieldAccessRuntime.filterReadableRecord(record, fieldAccess, {
|
|
118
118
|
action: "update",
|
|
@@ -126,7 +126,7 @@ async function crudServiceDeleteRecord(runtime, repository, fieldAccess = {}, re
|
|
|
126
126
|
const resolvedRepository = requireCrudServiceRepository(runtime, repository);
|
|
127
127
|
const deleted = await resolvedRepository.deleteById(recordId, options);
|
|
128
128
|
if (!deleted) {
|
|
129
|
-
throw new AppError(404, "Record not found.");
|
|
129
|
+
throw new AppError(404, "Record not found.", { code: "crud.record.notFound" });
|
|
130
130
|
}
|
|
131
131
|
return deleted;
|
|
132
132
|
}
|
|
@@ -150,6 +150,45 @@ test("defineCrudJsonApiFeature composes one resource without ceremonial token la
|
|
|
150
150
|
await runtime.shutdown();
|
|
151
151
|
});
|
|
152
152
|
|
|
153
|
+
test("defineCrudJsonApiFeature sends record.changed to its realtime audience", async () => {
|
|
154
|
+
const execution = { input: { recordId: "5" }, result: { data: { id: "5" } }, context: {} };
|
|
155
|
+
for (const [options, audience] of [[{}, "event_scope"], [{ realtimeAudience: "all_users" }, "all_users"]]) {
|
|
156
|
+
let actions;
|
|
157
|
+
const observer = defineProvider({
|
|
158
|
+
id: "test.observer",
|
|
159
|
+
requires: { actions: "runtime.actions", books: "crud.books" },
|
|
160
|
+
setup(dependencies) {
|
|
161
|
+
actions = dependencies.actions;
|
|
162
|
+
return {};
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
const runtime = createCapabilityRuntime({
|
|
166
|
+
providers: [
|
|
167
|
+
createActionProvider(),
|
|
168
|
+
...createFeatureDependencies([]),
|
|
169
|
+
defineCrudJsonApiFeature({
|
|
170
|
+
resource: createBookResource({ apiAccess: "public", autofilter: "public" }),
|
|
171
|
+
surface: "home",
|
|
172
|
+
...options
|
|
173
|
+
}),
|
|
174
|
+
observer
|
|
175
|
+
]
|
|
176
|
+
});
|
|
177
|
+
await runtime.start();
|
|
178
|
+
|
|
179
|
+
assert.deepEqual(
|
|
180
|
+
actions.listDefinitions()
|
|
181
|
+
.filter((entry) => entry.events.length > 0)
|
|
182
|
+
.map((entry) => [entry.id, entry.events[0](execution).realtime]),
|
|
183
|
+
["create", "update", "delete"].map((operation) => [
|
|
184
|
+
`crud.books.${operation}`,
|
|
185
|
+
{ event: "books.record.changed", audience }
|
|
186
|
+
])
|
|
187
|
+
);
|
|
188
|
+
await runtime.shutdown();
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
153
192
|
test("defineCrudJsonApiFeature makes workspace scope and permissions explicit", async () => {
|
|
154
193
|
const routes = [];
|
|
155
194
|
const feature = defineCrudJsonApiFeature({
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { createSchema, validateSchemaPayload } from "@jskit-ai/kernel/shared/validators";
|
|
4
|
+
import { catalogMessage, formatMessage, messageKeyForCode, mergeCatalogs } from "@jskit-ai/kernel/shared/i18n";
|
|
5
|
+
import kernelEnglish from "@jskit-ai/kernel/locales/en.json" with { type: "json" };
|
|
6
|
+
|
|
7
|
+
import crudEnglish from "../locales/en.json" with { type: "json" };
|
|
8
|
+
import { normalizeCrudListCursor } from "../src/server/repositorySupport.js";
|
|
9
|
+
import {
|
|
10
|
+
createJsonApiFieldsetsQueryValidator,
|
|
11
|
+
jsonApiFieldsetsQueryValidator,
|
|
12
|
+
lookupIncludeQueryValidator
|
|
13
|
+
} from "../src/server/listQueryValidators.js";
|
|
14
|
+
import { createCrudJsonApiService } from "../src/server/jsonApiModule/service.js";
|
|
15
|
+
import {
|
|
16
|
+
createCrudServiceRuntime,
|
|
17
|
+
crudServiceCreateRecord,
|
|
18
|
+
crudServiceDeleteRecord,
|
|
19
|
+
crudServiceGetRecord,
|
|
20
|
+
crudServiceUpdateRecord
|
|
21
|
+
} from "../src/server/serviceMethods.js";
|
|
22
|
+
|
|
23
|
+
const english = mergeCatalogs(kernelEnglish, crudEnglish);
|
|
24
|
+
|
|
25
|
+
// The client translates a code only when the server's English is the catalog's
|
|
26
|
+
// English for that code, rendered with the params.
|
|
27
|
+
function assertCatalogRenders({ code, params }, sentEnglish) {
|
|
28
|
+
const template = catalogMessage(english, messageKeyForCode(code));
|
|
29
|
+
assert.notEqual(template, undefined, `${code} has no English`);
|
|
30
|
+
assert.equal(formatMessage(template, params), sentEnglish, code);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function rejection(run) {
|
|
34
|
+
try {
|
|
35
|
+
await run();
|
|
36
|
+
} catch (error) {
|
|
37
|
+
return error;
|
|
38
|
+
}
|
|
39
|
+
return assert.fail("expected an error");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test("crud-core errors carry codes and params rendering their English", async () => {
|
|
43
|
+
const runtime = createCrudServiceRuntime({ namespace: "contacts" });
|
|
44
|
+
const missingRepository = {
|
|
45
|
+
async findById() {
|
|
46
|
+
return null;
|
|
47
|
+
},
|
|
48
|
+
async create(payload) {
|
|
49
|
+
return { id: 1, ...payload };
|
|
50
|
+
},
|
|
51
|
+
async deleteById() {
|
|
52
|
+
return null;
|
|
53
|
+
},
|
|
54
|
+
async getDocumentById() {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const errors = [
|
|
60
|
+
await rejection(() => crudServiceGetRecord(runtime, missingRepository, {}, 7, {})),
|
|
61
|
+
await rejection(() => crudServiceUpdateRecord(runtime, missingRepository, {}, 7, {}, {})),
|
|
62
|
+
await rejection(() => crudServiceDeleteRecord(runtime, missingRepository, {}, 7, {})),
|
|
63
|
+
await rejection(() => crudServiceCreateRecord(runtime, missingRepository, {
|
|
64
|
+
writable: () => ["name"],
|
|
65
|
+
writeMode: "throw"
|
|
66
|
+
}, { name: "Ada", role: "owner", secret: "x" }, {})),
|
|
67
|
+
await rejection(() => createCrudJsonApiService({ repository: missingRepository }).getDocumentById(7)),
|
|
68
|
+
await rejection(() => normalizeCrudListCursor("abc"))
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
assert.deepEqual(errors.map((error) => [error.status, error.code]), [
|
|
72
|
+
[404, "crud.record.notFound"],
|
|
73
|
+
[404, "crud.record.notFound"],
|
|
74
|
+
[404, "crud.record.notFound"],
|
|
75
|
+
[403, "crud.fieldAccess.writeDenied"],
|
|
76
|
+
[404, "crud.document.notFound"],
|
|
77
|
+
[400, "INVALID_CURSOR"]
|
|
78
|
+
]);
|
|
79
|
+
assert.deepEqual(errors[3].params, { fields: "role, secret" });
|
|
80
|
+
for (const error of errors) {
|
|
81
|
+
assertCatalogRenders(error, error.message);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("crud list query messages are coded field errors", () => {
|
|
86
|
+
const resource = {
|
|
87
|
+
namespace: "bookings",
|
|
88
|
+
operations: {
|
|
89
|
+
view: {
|
|
90
|
+
output: {
|
|
91
|
+
schema: createSchema({
|
|
92
|
+
petId: { type: "integer", belongsTo: "pets", as: "pet", relation: { labelKey: "name" } }
|
|
93
|
+
}),
|
|
94
|
+
mode: "replace"
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const failures = [
|
|
100
|
+
[lookupIncludeQueryValidator, { include: {} }, "include", "crud.query.includeInvalid"],
|
|
101
|
+
[jsonApiFieldsetsQueryValidator, { fields: "petId" }, "fields", "crud.query.fieldsInvalid"],
|
|
102
|
+
[createJsonApiFieldsetsQueryValidator({ resource }), { fields: "petId" }, "fields", "crud.query.fieldsByTypeInvalid"],
|
|
103
|
+
[createJsonApiFieldsetsQueryValidator({ resource }), { fields: { pet: ["name"] } }, "fields.pet",
|
|
104
|
+
"crud.query.fieldsKeysInvalidWithAliases"]
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
for (const [validator, payload, field, code] of failures) {
|
|
108
|
+
let error = null;
|
|
109
|
+
try {
|
|
110
|
+
validateSchemaPayload(validator, payload, { phase: "input" });
|
|
111
|
+
} catch (caught) {
|
|
112
|
+
error = caught;
|
|
113
|
+
}
|
|
114
|
+
assert.equal(error?.fieldErrorCodes?.[field]?.code, code, JSON.stringify(payload));
|
|
115
|
+
assertCatalogRenders(error.fieldErrorCodes[field], error.fieldErrors[field]);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import test from "node:test";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { evaluateI18nCatalogContract } from "../../../tooling/test-support/i18nCatalogContract.mjs";
|
|
6
|
+
|
|
7
|
+
const TEST_DIRECTORY = path.dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
const REPO_ROOT = path.resolve(TEST_DIRECTORY, "..", "..", "..");
|
|
9
|
+
|
|
10
|
+
test("crud-core English catalog matches its source", () => {
|
|
11
|
+
const { problems } = evaluateI18nCatalogContract({
|
|
12
|
+
repoRoot: REPO_ROOT,
|
|
13
|
+
packageDir: path.join(REPO_ROOT, "packages", "crud-core")
|
|
14
|
+
});
|
|
15
|
+
assert.deepEqual(problems, []);
|
|
16
|
+
});
|
|
@@ -998,7 +998,9 @@ test("resourceRuntime maps schema field errors for create and update", async ()
|
|
|
998
998
|
() => repository.create({ firstName: " " }),
|
|
999
999
|
(error) => (
|
|
1000
1000
|
error?.status === 400 &&
|
|
1001
|
-
|
|
1001
|
+
error?.code === "validation_failed" &&
|
|
1002
|
+
typeof error?.details?.fieldErrors?.firstName === "string" &&
|
|
1003
|
+
error.details.fieldErrorCodes?.firstName?.code?.startsWith("kernel.validation.field.")
|
|
1002
1004
|
)
|
|
1003
1005
|
);
|
|
1004
1006
|
|