Skip to content
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

feat(samples): add support for const keyword #8884

Merged
merged 1 commit into from
Jun 6, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const liftSampleHelper = (oldSchema, target, config = {}) => {
"enum",
"xml",
"type",
"const",
...objectContracts,
...arrayContracts,
...numberContracts,
Expand Down Expand Up @@ -683,7 +684,10 @@ export const sampleFromSchemaGeneric = (
}

let value
if (schema && Array.isArray(schema.enum)) {
if (typeof schema?.const !== "undefined") {
// display const value
value = schema.const
} else if (schema && Array.isArray(schema.enum)) {
//display enum first value
value = normalizeArray(schema.enum)[0]
} else if (schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,33 @@ describe("sampleFromSchema", () => {
})

it("should handle type keyword defined as list of types", function () {
const definition = fromJS({
type: ["object", "string"],
})
const definition = fromJS({ type: ["object", "string"] })
const expected = {}

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

it("should prioritize array when array and object defined as list of types", function () {
const definition = fromJS({
type: ["object", "array"],
})
const definition = fromJS({ type: ["object", "array"] })
const expected = []

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

it("should handle primitive types defined as list of types", function () {
const definition = fromJS({
type: ["string", "number"],
})
const definition = fromJS({ type: ["string", "number"] })
const expected = "string"

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

it("should return const value", function () {
const definition = fromJS({ const: 3 })
const expected = 3

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

it("handles Immutable.js objects for nested schemas", function () {
const definition = fromJS({
type: "object",
Expand Down