-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[SOR] Adds support for validation schema with models #158527
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
Merged
TinaHeiligers
merged 20 commits into
elastic:main
from
TinaHeiligers:kbn-152994-SO-model-version-support-schema
Jun 5, 2023
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9390d95
Adds support for model version schema
TinaHeiligers 4361dc6
Adds tests for ValidationHelper.validateObjectForCreate
TinaHeiligers 1476bc1
Adds code comments about dependencies and assumptions
TinaHeiligers f69c19a
updates code comments
TinaHeiligers ca9b8ac
improvements
TinaHeiligers c59a391
Merge branch 'main' into kbn-152994-SO-model-version-support-schema
kibanamachine 69f80a8
Assert validation against virtual model version
TinaHeiligers bbedd7d
Assert validation against virtual model version
TinaHeiligers 3c405e8
Merge branch 'main' into kbn-152994-SO-model-version-support-schema
kibanamachine 8f4ca19
Add comments
TinaHeiligers 0b0b5ae
adds unit tests for new behavior
TinaHeiligers ef3e6d6
Remove option to specify validation version from SOR create and bulkC…
TinaHeiligers 93f3e54
Don't check version against default
TinaHeiligers fdde775
comments cleanup
TinaHeiligers 429a70c
Improve type safety check
TinaHeiligers 463b981
PR review comments
TinaHeiligers 3c25cb2
add test for other conditionals
TinaHeiligers ee9d035
Merge branch 'main' into kbn-152994-SO-model-version-support-schema
kibanamachine c38b90e
cleanup
TinaHeiligers 8f4ff4b
Merge branch 'main' into kbn-152994-SO-model-version-support-schema
kibanamachine 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
95 changes: 95 additions & 0 deletions
95
...ed-objects/core-saved-objects-api-server-internal/src/lib/apis/helpers/validation.test.ts
This file contains hidden or 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,95 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { loggerMock, type MockedLogger } from '@kbn/logging-mocks'; | ||
| import { SavedObjectsType } from '@kbn/core-saved-objects-server'; | ||
| import { type SavedObjectSanitizedDoc } from '@kbn/core-saved-objects-server'; | ||
| import { ValidationHelper } from './validation'; | ||
| import { typedef, typedef1, typedef2 } from './validation_fixtures'; | ||
| import { SavedObjectTypeRegistry } from '@kbn/core-saved-objects-base-server-internal'; | ||
|
|
||
| const defaultVersion = '8.10.0'; | ||
| const modelVirtualVersion = '10.1.0'; | ||
| const typeA = 'my-typeA'; | ||
| const typeB = 'my-typeB'; | ||
| const typeC = 'my-typeC'; | ||
|
|
||
| describe('Saved Objects type validation helper', () => { | ||
| let helper: ValidationHelper; | ||
| let logger: MockedLogger; | ||
| let typeRegistry: SavedObjectTypeRegistry; | ||
|
|
||
| const createMockObject = ( | ||
| type: string, | ||
| attr: Partial<SavedObjectSanitizedDoc> | ||
| ): SavedObjectSanitizedDoc => ({ | ||
| type, | ||
| id: 'test-id', | ||
| references: [], | ||
| attributes: {}, | ||
| ...attr, | ||
| }); | ||
| const registerType = (name: string, parts: Partial<SavedObjectsType>) => { | ||
| typeRegistry.registerType({ | ||
| name, | ||
| hidden: false, | ||
| namespaceType: 'single', | ||
| mappings: { properties: {} }, | ||
| ...parts, | ||
| }); | ||
| }; | ||
| beforeEach(() => { | ||
| logger = loggerMock.create(); | ||
| typeRegistry = new SavedObjectTypeRegistry(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| describe('validation helper', () => { | ||
| beforeEach(() => { | ||
| registerType(typeA, typedef); | ||
| registerType(typeB, typedef1); | ||
| registerType(typeC, typedef2); | ||
| }); | ||
|
|
||
| it('should validate objects against stack versions', () => { | ||
| helper = new ValidationHelper({ | ||
| logger, | ||
| registry: typeRegistry, | ||
| kibanaVersion: defaultVersion, | ||
| }); | ||
| const data = createMockObject(typeA, { attributes: { foo: 'hi', count: 1 } }); | ||
| expect(() => helper.validateObjectForCreate(typeA, data)).not.toThrowError(); | ||
| }); | ||
|
|
||
| it('should validate objects against model versions', () => { | ||
| helper = new ValidationHelper({ | ||
| logger, | ||
| registry: typeRegistry, | ||
| kibanaVersion: modelVirtualVersion, | ||
| }); | ||
| const data = createMockObject(typeB, { attributes: { foo: 'hi', count: 1 } }); | ||
| expect(() => helper.validateObjectForCreate(typeB, data)).not.toThrowError(); | ||
| }); | ||
|
|
||
| it('should fail validation against invalid objects when version requested does not support a field', () => { | ||
| helper = new ValidationHelper({ | ||
| logger, | ||
| registry: typeRegistry, | ||
| kibanaVersion: defaultVersion, | ||
| }); | ||
| const validationError = new Error( | ||
| '[attributes.count]: definition for this key is missing: Bad Request' | ||
| ); | ||
| const data = createMockObject(typeC, { attributes: { foo: 'hi', count: 1 } }); | ||
| expect(() => helper.validateObjectForCreate(typeC, data)).toThrowError(validationError); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
123 changes: 123 additions & 0 deletions
123
...bjects/core-saved-objects-api-server-internal/src/lib/apis/helpers/validation_fixtures.ts
This file contains hidden or 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,123 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { schema } from '@kbn/config-schema'; | ||
| import { SavedObjectsType } from '@kbn/core-saved-objects-server'; | ||
|
|
||
| export const typedef: Partial<SavedObjectsType> = { | ||
| mappings: { | ||
| properties: { | ||
| foo: { | ||
| type: 'keyword', | ||
| }, | ||
| count: { | ||
| type: 'integer', | ||
| }, | ||
| }, | ||
| }, | ||
| schemas: { | ||
| '8.9.0': schema.object({ | ||
| foo: schema.string(), | ||
| }), | ||
| '8.10.0': schema.object({ | ||
| foo: schema.string(), | ||
| count: schema.number(), | ||
| }), | ||
| }, | ||
| switchToModelVersionAt: '8.10.0', | ||
| }; | ||
|
|
||
| export const typedef1: Partial<SavedObjectsType> = { | ||
| mappings: { | ||
| properties: { | ||
| foo: { | ||
| type: 'keyword', | ||
| }, | ||
| count: { | ||
| type: 'integer', | ||
| }, | ||
| }, | ||
| }, | ||
| schemas: { | ||
| '8.9.0': schema.object({ | ||
| foo: schema.string(), | ||
| }), | ||
| '8.10.0': schema.object({ | ||
| foo: schema.string(), | ||
| count: schema.number(), | ||
| }), | ||
| }, | ||
| switchToModelVersionAt: '8.10.0', | ||
| modelVersions: { | ||
| '1': { | ||
| changes: [ | ||
| { | ||
| type: 'mappings_addition', | ||
| addedMappings: { | ||
| count: { | ||
| properties: { | ||
| count: { | ||
| type: 'integer', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| schemas: { | ||
| create: schema.object({ | ||
| foo: schema.string(), | ||
| count: schema.number(), | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const typedef2: Partial<SavedObjectsType> = { | ||
| mappings: { | ||
| properties: { | ||
| foo: { | ||
| type: 'keyword', | ||
| }, | ||
| count: { | ||
| type: 'integer', | ||
| }, | ||
| }, | ||
| }, | ||
| schemas: { | ||
| '8.9.0': schema.object({ | ||
| foo: schema.string(), | ||
| }), | ||
| }, | ||
| switchToModelVersionAt: '8.10.0', | ||
| modelVersions: { | ||
| '1': { | ||
| changes: [ | ||
| { | ||
| type: 'mappings_addition', | ||
| addedMappings: { | ||
| count: { | ||
| properties: { | ||
| count: { | ||
| type: 'integer', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| schemas: { | ||
| create: schema.object({ | ||
| foo: schema.string(), | ||
| count: schema.number(), | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.