-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into further-serve-refactors
- Loading branch information
Showing
67 changed files
with
2,177 additions
and
543 deletions.
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 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 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 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.