-
Notifications
You must be signed in to change notification settings - Fork 936
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#2449) * State validation before dispatching * Validate style and visualization state schema before dispatching state updates when loading wizard. Show error via toast. * Fixes and add unit tests * Add PR to changelog.md Signed-off-by: abbyhu2000 <[email protected]> (cherry picked from commit 0279588) Co-authored-by: Qingyang(Abby) Hu <[email protected]>
- Loading branch information
1 parent
453428d
commit a68302b
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