Skip to content

Commit a28a825

Browse files
remove 'likeAllOf' support and expose helper functions for custom call-backs
Merge pull request #4
2 parents 1ae6fcf + 3338070 commit a28a825

27 files changed

+501
-1419
lines changed

CHANGELOG.MD

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [2.1.0] - 2019-05-01
10+
### Added
11+
- exporting helper function `getFieldValueArrayFromSchemaGroup()`
12+
- exporting helper function `getCommonFieldValuesFromSchemaGroup()`
13+
- exporting helper function `getMaximumFieldValueFromSchemaGroup()`
14+
- exporting helper function `getMinimumFieldValueFromSchemaGroup()`
15+
16+
### Changed
17+
- not including `anyOf` or `oneOf` in `<Inspector>` prop `parserConfig` no longer hides the respective schema parts (always including everything)
18+
19+
### Removed
20+
- "type" property in `<Inspector>` prop `parserConfig.anyOf`/`parserConfig.oneOf` (i.e. always "asAdditionalColumn" behaviour from now on)
21+
- undocumented export of helper function `getFieldValueFromSchemaGroup()`, one of the four new helper functions should be used instead
22+
23+
## [2.0.1] - 2019-04-24
24+
### Changed
25+
- values displayed in (default) Details Form now respect the validation logic behind `allOf` by not simply listing all values for the same field, but rather semantically combining/merging them
26+
- `"type"` contains only those common/intersecting values if it appears multiple times
27+
- `"minimum"`/`"minLength"`/`"minItems"` show the maximum value if there are multiple occurrences of the respective field
28+
- `"maximum"`/`"maxLength"`/`"maxItems"` show the minimum value if there are multiple occurrences of the respective field
29+
- `"const"`/`"enum"` are used interchangeably for either a single or multiple allowed values
30+
31+
## [2.0.0] - 2019-04-12
32+
### Added
33+
- general support for `anyOf` and `oneOf`
34+
- `anyOf`/`oneOf` may be included or excluded depending on the respective fields with the same name in the `parserConfig` prop
35+
- can determine the way of inclusion for `anyOf`/`oneOf` via the respective `type` field of value `likeAllOf` or `asAdditionalColumn` in the `parserConfig` prop
36+
- introduction of explicit (configurable/customisable) column for accessing an array's items and/or other properties
37+
38+
### Changed
39+
- named export of `<Inspector>` component (instead of default export)
40+
41+
### Removed
42+
- default export of main component
43+
44+
## [1.2.1] - 2019-03-18
45+
### Changed
46+
- configuration of npm bundle; thereby reducing size of release package
47+
48+
## [1.2.0] - 2019-03-09
49+
### Added
50+
- allow to mutate the name of a selected item when including it in breadcrumbs (e.g. to sanitise it)
51+
- allow skipping a breadcrumb (e.g. omitting the root schema selection)
52+
- include breadcrumbs as array parameter in `onSelect` call-back
53+
54+
## [1.1.0] - 2019-03-08
55+
### Added
56+
- offering free-text search input in the top right corner
57+
- allow defining custom search function (based on a single raw schema – handling of sub-schemas is transparent)
58+
- allow alternative configuration of field names to search in, e.g. `["title", "description"]`
59+
60+
## [1.0.0] - 2019-03-03

