|
29 | 29 | * asserted rather than argued), and the predicate is shown STATELESS, which |
30 | 30 | * is what a flagless `RegExp` buys and what a JSON Schema `pattern` — which |
31 | 31 | * has no flags to carry — means. |
| 32 | + * - `unique-items` — over a corpus of JSON scalar lists, against a |
| 33 | + * `uniqueItems` evaluator that compares the JSON way (type, then value) |
| 34 | + * rather than through a `Set`, including `0` beside `-0` and `"1"` beside |
| 35 | + * `1`, the two places SameValueZero and JSON equality could have come |
| 36 | + * apart on scalars. |
32 | 37 | * |
33 | 38 | * ## Why an equality pin and not a comment |
34 | 39 | * |
@@ -59,7 +64,22 @@ import { |
59 | 64 | dependentRequired, |
60 | 65 | projectableRefinementOf, |
61 | 66 | requiredOneOf, |
| 67 | + UNIQUE_SCALAR_ITEMS, |
| 68 | + type JsonScalar, |
62 | 69 | } from '../src/shared/refinement-projection'; |
| 70 | +import { |
| 71 | + MAX_BUSINESS_FUNCTIONS, |
| 72 | + MAX_INDUSTRIES, |
| 73 | + MarketplaceBusinessFunctionsSchema, |
| 74 | + MarketplaceIndustriesSchema, |
| 75 | +} from '../src/marketplace/taxonomy.zod'; |
| 76 | +import { |
| 77 | + CreatePackageRequestSchema, |
| 78 | + PackageSchema, |
| 79 | + UpdatePackageRequestSchema, |
| 80 | +} from '../src/marketplace/package.zod'; |
| 81 | +import { MarketplaceListingSchema, MarketplaceSearchResponseSchema } from '../src/marketplace/marketplace.zod'; |
| 82 | +import { TemplateManifestSchema } from '../src/marketplace/template-manifest.zod'; |
63 | 83 | import { SSLConfigSchema } from '../src/data/driver-sql.zod'; |
64 | 84 | import { NormalizedFilterSchema } from '../src/data/filter.zod'; |
65 | 85 | import { TraceSamplingConfigSchema } from '../src/system/tracing.zod'; |
@@ -175,6 +195,7 @@ describe('the list of projectable patterns is CLOSED', () => { |
175 | 195 | 'dependent-required', |
176 | 196 | 'banned-keys', |
177 | 197 | 'banned-key-pattern', |
| 198 | + 'unique-items', |
178 | 199 | ]); |
179 | 200 | }); |
180 | 201 |
|
@@ -1087,3 +1108,182 @@ describe('generator and detector project through ONE call, not two conventions', |
1087 | 1108 | expect(census.projected.map((s) => s.declaredPatterns)).toEqual([['dependent-required']]); |
1088 | 1109 | }); |
1089 | 1110 | }); |
| 1111 | + |
| 1112 | +/** |
| 1113 | + * The node's `uniqueItems` rule, evaluated the way a validator would — JSON |
| 1114 | + * equality (same type, then same value), ⛔ deliberately NOT a `Set`, so the |
| 1115 | + * pin compares two spellings of the rule rather than one spelling with itself |
| 1116 | + * — and refusing to report anything when the node does not carry |
| 1117 | + * `uniqueItems: true`, so a projection that stopped emitting fails rather than |
| 1118 | + * passing vacuously. |
| 1119 | + */ |
| 1120 | +const uniqueItemsSatisfied = (node: Record<string, unknown>, doc: readonly unknown[]): boolean => { |
| 1121 | + if (node.uniqueItems !== true) { |
| 1122 | + throw new Error('the node carries no uniqueItems: true — nothing to evaluate'); |
| 1123 | + } |
| 1124 | + const jsonTypeOf = (value: unknown): string => (value === null ? 'null' : typeof value); |
| 1125 | + // JSON Schema compares numbers by mathematical value, so `0` and `-0` are |
| 1126 | + // one number; `==` on two same-typed scalars says exactly that. |
| 1127 | + const jsonEqual = (a: unknown, b: unknown): boolean => jsonTypeOf(a) === jsonTypeOf(b) && a == b; |
| 1128 | + return doc.every((item, i) => doc.slice(i + 1).every((other) => !jsonEqual(item, other))); |
| 1129 | +}; |
| 1130 | + |
| 1131 | +describe('unique-items: a Set and `uniqueItems` are ONE relation on JSON scalars', () => { |
| 1132 | + it('declares itself, and has nothing per call site to declare', () => { |
| 1133 | + expect(projectableRefinementOf(UNIQUE_SCALAR_ITEMS)).toEqual({ pattern: 'unique-items' }); |
| 1134 | + }); |
| 1135 | + |
| 1136 | + it('emits JSON Schema`s own `uniqueItems: true`, beside the node`s own keywords', () => { |
| 1137 | + const node = publish(z.array(z.string()).min(1).max(3).refine(UNIQUE_SCALAR_ITEMS, 'once')); |
| 1138 | + expect(node).toEqual({ ...publish(z.array(z.string()).min(1).max(3)), uniqueItems: true }); |
| 1139 | + // ⛔ Not nested: `uniqueItems` is no keyword the reference renderer reads |
| 1140 | + // as the node's type, so there is no `allOf` to hide it in. |
| 1141 | + expect(node.allOf).toBeUndefined(); |
| 1142 | + }); |
| 1143 | + |
| 1144 | + it('is idempotent — the same arm twice states one rule', () => { |
| 1145 | + const node: Record<string, unknown> = { type: 'array', items: { type: 'string' } }; |
| 1146 | + emitProjectableRefinement(node, { pattern: 'unique-items' }); |
| 1147 | + emitProjectableRefinement(node, { pattern: 'unique-items' }); |
| 1148 | + expect(node).toEqual({ type: 'array', items: { type: 'string' }, uniqueItems: true }); |
| 1149 | + }); |
| 1150 | + |
| 1151 | + it('the predicate and the keyword agree on every scalar list in the corpus', () => { |
| 1152 | + const node = publish(z.array(z.union([z.string(), z.number(), z.boolean(), z.null()])).refine(UNIQUE_SCALAR_ITEMS)); |
| 1153 | + const corpus: JsonScalar[][] = [ |
| 1154 | + [], |
| 1155 | + ['sales'], |
| 1156 | + ['sales', 'marketing'], |
| 1157 | + ['sales', 'sales'], |
| 1158 | + ['sales', 'marketing', 'sales'], |
| 1159 | + ['Sales', 'sales'], |
| 1160 | + ['1', 1], |
| 1161 | + [1, 1], |
| 1162 | + [0, -0], |
| 1163 | + [1, 1.0], |
| 1164 | + [true, 1], |
| 1165 | + [true, true], |
| 1166 | + [false, 0, '', null], |
| 1167 | + [null, null], |
| 1168 | + ]; |
| 1169 | + for (const list of corpus) { |
| 1170 | + // Through a JSON round trip on purpose: the domain is the document an |
| 1171 | + // author writes, and `JSON.parse` is what turns `1.0` into `1`. |
| 1172 | + const asJson = JSON.parse(JSON.stringify(list)) as JsonScalar[]; |
| 1173 | + // Equality, not implication: the arm is exact, so a one-sided pin would |
| 1174 | + // pass a projection that had stopped narrowing at all. |
| 1175 | + expect(uniqueItemsSatisfied(node, asJson), `disagreement on ${JSON.stringify(asJson)}`) |
| 1176 | + .toBe(UNIQUE_SCALAR_ITEMS(asJson)); |
| 1177 | + } |
| 1178 | + // `-0` survives `JSON.parse`, so the one SameValueZero-vs-`===` pair is |
| 1179 | + // asserted on the value itself rather than on its serialisation. |
| 1180 | + const negativeZero = JSON.parse('[0,-0]') as number[]; |
| 1181 | + expect(Object.is(negativeZero[1], -0)).toBe(true); |
| 1182 | + expect(UNIQUE_SCALAR_ITEMS(negativeZero)).toBe(false); |
| 1183 | + expect(uniqueItemsSatisfied(node, negativeZero)).toBe(false); |
| 1184 | + }); |
| 1185 | + |
| 1186 | + it('⛔ an array of OBJECTS cannot compose it — the compile-time half of the exactness argument', () => { |
| 1187 | + // Two equal objects parsed from one document are two members to a `Set` |
| 1188 | + // and one value to `uniqueItems`; the scalar parameter type keeps that |
| 1189 | + // disagreement out of the published file. |
| 1190 | + // @ts-expect-error — `{ a: string }` is not a JSON scalar |
| 1191 | + z.array(z.object({ a: z.string() })).refine(UNIQUE_SCALAR_ITEMS); |
| 1192 | + const twins = JSON.parse('[{"a":"x"},{"a":"x"}]') as unknown[]; |
| 1193 | + expect(new Set(twins).size).toBe(2); |
| 1194 | + }); |
| 1195 | +}); |
| 1196 | + |
| 1197 | +describe('the LIVE seam: the marketplace taxonomy facets publish their no-duplicates rule', () => { |
| 1198 | + /** Every published schema the ledger named for the facets, with its own sites. */ |
| 1199 | + const SEAMS: ReadonlyArray<readonly [string, z.ZodType, readonly string[]]> = [ |
| 1200 | + ['marketplace/MarketplaceBusinessFunctions', MarketplaceBusinessFunctionsSchema, ['']], |
| 1201 | + ['marketplace/MarketplaceIndustries', MarketplaceIndustriesSchema, ['']], |
| 1202 | + ['marketplace/Package', PackageSchema, ['businessFunctions', 'industries']], |
| 1203 | + ['marketplace/CreatePackageRequest', CreatePackageRequestSchema, ['businessFunctions', 'industries']], |
| 1204 | + ['marketplace/UpdatePackageRequest', UpdatePackageRequestSchema, ['businessFunctions', 'industries']], |
| 1205 | + ['marketplace/MarketplaceListing', MarketplaceListingSchema, ['businessFunctions', 'industries']], |
| 1206 | + ['marketplace/TemplateManifest', TemplateManifestSchema, ['businessFunctions', 'industries']], |
| 1207 | + [ |
| 1208 | + 'marketplace/MarketplaceSearchResponse', |
| 1209 | + MarketplaceSearchResponseSchema, |
| 1210 | + ['items.element.businessFunctions', 'items.element.industries'], |
| 1211 | + ], |
| 1212 | + ]; |
| 1213 | + |
| 1214 | + it('both facet schemas state the rule, and keep the array shape they always stated', () => { |
| 1215 | + const facets: Array<readonly [z.ZodType, number]> = [ |
| 1216 | + [MarketplaceBusinessFunctionsSchema, MAX_BUSINESS_FUNCTIONS], |
| 1217 | + [MarketplaceIndustriesSchema, MAX_INDUSTRIES], |
| 1218 | + ]; |
| 1219 | + for (const [schema, max] of facets) { |
| 1220 | + const node = publish(schema); |
| 1221 | + expect(node.type).toBe('array'); |
| 1222 | + expect((node.items as Record<string, unknown>).enum).toBeInstanceOf(Array); |
| 1223 | + expect(node.maxItems).toBe(max); |
| 1224 | + expect(node.uniqueItems).toBe(true); |
| 1225 | + } |
| 1226 | + expect(publish(MarketplaceBusinessFunctionsSchema).minItems).toBe(1); |
| 1227 | + }); |
| 1228 | + |
| 1229 | + it('a schema that EMBEDS a facet states the rule at the facet`s own property', () => { |
| 1230 | + const props = publish(PackageSchema).properties as Record<string, Record<string, unknown>>; |
| 1231 | + expect(props.businessFunctions.uniqueItems).toBe(true); |
| 1232 | + expect(props.industries.uniqueItems).toBe(true); |
| 1233 | + }); |
| 1234 | + |
| 1235 | + it('⛔ the Zod parse is unchanged — it still refuses a duplicate and accepts the same lists', () => { |
| 1236 | + // The helper's predicate became a DECLARED one; the rule it enforces must |
| 1237 | + // not have moved. Same corpus, same verdicts, and the refusal still names |
| 1238 | + // the key with the helper's own message. |
| 1239 | + const verdicts: Array<readonly [unknown, boolean]> = [ |
| 1240 | + [['sales'], true], |
| 1241 | + [['sales', 'marketing'], true], |
| 1242 | + [['sales', 'marketing', 'it'], true], |
| 1243 | + [['sales', 'sales'], false], |
| 1244 | + [['sales', 'marketing', 'sales'], false], |
| 1245 | + [[], false], |
| 1246 | + [['sales', 'marketing', 'it', 'hr'], false], |
| 1247 | + [['not-a-function'], false], |
| 1248 | + ]; |
| 1249 | + for (const [list, accepted] of verdicts) { |
| 1250 | + expect(MarketplaceBusinessFunctionsSchema.safeParse(list).success, JSON.stringify(list)).toBe(accepted); |
| 1251 | + } |
| 1252 | + expect(MarketplaceIndustriesSchema.safeParse([]).success).toBe(true); |
| 1253 | + expect(MarketplaceIndustriesSchema.safeParse(['retail', 'retail']).success).toBe(false); |
| 1254 | + const refused = MarketplaceBusinessFunctionsSchema.safeParse(['sales', 'sales']); |
| 1255 | + expect(refused.success).toBe(false); |
| 1256 | + expect(refused.error?.issues.map((issue) => [issue.code, issue.message])).toEqual([ |
| 1257 | + ['custom', '`businessFunctions` lists a value more than once; list each value once.'], |
| 1258 | + ]); |
| 1259 | + }); |
| 1260 | + |
| 1261 | + it('⛔ the runtime and the emitted keywords agree on every facet list in the corpus', () => { |
| 1262 | + const node = publish(MarketplaceIndustriesSchema); |
| 1263 | + const corpus: string[][] = [ |
| 1264 | + [], |
| 1265 | + ['retail'], |
| 1266 | + ['retail', 'logistics'], |
| 1267 | + ['retail', 'retail'], |
| 1268 | + ['retail', 'logistics', 'retail'], |
| 1269 | + ['manufacturing', 'retail', 'logistics'], |
| 1270 | + ]; |
| 1271 | + for (const list of corpus) { |
| 1272 | + // Uniqueness only, deliberately: every list here is valid in every other |
| 1273 | + // respect, so the runtime's verdict is the duplicates rule's verdict. |
| 1274 | + expect(uniqueItemsSatisfied(node, list), `disagreement on ${JSON.stringify(list)}`) |
| 1275 | + .toBe(MarketplaceIndustriesSchema.safeParse(list).success); |
| 1276 | + } |
| 1277 | + }); |
| 1278 | + |
| 1279 | + it('their eight ledger rows are gone because every site now reads `projected`, naming the arm', () => { |
| 1280 | + for (const [defKey, schema, paths] of SEAMS) { |
| 1281 | + const census = collectDroppedRefinements(defKey, schema); |
| 1282 | + expect(census.dropped, defKey).toEqual([]); |
| 1283 | + for (const path of paths) { |
| 1284 | + const site = census.projected.find((s) => s.path === path); |
| 1285 | + expect(site?.declaredPatterns, `${defKey} @ ${JSON.stringify(path)}`).toEqual(['unique-items']); |
| 1286 | + } |
| 1287 | + } |
| 1288 | + }); |
| 1289 | +}); |
0 commit comments