-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Viz Builder] State validation before dispatching and loading (#2351)
* State validation before dispatching Validate style and visualization state schema before dispatching state updates when loading wizard. Show error via toast. Signed-off-by: abbyhu2000 <[email protected]> * Fixes and add unit tests Signed-off-by: abbyhu2000 <[email protected]> * Add PR to changelog.md Signed-off-by: abbyhu2000 <[email protected]> Signed-off-by: abbyhu2000 <[email protected]> (cherry picked from commit 0279588)
- Loading branch information
1 parent
dd3ade1
commit c2e420d
Showing
7 changed files
with
117 additions
and
8 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
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 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"styleState": { | ||
"type": "object" | ||
}, | ||
"visualizationState": { | ||
"type": "object", | ||
"properties": { | ||
"activeVisualization": { | ||
"type": "object", | ||
"properties": { | ||
"name": { "type": "string" }, | ||
"aggConfigParams": { "type": "array" } | ||
}, | ||
"required": ["name", "aggConfigParams"], | ||
"additionalProperties": false | ||
}, | ||
"indexPattern": { "type": "string" }, | ||
"searchField": { "type": "string" } | ||
}, | ||
"required": ["searchField"], | ||
"additionalProperties": false | ||
} | ||
}, | ||
"required": ["styleState", "visualizationState"], | ||
"additionalProperties": false | ||
} |
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
43 changes: 43 additions & 0 deletions
43
src/plugins/wizard/public/application/utils/wizard_state_validation.test.ts
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,43 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { validateWizardState } from './wizard_state_validation'; | ||
|
||
describe('wizard state validation', () => { | ||
const validStyleState = { | ||
addLegend: true, | ||
addTooltip: true, | ||
legendPosition: '', | ||
type: 'metric', | ||
}; | ||
const validVisualizationState = { | ||
activeVisualization: { | ||
name: 'metric', | ||
aggConfigParams: [], | ||
}, | ||
indexPattern: '', | ||
searchField: '', | ||
}; | ||
describe('correct return when validation suceeds', () => { | ||
test('with correct wizard state', () => { | ||
const validationResult = validateWizardState({ | ||
styleState: validStyleState, | ||
visualizationState: validVisualizationState, | ||
}); | ||
expect(validationResult.valid).toBeTruthy(); | ||
expect(validationResult.errors).toBeNull(); | ||
}); | ||
}); | ||
describe('correct return with errors when validation fails', () => { | ||
test('with non object type styleStyle', () => { | ||
const validationResult = validateWizardState({ | ||
styleState: [], | ||
visualizationState: validVisualizationState, | ||
}); | ||
expect(validationResult.valid).toBeFalsy(); | ||
expect(validationResult.errors).toBeDefined(); | ||
}); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
src/plugins/wizard/public/application/utils/wizard_state_validation.ts
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,19 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import Ajv from 'ajv'; | ||
import wizardStateSchema from './schema.json'; | ||
|
||
const ajv = new Ajv(); | ||
const validateState = ajv.compile(wizardStateSchema); | ||
|
||
export const validateWizardState = (wizardState) => { | ||
const isWizardStateValid = validateState(wizardState); | ||
|
||
return { | ||
valid: isWizardStateValid, | ||
errors: validateState.errors, | ||
}; | ||
}; |
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