@formspec/build 0.1.0-alpha.30 → 0.1.0-alpha.31

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/dist/index.js CHANGED
@@ -932,9 +932,9 @@ function collectFields(elements, properties, required, ctx) {
932
932
  for (const element of elements) {
933
933
  switch (element.kind) {
934
934
  case "field":
935
- properties[getSerializedName(element.name, element.metadata)] = generateFieldSchema(element, ctx);
935
+ properties[getSerializedFieldName(element)] = generateFieldSchema(element, ctx);
936
936
  if (element.required) {
937
- required.push(getSerializedName(element.name, element.metadata));
937
+ required.push(getSerializedFieldName(element));
938
938
  }
939
939
  break;
940
940
  case "group":
@@ -1005,19 +1005,21 @@ function applyPathTargetedConstraints(schema, pathConstraints, ctx, typeNode) {
1005
1005
  schema.items = applyPathTargetedConstraints(schema.items, pathConstraints, ctx, nestedType);
1006
1006
  return schema;
1007
1007
  }
1008
- const byTarget = /* @__PURE__ */ new Map();
1009
- for (const c of pathConstraints) {
1010
- const target = c.path?.segments[0];
1011
- if (!target) continue;
1012
- const group = byTarget.get(target) ?? [];
1013
- group.push(c);
1014
- byTarget.set(target, group);
1015
- }
1016
- const propertyOverrides = {};
1017
- for (const [target, constraints] of byTarget) {
1018
- const subSchema = {};
1019
- applyConstraints(subSchema, constraints, ctx);
1020
- propertyOverrides[resolveSerializedPropertyName(target, typeNode, ctx)] = subSchema;
1008
+ const propertyOverrides = buildPropertyOverrides(pathConstraints, typeNode, ctx);
1009
+ const nullableValueBranch = getNullableUnionValueSchema(schema);
1010
+ if (nullableValueBranch !== void 0) {
1011
+ const updatedNullableValueBranch = applyPathTargetedConstraints(
1012
+ nullableValueBranch,
1013
+ pathConstraints,
1014
+ ctx,
1015
+ resolveTraversableTypeNode(typeNode, ctx)
1016
+ );
1017
+ if (schema.oneOf !== void 0) {
1018
+ schema.oneOf = schema.oneOf.map(
1019
+ (branch) => branch === nullableValueBranch ? updatedNullableValueBranch : branch
1020
+ );
1021
+ }
1022
+ return schema;
1021
1023
  }
1022
1024
  if (schema.$ref) {
1023
1025
  const { $ref, ...rest } = schema;
@@ -1032,7 +1034,7 @@ function applyPathTargetedConstraints(schema, pathConstraints, ctx, typeNode) {
1032
1034
  const missingOverrides = {};
1033
1035
  for (const [target, overrideSchema] of Object.entries(propertyOverrides)) {
1034
1036
  if (schema.properties[target]) {
1035
- Object.assign(schema.properties[target], overrideSchema);
1037
+ mergeSchemaOverride(schema.properties[target], overrideSchema);
1036
1038
  } else {
1037
1039
  missingOverrides[target] = overrideSchema;
1038
1040
  }
@@ -1106,7 +1108,7 @@ function generateObjectType(type, ctx) {
1106
1108
  const properties = {};
1107
1109
  const required = [];
1108
1110
  for (const prop of type.properties) {
1109
- const propertyName = getSerializedName(prop.name, prop.metadata);
1111
+ const propertyName = getSerializedObjectPropertyName(prop);
1110
1112
  properties[propertyName] = generatePropertySchema(prop, ctx);
1111
1113
  if (!prop.optional) {
1112
1114
  required.push(propertyName);
@@ -1160,7 +1162,16 @@ function isNullableUnion(type) {
1160
1162
  return nullCount === 1;
1161
1163
  }
1162
1164
  function generateReferenceType(type, ctx) {
1163
- return { $ref: `#/$defs/${ctx.typeNameMap[type.name] ?? type.name}` };
1165
+ return { $ref: `#/$defs/${getSerializedTypeName(type.name, ctx)}` };
1166
+ }
1167
+ function getSerializedFieldName(field) {
1168
+ return getSerializedName(field.name, field.metadata);
1169
+ }
1170
+ function getSerializedObjectPropertyName(property) {
1171
+ return getSerializedName(property.name, property.metadata);
1172
+ }
1173
+ function getSerializedTypeName(logicalName, ctx) {
1174
+ return ctx.typeNameMap[logicalName] ?? logicalName;
1164
1175
  }
1165
1176
  function applyResolvedMetadata(schema, metadata) {
1166
1177
  const displayName = getDisplayName(metadata);
@@ -1171,17 +1182,148 @@ function applyResolvedMetadata(schema, metadata) {
1171
1182
  function resolveReferencedType(type, ctx) {
1172
1183
  return ctx.typeRegistry[type.name]?.type;
1173
1184
  }
1185
+ function dereferenceTypeNode(typeNode, ctx) {
1186
+ if (typeNode?.kind !== "reference") {
1187
+ return typeNode;
1188
+ }
1189
+ return resolveReferencedType(typeNode, ctx);
1190
+ }
1191
+ function unwrapNullableTypeNode(typeNode) {
1192
+ if (typeNode?.kind !== "union" || !isNullableUnion(typeNode)) {
1193
+ return typeNode;
1194
+ }
1195
+ return typeNode.members.find(
1196
+ (member) => !(member.kind === "primitive" && member.primitiveKind === "null")
1197
+ );
1198
+ }
1199
+ function resolveTraversableTypeNode(typeNode, ctx) {
1200
+ const dereferenced = dereferenceTypeNode(typeNode, ctx);
1201
+ const unwrapped = unwrapNullableTypeNode(dereferenced);
1202
+ if (unwrapped !== dereferenced) {
1203
+ return resolveTraversableTypeNode(unwrapped, ctx);
1204
+ }
1205
+ return dereferenced;
1206
+ }
1174
1207
  function resolveSerializedPropertyName(logicalName, typeNode, ctx) {
1175
- if (typeNode?.kind === "object") {
1176
- const property = typeNode.properties.find((candidate) => candidate.name === logicalName);
1177
- return property === void 0 ? logicalName : getSerializedName(property.name, property.metadata);
1208
+ const effectiveType = resolveTraversableTypeNode(typeNode, ctx);
1209
+ if (effectiveType?.kind === "array") {
1210
+ return resolveSerializedPropertyName(logicalName, effectiveType.items, ctx);
1178
1211
  }
1179
- if (typeNode?.kind === "reference") {
1180
- const referencedType = resolveReferencedType(typeNode, ctx);
1181
- return referencedType === void 0 ? logicalName : resolveSerializedPropertyName(logicalName, referencedType, ctx);
1212
+ if (effectiveType?.kind === "object") {
1213
+ const property = effectiveType.properties.find((candidate) => candidate.name === logicalName);
1214
+ return property === void 0 ? logicalName : getSerializedObjectPropertyName(property);
1182
1215
  }
1183
1216
  return logicalName;
1184
1217
  }
1218
+ function resolveTargetTypeNode(logicalName, typeNode, ctx) {
1219
+ const effectiveType = resolveTraversableTypeNode(typeNode, ctx);
1220
+ if (effectiveType?.kind === "array") {
1221
+ return resolveTargetTypeNode(logicalName, effectiveType.items, ctx);
1222
+ }
1223
+ if (effectiveType?.kind !== "object") {
1224
+ return void 0;
1225
+ }
1226
+ return effectiveType.properties.find((candidate) => candidate.name === logicalName)?.type;
1227
+ }
1228
+ function buildPropertyOverrides(pathConstraints, typeNode, ctx) {
1229
+ const byTarget = /* @__PURE__ */ new Map();
1230
+ for (const constraint of pathConstraints) {
1231
+ const target = constraint.path?.segments[0];
1232
+ if (!target) {
1233
+ continue;
1234
+ }
1235
+ const grouped = byTarget.get(target) ?? [];
1236
+ grouped.push(constraint);
1237
+ byTarget.set(target, grouped);
1238
+ }
1239
+ const overrides = {};
1240
+ for (const [target, constraints] of byTarget) {
1241
+ overrides[resolveSerializedPropertyName(target, typeNode, ctx)] = buildPathOverrideSchema(
1242
+ constraints.map(stripLeadingPathSegment),
1243
+ resolveTargetTypeNode(target, typeNode, ctx),
1244
+ ctx
1245
+ );
1246
+ }
1247
+ return overrides;
1248
+ }
1249
+ function buildPathOverrideSchema(constraints, typeNode, ctx) {
1250
+ const schema = {};
1251
+ const directConstraints = [];
1252
+ const nestedConstraints = [];
1253
+ for (const constraint of constraints) {
1254
+ if (constraint.path === void 0 || constraint.path.segments.length === 0) {
1255
+ directConstraints.push(constraint);
1256
+ } else {
1257
+ nestedConstraints.push(constraint);
1258
+ }
1259
+ }
1260
+ applyConstraints(schema, directConstraints, ctx);
1261
+ if (nestedConstraints.length === 0) {
1262
+ return schema;
1263
+ }
1264
+ const effectiveType = resolveTraversableTypeNode(typeNode, ctx);
1265
+ if (effectiveType?.kind === "array") {
1266
+ schema.items = buildPathOverrideSchema(nestedConstraints, effectiveType.items, ctx);
1267
+ return schema;
1268
+ }
1269
+ schema.properties = buildPropertyOverrides(nestedConstraints, effectiveType, ctx);
1270
+ return schema;
1271
+ }
1272
+ function mergeSchemaOverride(target, override) {
1273
+ const nullableValueBranch = getNullableUnionValueSchema(target);
1274
+ if (nullableValueBranch !== void 0) {
1275
+ mergeSchemaOverride(nullableValueBranch, override);
1276
+ return;
1277
+ }
1278
+ if (override.properties !== void 0) {
1279
+ const mergedProperties = target.properties ?? {};
1280
+ for (const [name, propertyOverride] of Object.entries(override.properties)) {
1281
+ const existing = mergedProperties[name];
1282
+ if (existing === void 0) {
1283
+ mergedProperties[name] = propertyOverride;
1284
+ } else {
1285
+ mergeSchemaOverride(existing, propertyOverride);
1286
+ }
1287
+ }
1288
+ target.properties = mergedProperties;
1289
+ }
1290
+ if (override.items !== void 0) {
1291
+ if (target.items === void 0) {
1292
+ target.items = override.items;
1293
+ } else {
1294
+ mergeSchemaOverride(target.items, override.items);
1295
+ }
1296
+ }
1297
+ for (const [key, value] of Object.entries(override)) {
1298
+ if (key === "properties" || key === "items") {
1299
+ continue;
1300
+ }
1301
+ target[key] = value;
1302
+ }
1303
+ }
1304
+ function stripLeadingPathSegment(constraint) {
1305
+ const segments = constraint.path?.segments;
1306
+ if (segments === void 0 || segments.length === 0) {
1307
+ return constraint;
1308
+ }
1309
+ const [, ...rest] = segments;
1310
+ if (rest.length === 0) {
1311
+ const { path: _path, ...stripped } = constraint;
1312
+ return stripped;
1313
+ }
1314
+ return {
1315
+ ...constraint,
1316
+ path: { segments: rest }
1317
+ };
1318
+ }
1319
+ function getNullableUnionValueSchema(schema) {
1320
+ if (schema.oneOf?.length !== 2) {
1321
+ return void 0;
1322
+ }
1323
+ const valueSchema = schema.oneOf.find((branch) => branch.type !== "null");
1324
+ const nullSchema = schema.oneOf.find((branch) => branch.type === "null");
1325
+ return valueSchema !== void 0 && nullSchema !== void 0 ? valueSchema : void 0;
1326
+ }
1185
1327
  function generateDynamicType(type) {
1186
1328
  if (type.dynamicKind === "enum") {
1187
1329
  const schema = {