README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Or try it out and [![Edit on CodeSandbox][codesandbox-image]][codesandbox-url]
2727
npm i react-jsonschema-inspector
2828
```
2929

30-
### React Component Props
30+
### React Component Props of `<Inspector>`
3131

3232
| Prop | Description |
3333
| --- | --- |
@@ -37,19 +37,17 @@ npm i react-jsonschema-inspector
3737
| `onSelect` | Function: call-back being invoked after the selection changed. Receives two parameters: (1) the selection - as per the `defaultSelectedItems`, (2) an object containing the "columnData" - the full render information for all visible columns |
3838
| `buildArrayProperties` | Function: accepting a `JsonSchema` instance representing an array's declared type of items and returning an object listing the available properties to offer with either `JsonSchema` or raw JSON Schemas as values. The default, providing access to the array's items, is: `arrayItemSchema => ({ "[0]": arrayItemSchema })` |
3939
| `parserConfig` | Object: enabling the inclusion/exclusion of optional parts of a JSON Schema – both for the inclusion of properties and their attributes as well as in the search. |
40-
| `parserConfig.anyOf` | Object: enabling the inclusion/exclusion of JSON Schema parts wrapped in `anyOf`. |
41-
| `parserConfig.anyOf.type` | String: can be `"likeAllOf"` or `"asAdditionalColumn"` (the latter being the default if no `parserConfig` is provided). |
42-
| `parserConfig.anyOf.groupTitle` | String: alternative title to show in option selection column (only relevant if `type: "asAdditionalColumn"`) – defaults to `"any of"` |
43-
| `parserConfig.anyOf.optionNameForIndex` | Function: providing the name/label to show for a single option (only relevant if `type: "asAdditionalColumn"`) – defaults to ``(optionIndexes) => `Option ${optionIndexes.map(index => index + 1).join("-")}` ``, resulting in e.g. "Option 1", "Option 2-1", "Option 3" |
44-
| `parserConfig.oneOf` | Object: enabling the inclusion/exclusion of JSON Schema parts wrapped in `oneOf`. |
45-
| `parserConfig.oneOf.type` | String: can be `"likeAllOf"` or `"asAdditionalColumn"` (the latter being the default if no `parserConfig` is provided). |
46-
| `parserConfig.oneOf.groupTitle` | String: alternative title to show in option selection column (only relevant if `type: "asAdditionalColumn"`) – defaults to `"one of"` |
47-
| `parserConfig.oneOf.optionNameForIndex` | Function: providing the name/label to show for a single option (only relevant if `type: "asAdditionalColumn"`) – defaults to ``(optionIndexes) => `Option ${optionIndexes.map(index => index + 1).join("-")}` ``, resulting in e.g. "Option 1", "Option 2-1", "Option 3" |
40+
| `parserConfig.anyOf` | Object: specifying details of the inclusion of JSON Schema parts wrapped in `anyOf`. |
41+
| `parserConfig.anyOf.groupTitle` | String: alternative title to show in option selection column – defaults to `"any of"` |
42+
| `parserConfig.anyOf.optionNameForIndex` | Function: providing the name/label to show for a single option – defaults to ``(optionIndexes) => `Option ${optionIndexes.map(index => index + 1).join("-")}` ``, resulting in e.g. "Option 1", "Option 2-1", "Option 3" |
43+
| `parserConfig.oneOf` | Object: specifying details of the inclusion of JSON Schema parts wrapped in `oneOf`. |
44+
| `parserConfig.oneOf.groupTitle` | String: alternative title to show in option selection column – defaults to `"one of"` |
45+
| `parserConfig.oneOf.optionNameForIndex` | Function: providing the name/label to show for a single option – defaults to ``(optionIndexes) => `Option ${optionIndexes.map(index => index + 1).join("-")}` ``, resulting in e.g. "Option 1", "Option 2-1", "Option 3" |
4846
| `breadcrumbs` | Object: enabling the definition of options for the breadcrumbs feature in the footer (can be disabled by setting to `null`) |
4947
| `breadcrumbs.prefix` | String: to be shown in front of the root selection (e.g. "//" or "./") – defaults to `""` |
5048
| `breadcrumbs.separator` | String: to be shown in front of any non-root selection (e.g. "/") – defaults to `"."` |
5149
| `breadcrumbs.skipSeparator` | Function: expecting a `JsonSchema` as input and should return an object containing `JsonSchema` or raw JSON Schemas as values – defaults to excluding `"[0]"` |
52-
| `breadcrumbs.mutateName` | Function: expecting two inputs: (1) the selected item's name, (2) the full information for the respective column and (3) the index of the respective column; a column's breadcrumb can be skipped by returning `null` |
50+
| `breadcrumbs.mutateName` | Function: expecting the following inputs: (1) the selected item's name, (2) the full information for the respective column and (3) the index of the respective column; a column's breadcrumb can be skipped by returning `null` |
5351
| `breadcrumbs.preventNavigation` | Boolean: set to `true` in order to turn-off the default behaviour of discarding any following selections when double-clicking on a breadcrumbs item |
5452
| `searchOptions` | Object: enabling the definition of options for the search/filter feature in the header (is disabled by default) – either `searchOptions.fields` or `searchOptions.filterBy` needs to be specified to enable it. the component itself will take care of looking-up sub-schemas (e.g. in `properties`) and also respects `$ref`-erences and has no problem with circular references. |
5553
| `searchOptions.fields`| Array of strings: each referring to the name of a text field in a JSON Schema (e.g. `["title", "description"]`) in which to search/filter – this applies a case-insensitive contains() check on each of the given fields |
@@ -58,9 +56,26 @@ npm i react-jsonschema-inspector
5856
| `searchOptions.debounceWait` | Number indicating the delay in milliseconds between the last change to the search term being entered and it actually being applied. This defaults to `200` but may be increased when used with exceptionally large schemas and you experience performance issues. Please refer to the documentation on [`lodash.debounce`](https://lodash.com/docs/4.17.11#debounce). |
5957
| `searchOptions.debounceMaxWait` | Number indicating the maximum delay in milliseconds after the search term was changed. This defaults to `500`. Please refer to the documentation on [`lodash.debounce`](https://lodash.com/docs/4.17.11#debounce). |
6058
| `renderItemContent` | Function: custom render function for name of single property/sub-schema in a column. Receives one parameter: object with the following properties: "name", "hasNestedItems", "selected", "schemaGroup" |
61-
| `renderSelectionDetails` | Function: custom render function for the "Details" block on the right for the single property/sub-schema being selected. Receives one parameter: object with the following properties: "itemSchemaGroup", "columnData", "selectionColumnIndex" |
59+
| `renderSelectionDetails` | Function: custom render function for the "Details" block on the right for the single property/sub-schema being selected. Receives one parameter: object with the following properties: "itemSchemaGroup", "columnData", "selectionColumnIndex", "optionIndexes" |
6260
| `renderEmptyDetails` | Function: custom render function for the "Details" block on the right if nothing is selected yet. Receives one parameter, which is an object with the "rootColumnSchemas" property, which holds the array of top-level schemas (as derived from the `schemas` prop and augmented by any given `referenceSchemas`)
6361

62+
### Additional Helper Functions
63+
64+
Besides the main `<Inspector>` component, there are additional named helper functions being provided in the scope of this library:
65+
- `getFieldValueArrayFromSchemaGroup()` – listing all values of the targeted field in an array (skipping `undefined` and `null` values)
66+
- `getCommonFieldValuesFromSchemaGroup()` – listing only those values of the targeted field in an array that are included in all occurrences of the field not being `undefined` or `null`
67+
- `getMinimumFieldValueFromSchemaGroup()` – expecting numeric values in the targeted field and returning the single lowest value (ignoring `undefined` and `null` values)
68+
- `getMaximumFieldValueFromSchemaGroup()` – expecting numeric values in the targeted field and returning the single highest value (ignoring `undefined` and `null` values)
69+
70+
All four of these are intended to be used within props enabling the customisation of an `<Inspector>` instance, e.g. in `onSelect`, `buildArrayProperties`, `breadcrumbs.mutateName`, `renderItemContent`, `renderSelectionDetails`, `renderEmptyDetails`.
71+
All four helper functions expect the same input parameters:
72+
1. `schemaGroup` – a group object representing a single schema with all its parts, as provided to the various call-back functions mentioned above
73+
2. `fieldName` – textual name of the targeted field in the schema (group), e.g. `"title"`, `"maximum"`, `"minLength"`
74+
3. `defaultValue` – value to return if there is no value for the targeted field; or as initial/base value for the `Array.reduce()` being performed for the encountered values
75+
4. `optionIndexes` – only provided if the `schemaGroup` contains optional parts (i.e. `anyOf`/`oneOf`); used to identify the particular optional path within the `anyOf`/`oneOf` part(s) – this is also provided in one way or another in those call-back functions listed above
76+
77+
As output, the respective helper functions either return a single value or – in case of multiple values – an array.
78+
6479

6580
## Compatibility
6681

@@ -83,8 +98,8 @@ It is also backwards-compatible with Drafts 4 and 6.
8398
| `items`| Partially | used to look-up `properties` of single kind of items in an array; however if `items` is an array of multiple sub-schemas they are being *ignored* |
8499
| `additionalItems`| Yes | used to look-up `properties` of kind of items in an array if `items` is not present or defined as an array (which is not supported itself), otherwise `additionalItems` are being *ignored* |
85100
| `allOf` | Yes | used to combine sub-schemas transparently |
86-
| `anyOf` | Yes | used to combine sub-schemas (transparently via `parserConfig.anyOf.type === "likeAllOf"` or explicitly via `parserConfig.anyOf.type === "asAdditionalColumn"`) |
87-
| `oneOf` | Yes | used to combine sub-schemas (transparently via `parserConfig.oneOf.type === "likeAllOf"` or explicitly via `parserConfig.oneOf.type === "asAdditionalColumn"`) |
101+
| `anyOf` | Yes | used to combine sub-schemas |
102+
| `oneOf` | Yes | used to combine sub-schemas |
88103
| `not` | - | *ignored* |
89104
| `contains` | - | *ignored* |
90105
| `dependencies` | - | *ignored* |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-jsonschema-inspector",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "View component for traversing/searching in a JSON Schema",
55
"homepage": "https://CarstenWickner.github.io/react-jsonschema-inspector",
66
"author": "Carsten Wickner",

src/component/Inspector.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Inspector extends Component {
130130
* @return {Object} return wrapper object for the column data (for the sake of future extensibility)
131131
* @return {Array.<Object>} return.columnData
132132
* @return {?Object.<String, JsonSchemaGroup>} return.columnData[].items named schemas to list in the respective column
133-
* @return {?Object} return.columnData[].options representation of a schema's hierarchy in case of optionals being included `"asAdditionalColumn"`
133+
* @return {?Object} return.columnData[].options representation of a schema's hierarchy in case of optionals being included
134134
* @return {?JsonSchemaGroup} return.columnData[].contextGroup the schema group containing the `options`
135135
* @return {?String} return.columnData[].selectedItem name of the currently selected item (may be null)
136136
* @return {?Boolean} return.columnData[].trailingSelection flag indicating whether this column's selection is the last
@@ -232,22 +232,20 @@ Inspector.propTypes = {
232232
*/
233233
defaultSelectedItems: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.number)])),
234234
/**
235-
* Options for the traversing/parsing of JSON schemas. Enabling the inclusion of optional part of a schema.
235+
* Options for the traversing/parsing of JSON schemas. Defining how optional parts of a schema should be represented.
236236
*/
237237
parserConfig: PropTypes.shape({
238238
/**
239-
* Setting indicating whether/how to include schema parts wrapped in "anyOf".
239+
* Setting indicating how to include schema parts wrapped in "anyOf".
240240
*/
241241
anyOf: PropTypes.shape({
242-
type: PropTypes.oneOf(["likeAllOf", "asAdditionalColumn"]).isRequired,
243242
groupTitle: PropTypes.string,
244243
optionNameForIndex: PropTypes.func
245244
}),
246245
/**
247-
* Setting indicating whether/how to include schema parts wrapped in "oneOf".
246+
* Setting indicating how to include schema parts wrapped in "oneOf".
248247
*/
249248
oneOf: PropTypes.shape({
250-
type: PropTypes.oneOf(["likeAllOf", "asAdditionalColumn"]).isRequired,
251249
groupTitle: PropTypes.string,
252250
optionNameForIndex: PropTypes.func
253251
})
@@ -326,10 +324,7 @@ Inspector.propTypes = {
326324
Inspector.defaultProps = {
327325
referenceSchemas: [],
328326
defaultSelectedItems: [],
329-
parserConfig: {
330-
oneOf: { type: "asAdditionalColumn" },
331-
anyOf: { type: "asAdditionalColumn" }
332-
},
327+
parserConfig: {},
333328
buildArrayProperties: undefined,
334329
breadcrumbs: {
335330
skipSeparator: fieldName => (fieldName === "[0]")

src/component/InspectorDetails.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ const InspectorDetails = ({
1111
const selectionColumnIndex = columnData.length - (lastColumnContainsSelection ? 1 : 2);
1212
const trailingSelectionColumn = selectionColumnIndex < 0 ? null : columnData[selectionColumnIndex];
1313
let itemSchemaGroup;
14+
let optionIndexes;
1415
if (trailingSelectionColumn) {
1516
itemSchemaGroup = trailingSelectionColumn.items
1617
? trailingSelectionColumn.items[trailingSelectionColumn.selectedItem]
1718
: trailingSelectionColumn.contextGroup;
19+
if (trailingSelectionColumn.options) {
20+
optionIndexes = trailingSelectionColumn.selectedItem;
21+
}
1822
}
1923
return (
2024
<div className="jsonschema-inspector-details">
2125
{itemSchemaGroup && renderSelectionDetails && renderSelectionDetails({
2226
itemSchemaGroup,
2327
selectionColumnIndex,
24-
columnData
28+
columnData,
29+
optionIndexes
2530
})}
2631
{itemSchemaGroup && !renderSelectionDetails && (
2732
<InspectorDetailsContent

0 commit comments

Comments
 (0)