@jskit-ai/kernel 0.1.167 → 0.1.169

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/kernel",
3
- "version": "0.1.167",
3
+ "version": "0.1.169",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "json-rest-schema": "^1.0.17"
@@ -86,9 +86,22 @@ function resolveJsonRestSchemaFieldMessages(schemaDefinition = null, fieldName =
86
86
  return normalizeObject(source.schema.getFieldMessages(normalizedFieldName));
87
87
  }
88
88
 
89
+ function resolveJsonRestSchemaParentFieldMessages(schemaDefinition = null, fieldName = "") {
90
+ const segments = normalizeText(fieldName).split(".").filter(Boolean);
91
+ if (segments.length < 2) {
92
+ return {};
93
+ }
94
+
95
+ segments.pop();
96
+ return resolveJsonRestSchemaFieldMessages(schemaDefinition, segments.join("."));
97
+ }
98
+
89
99
  function resolveJsonRestSchemaFieldErrorMessage(fieldName, entry, schemaDefinition = null) {
90
- const messages = resolveJsonRestSchemaFieldMessages(schemaDefinition, fieldName);
91
100
  const errorCode = normalizeText(entry?.code).toUpperCase();
101
+ const directMessages = resolveJsonRestSchemaFieldMessages(schemaDefinition, fieldName);
102
+ const messages = errorCode === "FIELD_NOT_ALLOWED" && Object.keys(directMessages).length < 1
103
+ ? resolveJsonRestSchemaParentFieldMessages(schemaDefinition, fieldName)
104
+ : directMessages;
92
105
  const messageKey = JSON_REST_SCHEMA_ERROR_MESSAGE_KEYS[errorCode];
93
106
 
94
107
  if (messageKey && typeof messages[messageKey] === "string") {
@@ -88,3 +88,41 @@ test("removed timestamp schemas fail instead of silently changing meaning", () =
88
88
  /No casting function for type: timestamp/
89
89
  );
90
90
  });
91
+
92
+ test("unknown nested fields use the owning object additional-properties message", () => {
93
+ const definition = {
94
+ schema: createSchema({
95
+ fields: {
96
+ type: "object",
97
+ schema: createSchema({
98
+ bookings: {
99
+ type: "array",
100
+ items: { type: "string" }
101
+ },
102
+ pets: {
103
+ type: "array",
104
+ items: { type: "string" }
105
+ }
106
+ }),
107
+ messages: {
108
+ additionalProperties: "fields keys must be JSON:API resource types: bookings, pets."
109
+ }
110
+ }
111
+ }),
112
+ mode: "patch"
113
+ };
114
+
115
+ assert.throws(
116
+ () => validateSchemaPayload(definition, {
117
+ fields: {
118
+ pet: ["name"]
119
+ }
120
+ }),
121
+ (error) => {
122
+ assert.deepEqual(error.fieldErrors, {
123
+ "fields.pet": "fields keys must be JSON:API resource types: bookings, pets."
124
+ });
125
+ return true;
126
+ }
127
+ );
128
+ });