Skip to content

Commit 53b17ca

Browse files
committed
fix(json-schema-5-samples): fix example for time string (#10420)
1 parent fceaec6 commit 53b17ca

File tree

2 files changed

+31
-6
lines changed
  • src/core/plugins/json-schema-5-samples/fn
  • test/unit/core/plugins/json-schema-5-samples/fn

2 files changed

+31
-6
lines changed

src/core/plugins/json-schema-5-samples/fn/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const primitives = {
1919
"string_email": () => "[email protected]",
2020
"string_date-time": () => new Date().toISOString(),
2121
"string_date": () => new Date().toISOString().substring(0, 10),
22+
"string_time": () => new Date().toISOString().substring(11, 23),
2223
"string_uuid": () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
2324
"string_hostname": () => "example.com",
2425
"string_ipv4": () => "198.51.100.42",

test/unit/core/plugins/json-schema-5-samples/fn/index.js

+30-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ import {
88
} from "core/plugins/json-schema-5-samples/fn/index"
99

1010
describe("sampleFromSchema", () => {
11+
const oriDate = Date
12+
13+
beforeEach(() => {
14+
// eslint-disable-next-line no-global-assign
15+
Date = function () {
16+
this.toISOString = function () {
17+
return "2025-04-18T09:13:28.927Z"
18+
}
19+
}
20+
})
21+
22+
afterEach(() => {
23+
// eslint-disable-next-line no-global-assign
24+
Date = oriDate
25+
})
26+
1127
it("handles Immutable.js objects for nested schemas", function () {
1228
let definition = fromJS({
1329
"type": "object",
@@ -341,12 +357,9 @@ describe("sampleFromSchema", () => {
341357
format: "date-time"
342358
}
343359

344-
// 0-20 chops off milliseconds
345-
// necessary because test latency can cause failures
346-
// it would be better to mock Date globally and expect a string - KS 11/18
347-
let expected = new Date().toISOString().substring(0, 20)
360+
let expected = "2025-04-18T09:13:28.927Z"
348361

349-
expect(sampleFromSchema(definition)).toContain(expected)
362+
expect(sampleFromSchema(definition)).toEqual(expected)
350363
})
351364

352365
it("returns example value for date property", () => {
@@ -355,7 +368,18 @@ describe("sampleFromSchema", () => {
355368
format: "date"
356369
}
357370

358-
let expected = new Date().toISOString().substring(0, 10)
371+
let expected = "2025-04-18"
372+
373+
expect(sampleFromSchema(definition)).toEqual(expected)
374+
})
375+
376+
it("returns example value for time property", () => {
377+
let definition = {
378+
type: "string",
379+
format: "time"
380+
}
381+
382+
let expected = "09:13:28.927"
359383

360384
expect(sampleFromSchema(definition)).toEqual(expected)
361385
})

0 commit comments

Comments
 (0)