Skip to content

fix(json-schema-5-samples): fix example for time string (#10420) #10421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/plugins/json-schema-5-samples/fn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const primitives = {
"string_email": () => "[email protected]",
"string_date-time": () => new Date().toISOString(),
"string_date": () => new Date().toISOString().substring(0, 10),
"string_time": () => new Date().toISOString().substring(11, 23),
"string_uuid": () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"string_hostname": () => "example.com",
"string_ipv4": () => "198.51.100.42",
Expand Down
36 changes: 30 additions & 6 deletions test/unit/core/plugins/json-schema-5-samples/fn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import {
} from "core/plugins/json-schema-5-samples/fn/index"

describe("sampleFromSchema", () => {
const oriDate = Date

beforeEach(() => {
// eslint-disable-next-line no-global-assign
Date = function () {
this.toISOString = function () {
return "2025-04-18T09:13:28.927Z"
}
}
})

afterEach(() => {
// eslint-disable-next-line no-global-assign
Date = oriDate
})

it("handles Immutable.js objects for nested schemas", function () {
let definition = fromJS({
"type": "object",
Expand Down Expand Up @@ -341,12 +357,9 @@ describe("sampleFromSchema", () => {
format: "date-time"
}

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

expect(sampleFromSchema(definition)).toContain(expected)
expect(sampleFromSchema(definition)).toEqual(expected)
})

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

let expected = new Date().toISOString().substring(0, 10)
let expected = "2025-04-18"

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

it("returns example value for time property", () => {
let definition = {
type: "string",
format: "time"
}

let expected = "09:13:28.927"

expect(sampleFromSchema(definition)).toEqual(expected)
})
Expand Down