-
Notifications
You must be signed in to change notification settings - Fork 373
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
Provide additional tester context #1975
Merged
sdirix
merged 2 commits into
eclipsesource:master
from
lucas-koehler:1970-more-tester-context
Jul 15, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -16,15 +16,21 @@ Therefore JSON Forms was not able to properly run the testers on schemas contain | |
The workaround for this was to resolve the JSON Schema by hand before handing it over to JSON Forms. | ||
Only the React renderers did this automatically but we removed this functionality, see the next section for more information. | ||
|
||
We now added an additional parameter to the testers, the `rootSchema`. | ||
We now added an additional parameter to the testers, the new `TesterContext`. | ||
|
||
```ts | ||
type Tester = (uischema: UISchemaElement, schema: JsonSchema, rootSchema: JsonSchema) => boolean; | ||
type RankedTester = (uischema: UISchemaElement, schema: JsonSchema, rootSchema: JsonSchema) => number; | ||
interface TesterContext { | ||
rootSchema: JsonSchema; | ||
config: any; | ||
} | ||
|
||
type Tester = (uischema: UISchemaElement, schema: JsonSchema, context: TesterContext) => boolean; | ||
type RankedTester = (uischema: UISchemaElement, schema: JsonSchema, context: TesterContext) => number; | ||
``` | ||
|
||
This allows the testers to resolve any `$ref` they might encounter in their handed over `schema`. | ||
This allows the testers to resolve any `$ref` they might encounter in their handed over `schema` by using the context's `rootSchema`. | ||
Therefore the manual resolving of JSON Schemas before handing them over to JSON Forms does not need to be performed in those cases. | ||
In addition, testers can now access the global `config` to consider default UI Schema options. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
||
|
||
### Removal of JSON Schema $Ref Parser | ||
|
||
|
@@ -114,6 +120,10 @@ The utility function `fromScopable` was renamed to `fromScoped` accordingly. | |
|
||
Date Picker in Angular Material will use the global configuration of your Angular Material application. | ||
|
||
### React prop mapping functions | ||
|
||
Renamed `ctxToJsonFormsDispatchProps` to `ctxToJsonFormsRendererProps` in order to better reflect the function's purpose. | ||
|
||
## Migrating to JSON Forms 2.5 | ||
|
||
### JsonForms Component for Angular | ||
|
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,28 @@ | ||
/* | ||
The MIT License | ||
|
||
Copyright (c) 2022 EclipseSource | ||
https://github.com/eclipsesource/jsonforms | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
import { JsonSchema, TesterContext } from '@jsonforms/core'; | ||
|
||
export const createTesterContext = | ||
(rootSchema: JsonSchema, config?: any): TesterContext => ({ rootSchema, config }); |
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 |
---|---|---|
|
@@ -49,17 +49,27 @@ export const NOT_APPLICABLE = -1; | |
* A tester is a function that receives an UI schema and a JSON schema and returns a boolean. | ||
* The rootSchema is handed over as context. Can be used to resolve references. | ||
*/ | ||
export type Tester = (uischema: UISchemaElement, schema: JsonSchema, rootSchema: JsonSchema) => boolean; | ||
export type Tester = (uischema: UISchemaElement, schema: JsonSchema, context: TesterContext) => boolean; | ||
|
||
/** | ||
* A ranked tester associates a tester with a number. | ||
*/ | ||
export type RankedTester = ( | ||
uischema: UISchemaElement, | ||
schema: JsonSchema, | ||
rootSchema: JsonSchema | ||
context: TesterContext | ||
) => number; | ||
|
||
/** | ||
* Additional context given to a tester in addition to UISchema and JsonSchema. | ||
*/ | ||
export interface TesterContext { | ||
/** The root JsonSchema of the form. Can be used to resolve references. */ | ||
rootSchema: JsonSchema; | ||
/** The global configuration object given to JsonForms. Can be used to derive default UISchema options. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
config: any; | ||
} | ||
|
||
export const isControl = (uischema: any): uischema is ControlElement => | ||
!isEmpty(uischema) && uischema.scope !== undefined; | ||
|
||
|
@@ -75,7 +85,7 @@ export const isControl = (uischema: any): uischema is ControlElement => | |
*/ | ||
export const schemaMatches = ( | ||
predicate: (schema: JsonSchema, rootSchema: JsonSchema) => boolean | ||
): Tester => (uischema: UISchemaElement, schema: JsonSchema, rootSchema: JsonSchema): boolean => { | ||
): Tester => (uischema: UISchemaElement, schema: JsonSchema, context: TesterContext): boolean => { | ||
if (isEmpty(uischema) || !isControl(uischema)) { | ||
return false; | ||
} | ||
|
@@ -88,34 +98,34 @@ export const schemaMatches = ( | |
} | ||
let currentDataSchema = schema; | ||
if (hasType(schema, 'object')) { | ||
currentDataSchema = resolveSchema(schema, schemaPath, rootSchema); | ||
currentDataSchema = resolveSchema(schema, schemaPath, context?.rootSchema); | ||
} | ||
if (currentDataSchema === undefined) { | ||
return false; | ||
} | ||
|
||
return predicate(currentDataSchema, rootSchema); | ||
return predicate(currentDataSchema, context?.rootSchema); | ||
}; | ||
|
||
export const schemaSubPathMatches = ( | ||
subPath: string, | ||
predicate: (schema: JsonSchema, rootSchema: JsonSchema) => boolean | ||
): Tester => (uischema: UISchemaElement, schema: JsonSchema, rootSchema: JsonSchema): boolean => { | ||
): Tester => (uischema: UISchemaElement, schema: JsonSchema, context: TesterContext): boolean => { | ||
if (isEmpty(uischema) || !isControl(uischema)) { | ||
return false; | ||
} | ||
const schemaPath = uischema.scope; | ||
let currentDataSchema: JsonSchema = schema; | ||
if (hasType(schema, 'object')) { | ||
currentDataSchema = resolveSchema(schema, schemaPath, rootSchema); | ||
currentDataSchema = resolveSchema(schema, schemaPath, context?.rootSchema); | ||
} | ||
currentDataSchema = get(currentDataSchema, subPath); | ||
|
||
if (currentDataSchema === undefined) { | ||
return false; | ||
} | ||
|
||
return predicate(currentDataSchema, rootSchema); | ||
return predicate(currentDataSchema, context?.rootSchema); | ||
}; | ||
|
||
/** | ||
|
@@ -218,8 +228,8 @@ export const scopeEndIs = (expected: string): Tester => ( | |
export const and = (...testers: Tester[]): Tester => ( | ||
uischema: UISchemaElement, | ||
schema: JsonSchema, | ||
rootSchema: JsonSchema | ||
) => testers.reduce((acc, tester) => acc && tester(uischema, schema, rootSchema), true); | ||
context: TesterContext | ||
) => testers.reduce((acc, tester) => acc && tester(uischema, schema, context), true); | ||
|
||
/** | ||
* A tester that allow composing other testers by || them. | ||
|
@@ -229,8 +239,8 @@ export const and = (...testers: Tester[]): Tester => ( | |
export const or = (...testers: Tester[]): Tester => ( | ||
uischema: UISchemaElement, | ||
schema: JsonSchema, | ||
rootSchema: JsonSchema | ||
) => testers.reduce((acc, tester) => acc || tester(uischema, schema, rootSchema), false); | ||
context: TesterContext | ||
) => testers.reduce((acc, tester) => acc || tester(uischema, schema, context), false); | ||
/** | ||
* Create a ranked tester that will associate a number with a given tester, if the | ||
* latter returns true. | ||
|
@@ -241,9 +251,9 @@ export const or = (...testers: Tester[]): Tester => ( | |
export const rankWith = (rank: number, tester: Tester) => ( | ||
uischema: UISchemaElement, | ||
schema: JsonSchema, | ||
rootSchema: JsonSchema | ||
context: TesterContext | ||
): number => { | ||
if (tester(uischema, schema, rootSchema)) { | ||
if (tester(uischema, schema, context)) { | ||
return rank; | ||
} | ||
|
||
|
@@ -253,9 +263,9 @@ export const rankWith = (rank: number, tester: Tester) => ( | |
export const withIncreasedRank = (by: number, rankedTester: RankedTester) => ( | ||
uischema: UISchemaElement, | ||
schema: JsonSchema, | ||
rootSchema: JsonSchema | ||
context: TesterContext | ||
): number => { | ||
const rank = rankedTester(uischema, schema, rootSchema); | ||
const rank = rankedTester(uischema, schema, context); | ||
if (rank === NOT_APPLICABLE) { | ||
return NOT_APPLICABLE; | ||
} | ||
|
@@ -438,13 +448,13 @@ const traverse = ( | |
export const isObjectArrayWithNesting = ( | ||
uischema: UISchemaElement, | ||
schema: JsonSchema, | ||
rootSchema: JsonSchema | ||
context: TesterContext | ||
): boolean => { | ||
if (!uiTypeIs('Control')(uischema, schema, rootSchema)) { | ||
if (!uiTypeIs('Control')(uischema, schema, context)) { | ||
return false; | ||
} | ||
const schemaPath = (uischema as ControlElement).scope; | ||
const resolvedSchema = resolveSchema(schema, schemaPath, rootSchema ?? schema); | ||
const resolvedSchema = resolveSchema(schema, schemaPath, context?.rootSchema ?? schema); | ||
let objectDepth = 0; | ||
if (resolvedSchema !== undefined && resolvedSchema.items !== undefined) { | ||
// check if nested arrays | ||
|
@@ -466,7 +476,7 @@ export const isObjectArrayWithNesting = ( | |
return true; | ||
} | ||
return false; | ||
}, rootSchema) | ||
}, context?.rootSchema) | ||
) { | ||
return true; | ||
} | ||
|
@@ -532,7 +542,7 @@ export const isRangeControl = and( | |
|
||
/** | ||
* Tests whether the given UI schema is of type Control, if the schema | ||
* is of type string and has option format | ||
* is of type integer and has option format | ||
* @type {Tester} | ||
*/ | ||
export const isNumberFormatControl = and( | ||
|
@@ -566,6 +576,6 @@ export const categorizationHasCategory = (uischema: UISchemaElement) => | |
export const not = (tester: Tester): Tester => ( | ||
uischema: UISchemaElement, | ||
schema: JsonSchema, | ||
rootSchema: JsonSchema | ||
context: TesterContext | ||
|
||
) => !tester(uischema, schema, rootSchema); | ||
) => !tester(uischema, schema, context); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are also other use cases so I would just add that the
config
is now available. Especially as we don't use the config in this PR.