Skip to content

Commit e096b12

Browse files
committed
fix(ibm-use-date-based-format): handle null example values (#724)
Previously, if the example value given for a property was an explicit 'null', this rule would crash the validator. This commit filters null examples from the logic so that they won't cause any trouble. Signed-off-by: Dustin Popp <[email protected]>
1 parent 357dfb2 commit e096b12

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

packages/ruleset/src/functions/use-date-based-format.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ function checkForDateBasedFormat(s, p, apidef) {
147147
// order the schemas are checked in (the logic may look at more examples
148148
// for one instance of a property than another, arbitrarily) and we don't
149149
// make a guarantee that order will be stable in `validateNestedSchemas`.
150-
examples[logicalPath.join('.')] = [...schemaExamples, ...parentalExamples];
150+
examples[logicalPath.join('.')] = [
151+
...schemaExamples,
152+
...parentalExamples,
153+
].filter(e => e !== null); // Null values are technically allowed - don't keep them.
151154

152155
// Perform the validation using the first value example value found for the
153156
// schema at this logical path.

packages/ruleset/test/rules/use-date-based-format.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,40 @@ describe(`Spectral rule: ${ruleId}`, () => {
206206
expect(results).toHaveLength(0);
207207
});
208208

209+
it('date/time dictionary with null example', async () => {
210+
const testDocument = makeCopy(rootDocument);
211+
testDocument.components.schemas.Movie.properties.changes = {
212+
type: 'object',
213+
example: {
214+
issues: [
215+
{
216+
metadata: null,
217+
},
218+
],
219+
},
220+
properties: {
221+
issues: {
222+
type: 'array',
223+
items: {
224+
type: 'object',
225+
properties: {
226+
metadata: {
227+
type: 'object',
228+
additionalProperties: {
229+
type: 'string',
230+
format: 'date-time',
231+
},
232+
},
233+
},
234+
},
235+
},
236+
},
237+
};
238+
239+
const results = await testRule(ruleId, rule, testDocument);
240+
expect(results).toHaveLength(0);
241+
});
242+
209243
it('primitive date/time oneOf schema with example', async () => {
210244
const testDocument = makeCopy(rootDocument);
211245
testDocument.components.schemas.Movie.properties.first_completed = {

0 commit comments

Comments
 (0)