-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for vendor extensions (#245)
--------- Co-authored-by: Weyert de Boer <weyert@innerfuse.biz> Co-authored-by: Jakub Rożek <jakub@stoplight.io>
- Loading branch information
1 parent
0737d66
commit 129d05c
Showing
10 changed files
with
207 additions
and
12 deletions.
There are no files selected for viewing
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,97 @@ | ||
{ | ||
"title": "User", | ||
"type": "object", | ||
"x-stoplight": { "id": "root-id" }, | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"const": "Constant name", | ||
"examples": ["Example name", "Different name"], | ||
"x-stoplight": { "id": "name-id" } | ||
}, | ||
"age": { | ||
"type": "number", | ||
"minimum": 10, | ||
"maximum": 40, | ||
"x-stoplight": { "id": "age-id" } | ||
}, | ||
"completed_at": { | ||
"type": "string", | ||
"format": "date-time", | ||
"x-stoplight": { "id": "completed_at-id" } | ||
}, | ||
"list": { | ||
"type": ["null", "array"], | ||
"items": { | ||
"type": ["string", "number"], | ||
"x-stoplight": { "id": "list-items-id" } | ||
}, | ||
"minItems": 1, | ||
"maxItems": 4, | ||
"x-stoplight": { "id": "list-id" } | ||
}, | ||
"email": { | ||
"type": "string", | ||
"format": "email", | ||
"examples": ["one@email.com", "two@email.com"], | ||
"deprecated": true, | ||
"default": "default@email.com", | ||
"minLength": 2, | ||
"x-stoplight": { "id": "email-id" } | ||
}, | ||
"list-of-objects": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"x-stoplight": { "id": "list-of-objects-items-id" }, | ||
"properties": { | ||
"id": { | ||
"type": "string", | ||
"x-stoplight": { "id": "list-of-objects-items-id-id" } | ||
}, | ||
"friend": { | ||
"type": "object", | ||
"x-stoplight": { "id": "list-of-objects-items-friend-id" }, | ||
"properties": { | ||
"id": { | ||
"type": "string", | ||
"x-stoplight": { "id": "list-of-objects-items-friend-id-id" } | ||
}, | ||
"name": { | ||
"type": "object", | ||
"x-stoplight": { "id": "list-of-objects-items-friend-name-id" }, | ||
"properties": { | ||
"first": { | ||
"type": "string", | ||
"x-stoplight": { "id": "list-of-objects-items-friend-name-first-id" } | ||
}, | ||
"last": { | ||
"type": "string", | ||
"x-stoplight": { "id": "list-of-objects-items-friend-name-last-id" } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"minItems": 1, | ||
"maxItems": 4, | ||
"x-stoplight": { "id": "list-of-objects-id" } | ||
}, | ||
"friend": { | ||
"type": "object", | ||
"x-stoplight": { "id": "friend-id" }, | ||
"properties": { | ||
"id": { | ||
"type": "string", | ||
"x-stoplight": { "id": "friend-id-id" } | ||
}, | ||
"name": { | ||
"type": "string", | ||
"x-stoplight": { "id": "friend-name-id" } | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Flex } from '@stoplight/mosaic'; | ||
import { Story } from '@storybook/react'; | ||
import { JSONSchema4 } from 'json-schema'; | ||
import React from 'react'; | ||
|
||
import { JsonSchemaProps, JsonSchemaViewer } from '../components/JsonSchemaViewer'; | ||
|
||
const defaultSchema = require('../__fixtures__/default-schema.json'); | ||
const extensionsSchema = require('../__fixtures__/extensions/simple.json'); | ||
|
||
export default { | ||
component: JsonSchemaViewer, | ||
argTypes: {}, | ||
}; | ||
|
||
const Template: Story<JsonSchemaProps> = ({ schema = defaultSchema as JSONSchema4, ...args }) => ( | ||
<JsonSchemaViewer schema={schema} {...args} /> | ||
); | ||
|
||
export const ExtensionRowSchema = Template.bind({}); | ||
ExtensionRowSchema.args = { | ||
schema: extensionsSchema as JSONSchema4, | ||
defaultExpandedDepth: Infinity, | ||
renderRootTreeLines: true, | ||
renderExtensionAddon: ({ nestingLevel, vendorExtensions }) => { | ||
if (nestingLevel < 1) { | ||
return null; | ||
} | ||
|
||
if (typeof vendorExtensions['x-stoplight'] === 'undefined') { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Flex h="full" alignItems="center"> | ||
<strong>{JSON.stringify(vendorExtensions['x-stoplight'], null, 2)}</strong> | ||
</Flex> | ||
); | ||
}, | ||
}; |
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
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,26 @@ | ||
import { SchemaFragment } from '@stoplight/json-schema-tree'; | ||
|
||
export type VendorExtensionsList = { | ||
[keyof: string]: unknown; | ||
}; | ||
|
||
export type VendorExtensionsResult = [number, VendorExtensionsList]; | ||
|
||
/** | ||
* Extract all vendor extensions or properties prefix with 'x-' from the schema definition | ||
* @param fragment The fragment to extract the vendor extensions from | ||
* @returns VendorExtensionsResult | ||
*/ | ||
export function extractVendorExtensions(fragment: SchemaFragment | boolean): VendorExtensionsResult { | ||
if (typeof fragment === 'boolean') { | ||
return [0, {}]; | ||
} | ||
|
||
const extensionKeys = Object.keys(fragment).filter(key => key.startsWith('x-')); | ||
let vendorExtensions = {}; | ||
extensionKeys.forEach(key => { | ||
vendorExtensions[key] = fragment[key]; | ||
}); | ||
|
||
return [extensionKeys.length, vendorExtensions]; | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './extractVendorExtensions'; | ||
export * from './printName'; |