Skip to content

Commit 92d5d10

Browse files
authored
Merge pull request #77 from mitre-attack/bug/error-loading-latest-enterprise-attack-data
fix(bundle): allow bundle parsing under Zod v4 pick-with-refinements …
2 parents 66d162d + 6725f1a commit 92d5d10

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎src/main.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { v4 as uuidv4 } from 'uuid';
33
import { logger } from '@/logger.js';
44

55
import {
6-
stixBundleSchema,
6+
stixBundleBaseSchema,
77
type AttackObject,
88
type StixBundle,
99
} from './schemas/sdo/stix-bundle.schema.js';
@@ -202,7 +202,7 @@ function parseStixBundle(rawData: StixBundle, parsingMode: ParsingMode): AttackO
202202
const validObjects: AttackObject[] = [];
203203

204204
// Validate the bundle's top-level properties
205-
const baseBundleValidationResults = stixBundleSchema
205+
const baseBundleValidationResults = stixBundleBaseSchema
206206
.pick({
207207
id: true,
208208
type: true,

‎src/schemas/sdo/stix-bundle.schema.ts‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ export type AttackObjects = z.infer<typeof attackObjectsSchema>;
160160
//
161161
//==============================================================================
162162

163-
export const stixBundleSchema = z
163+
// Base schema without refinements so it can be composed with `.pick()`, `.extend()`, etc.
164+
// Zod v4 forbids `.pick()` on schemas containing refinements (`.check()`), so the
165+
// refinements below are attached to `stixBundleSchema` rather than this base.
166+
export const stixBundleBaseSchema = z
164167
.object({
165168
id: createStixIdValidator('bundle'),
166169
type: createStixTypeValidator('bundle'),
@@ -190,20 +193,21 @@ export const stixBundleSchema = z
190193
description:
191194
'A Bundle is a collection of arbitrary STIX Objects grouped together in a single container. A Bundle does not have any semantic meaning and the objects contained within the Bundle are not considered related by virtue of being in the same Bundle. A STIX Bundle Object is not a STIX Object but makes use of the type and id Common Properties.',
192195
})
193-
.strict()
194-
.check((ctx) => {
195-
// Validate that the first object is 'x-mitre-collection' and only one collection exists
196-
validateXMitreCollection()(ctx);
196+
.strict();
197197

198-
// Validate that all IDs referenced in 'x_mitre_contents' are present in 'objects' array
199-
validateXMitreContentsReferences()(ctx);
198+
export const stixBundleSchema = stixBundleBaseSchema.check((ctx) => {
199+
// Validate that the first object is 'x-mitre-collection' and only one collection exists
200+
validateXMitreCollection()(ctx);
200201

201-
// Validate that no duplicate objects are present in 'objects' array
202-
validateNoDuplicates(
203-
['objects'],
204-
['id'],
205-
'Duplicate object with id "{id}" found. Each object in the bundle must have a unique id.',
206-
)(ctx);
207-
});
202+
// Validate that all IDs referenced in 'x_mitre_contents' are present in 'objects' array
203+
validateXMitreContentsReferences()(ctx);
204+
205+
// Validate that no duplicate objects are present in 'objects' array
206+
validateNoDuplicates(
207+
['objects'],
208+
['id'],
209+
'Duplicate object with id "{id}" found. Each object in the bundle must have a unique id.',
210+
)(ctx);
211+
});
208212

209213
export type StixBundle = z.infer<typeof stixBundleSchema>;

0 commit comments

Comments
 (0)