forked from opensearch-project/dashboards-observability
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor integration validation logic with a cleaner interface (opens…
…earch-project#943) * Refactor validation logic with a deeper interface Signed-off-by: Simeon Widdis <[email protected]> * Remove redundant test. This test is unneeded after 12c4bcf Signed-off-by: Simeon Widdis <[email protected]> * Add tests for new validators Signed-off-by: Simeon Widdis <[email protected]> * Make better failure mode for invalid objects Signed-off-by: Simeon Widdis <[email protected]> * Convert validator methods to use result types Signed-off-by: Simeon Widdis <[email protected]> --------- Signed-off-by: Simeon Widdis <[email protected]>
- Loading branch information
Showing
5 changed files
with
150 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { validateTemplate, validateInstance, ValidationResult } from '../validators'; | ||
|
||
const validTemplate: IntegrationTemplate = { | ||
name: 'test', | ||
version: '1.0.0', | ||
license: 'Apache-2.0', | ||
type: 'logs', | ||
components: [ | ||
{ | ||
name: 'logs', | ||
version: '1.0.0', | ||
}, | ||
], | ||
assets: {}, | ||
}; | ||
|
||
const validInstance: IntegrationInstance = { | ||
name: 'test', | ||
templateName: 'test', | ||
dataSource: 'test', | ||
creationDate: new Date(0).toISOString(), | ||
assets: [], | ||
}; | ||
|
||
describe('validateTemplate', () => { | ||
it('Returns a success value for a valid Integration Template', () => { | ||
const result: ValidationResult<IntegrationTemplate> = validateTemplate(validTemplate); | ||
expect(result.ok).toBe(true); | ||
expect((result as any).value).toBe(validTemplate); | ||
}); | ||
|
||
it('Returns a failure value if a template is missing a license', () => { | ||
const sample: any = structuredClone(validTemplate); | ||
sample.license = undefined; | ||
|
||
const result: ValidationResult<IntegrationTemplate> = validateTemplate(sample); | ||
|
||
expect(result.ok).toBe(false); | ||
expect((result as any).error).toBeInstanceOf(Error); | ||
}); | ||
|
||
it('Returns a failure if a template has an invalid type', () => { | ||
const sample: any = structuredClone(validTemplate); | ||
sample.components[0].name = 'not-logs'; | ||
|
||
const result: ValidationResult<IntegrationTemplate> = validateTemplate(sample); | ||
|
||
expect(result.ok).toBe(false); | ||
expect((result as any).error).toBeInstanceOf(Error); | ||
}); | ||
|
||
it("Doesn't crash if given a non-object", () => { | ||
// May happen in some user-provided JSON parsing scenarios. | ||
expect(validateTemplate([] as any).ok).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('validateInstance', () => { | ||
it('Returns true for a valid Integration Instance', () => { | ||
const result: ValidationResult<IntegrationInstance> = validateInstance(validInstance); | ||
expect(result.ok).toBe(true); | ||
expect((result as any).value).toBe(validInstance); | ||
}); | ||
|
||
it('Returns false if an instance is missing a template', () => { | ||
const sample: any = structuredClone(validInstance); | ||
sample.templateName = undefined; | ||
|
||
const result: ValidationResult<IntegrationInstance> = validateInstance(sample); | ||
|
||
expect(result.ok).toBe(false); | ||
expect((result as any).error).toBeInstanceOf(Error); | ||
}); | ||
|
||
it("Doesn't crash if given a non-object", () => { | ||
// May happen in some user-provided JSON parsing scenarios. | ||
expect(validateInstance([] as any).ok).toBe(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
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