-
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
[SOR] Adds support for validation schema with models #158527
Conversation
...saved-objects/core-saved-objects-api-server-internal/src/lib/apis/helpers/validation.test.ts
Outdated
Show resolved
Hide resolved
TinaHeiligers
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Draft early stages of adding tests
...-objects/core-saved-objects-migration-server-internal/src/document_migrator/model_version.ts
Outdated
Show resolved
Hide resolved
...saved-objects/core-saved-objects-api-server-internal/src/lib/apis/helpers/validation.test.ts
Outdated
Show resolved
Hide resolved
0ad3f3e to
ca9b8ac
Compare
|
Pinging @elastic/kibana-core (Team:Core) |
|
@elasticmachine merge upstream |
afharo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I just added a few nits.
I didn't approve because I'd like to rely on SO gurus for this 😇
...core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/helpers/validation.ts
Show resolved
Hide resolved
packages/core/saved-objects/core-saved-objects-base-server-internal/src/validation/validator.ts
Outdated
Show resolved
Hide resolved
|
We need a different way to target the validation version on Proposed solutions: don't pass Since we already know issues will arise and, since it's going to be unused anyway, I'm going with removing the option altogether. There's no point in carting something along "just in case". We can add it back if we need to. |
|
@elasticmachine merge upstream |
…reate. The version is set within the validator
TinaHeiligers
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initial draft self review: comments to myself and todos
.../core/saved-objects/core-saved-objects-base-server-internal/src/validation/validator.test.ts
Outdated
Show resolved
Hide resolved
.../core/saved-objects/core-saved-objects-base-server-internal/src/validation/validator.test.ts
Outdated
Show resolved
Hide resolved
.../core/saved-objects/core-saved-objects-base-server-internal/src/validation/validator.test.ts
Outdated
Show resolved
Hide resolved
packages/core/saved-objects/core-saved-objects-base-server-internal/src/validation/validator.ts
Outdated
Show resolved
Hide resolved
...core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/helpers/validation.ts
Show resolved
Hide resolved
TinaHeiligers
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self review 2
packages/core/saved-objects/core-saved-objects-base-server-internal/src/validation/validator.ts
Show resolved
Hide resolved
packages/core/saved-objects/core-saved-objects-base-server-internal/src/validation/validator.ts
Show resolved
Hide resolved
| document.migrationVersion?.[document.type] ?? | ||
| this.defaultVersion; | ||
| const schemaVersion = previousVersionWithSchema(this.orderedVersions, docVersion); | ||
| let usedVersion = version; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though the version option is (or should be) unused now, keeping the behavior allows us to reuse it if needed.
I'm open to removing the option if someone objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct me if I'm wrong, but in current state, the version option is effectively unused (given usedVersion is always replaced L52-L56), right?
I'm fine either way (keeping it or removing it), but if we do keep it, then we need the version parameter to be used if specified (it doesn't make sense to keep an option ignored by the implementation), and to adapt the call to validate to no longer pass the option, here:
Lines 93 to 95 in 3b6b7ad
| try { | |
| validator.validate(doc, this.kibanaVersion); | |
| } catch (error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're entirely right, version isn't used anymore and it doesn't make sense to keep dead code around. I will remove it.
pgayvallet
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, looking good to me!
Just asked a few questions for clarification before approval:
| */ | ||
|
|
||
| import type { ObjectType } from '@kbn/config-schema'; | ||
| import { SavedObjectsValidationSpec } from '../validation'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: import type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| document.migrationVersion?.[document.type] ?? | ||
| this.defaultVersion; | ||
| const schemaVersion = previousVersionWithSchema(this.orderedVersions, docVersion); | ||
| let usedVersion = version; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct me if I'm wrong, but in current state, the version option is effectively unused (given usedVersion is always replaced L52-L56), right?
I'm fine either way (keeping it or removing it), but if we do keep it, then we need the version parameter to be used if specified (it doesn't make sense to keep an option ignored by the implementation), and to adapt the call to validate to no longer pass the option, here:
Lines 93 to 95 in 3b6b7ad
| try { | |
| validator.validate(doc, this.kibanaVersion); | |
| } catch (error) { |
| const virtualVersion = modelVersionToVirtualVersion(key); | ||
| if (modelVersion.schemas?.create) { | ||
| combinedSchemas[virtualVersion] = modelVersion.schemas!.create!; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: modelVersionToVirtualVersion can be done within the if block to avoid calling it when not needed
if (modelVersion.schemas?.create) {
const virtualVersion = modelVersionToVirtualVersion(key);
combinedSchemas[virtualVersion] = modelVersion.schemas!.create!;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| it('should use the correct schema for documents with with virtualModelVersion higher than default when not a valid virtual model version', () => { | ||
| const data = createMockObject({ typeMigrationVersion: '11.1.4' }); | ||
| validator.validate(data); | ||
| expect(getCalledVersion()).toEqual('3.0.0'); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: atm, 11.1.4 is an 'invalid' version for any document to have, given there's no reason to have a major superior to 10, so I wouldn't add tests about it tbh. (also, why does it use the 3.0.0 schema and not the 10.1.0 one?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, why does it use the 3.0.0 schema and not the 10.1.0 one?
The docVersion is 11.1.4and the default version is 3.2.0. Because 11.1.4 isn't a valid virtual mode version, the conditional:
usedVersion = isVirtualModelVersion(docVersion) ? docVersion : this.defaultVersion;
implements the false path: usedVersion = this.defaultVersion, and previousVersionWithSchema returns 3.0.0.
Hence 3.0.0 and not 10.1.0.
| it('should use the correct schema for documents with virtualModelVersion', () => { | ||
| const data = createMockObject({ typeMigrationVersion: '10.1.0' }); | ||
| validator.validate(data); | ||
| expect(getCalledVersion()).toEqual('10.1.0'); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Instead of registering a 10.1.0 schema, I would register one of 10.2.0 and then assert that
- a document with version
10.1.0uses schema4.3.0 - a document with version
10.2.0uses schema10.2.0 - a document with version
10.3.0uses schema10.2.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a document with version
10.1.0uses schema4.3.0
The default version is 3.3.0 in the tests, so the schema that will be used is 3.0.0
|
@elasticmachine merge upstream |
|
@pgayvallet I've answered your questions and handled the changes. Could you please take another look? |
|
@elasticmachine merge upstream |
💚 Build Succeeded
Metrics [docs]Unknown metric groupsAPI count
ESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: |
pgayvallet
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| // jest.clearAllMocks(); | ||
|
|
||
| data = createMockObject({ | ||
| migrationVersion: { | ||
| [type]: '3.0.0', | ||
| }, | ||
| }); | ||
| validator.validate(data); | ||
| expect(getCalledVersion()).toEqual('3.0.0'); | ||
| // data = createMockObject({ | ||
| // migrationVersion: { | ||
| // [type]: '3.2.0', | ||
| // }, | ||
| // }); | ||
| // validator.validate(data); | ||
| // expect(getCalledVersion()).toEqual('3.0.0'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: remove commented code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should already be done
fix #152994
saved objects convert to using models. Ideally, type owners should still be able to validate objects against an expected model version schema for creating and editing their objects.
This PR adds support for optionally declaring
model.schema.create, with which to validate against aconfig.schema.Validation schemas for models are mapped to their virtual model version (model version '1' -> virtual version 10.1.0) in the
validationMap.