-
Notifications
You must be signed in to change notification settings - Fork 9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): add support for examples keyword (#8908)
This change is specific to JSON Schema 2020-12 and OpenAPI 3.1.0. Refs #8577
- Loading branch information
Showing
7 changed files
with
116 additions
and
148 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/core/plugins/json-schema-2020-12/samples-extensions/fn/core/example.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import { isJSONSchemaObject } from "./predicates" | ||
|
||
/** | ||
* Precedence of keywords that provides author defined values (top of the list = higher priority) | ||
* | ||
* ### examples | ||
* Array containing example values for the item defined by the schema. | ||
* Not guaranteed to be valid or invalid against the schema | ||
* | ||
* ### default | ||
* Default value for an item defined by the schema. | ||
* Is expected to be a valid instance of the schema. | ||
* | ||
* ### example | ||
* Deprecated. Part of OpenAPI 3.1.0 Schema Object dialect. | ||
* Represents single example. Equivalent of `examples` keywords | ||
* with single item. | ||
*/ | ||
|
||
export const hasExample = (schema) => { | ||
if (!isJSONSchemaObject(schema)) return false | ||
|
||
const { examples, example, default: defaultVal } = schema | ||
|
||
if (Array.isArray(examples) && examples.length >= 1) { | ||
return true | ||
} | ||
|
||
if (typeof defaultVal !== "undefined") { | ||
return true | ||
} | ||
|
||
return typeof example !== "undefined" | ||
} | ||
|
||
export const extractExample = (schema) => { | ||
if (!isJSONSchemaObject(schema)) return null | ||
|
||
const { examples, example, default: defaultVal } = schema | ||
|
||
if (Array.isArray(examples) && examples.length >= 1) { | ||
return examples.at(0) | ||
} | ||
|
||
if (typeof defaultVal !== "undefined") { | ||
return defaultVal | ||
} | ||
|
||
if (typeof example !== "undefined") { | ||
return example | ||
} | ||
|
||
return undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.