-
Notifications
You must be signed in to change notification settings - Fork 561
fix: validate-metadata-required-for-on-all-entities #6658
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
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5ac05e2
feat: calculate-has-metadata-required-based-on-all-entities
Zaimwa9 e895451
fix: calculate-has-metadata-required-based-on-all-entities
Zaimwa9 b81f1d6
fix: calculate-has-metadata-required-based-on-all-entities
Zaimwa9 1305509
feat: refactor-metadata-with-hooks
Zaimwa9 9f65d7c
feat: consolidated-requests-and-merged-strategy-in-rtk
Zaimwa9 242cc97
Merge branch 'main' of github.com:Flagsmith/flagsmith into fix/requir…
Zaimwa9 6f8c33f
feat: resolved-review-comments
Zaimwa9 2c6cf8b
fix: skip-feature-metadata-fetch-when-creating-feature
Zaimwa9 463d388
fix: fixed-parsing-error
Zaimwa9 0503ad3
feat: invalidate-tags-on-delete-metadata
Zaimwa9 81f4323
Merge branch 'main' of github.com:Flagsmith/flagsmith into fix/requir…
Zaimwa9 3cc20aa
feat: fixed-use-effect-on-entity-id-switch
Zaimwa9 77bbdc0
feat: cleaned-up-boolean-no-op
Zaimwa9 e2ad26e
feat: only-sync-local-state-without-edit
Zaimwa9 129ca9c
fix: env-custom-fields-reverting-local-value-on-change
Zaimwa9 937aecb
fix: stabilize-empty-reference
Zaimwa9 5f502cd
fix: invalidate-cache-on-mounting
Zaimwa9 a68c1a6
fix: fixed-empty-toas-if-message-error
Zaimwa9 ca9f681
fix: undefined-create-segment-id-blocking-metadata
Zaimwa9 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
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
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,9 @@ | ||
| import { MetadataField } from './responses' | ||
|
|
||
| export type CustomMetadataField = MetadataField & { | ||
| metadataModelFieldId: number | string | null | ||
| isRequiredFor: boolean | ||
| model_field?: string | number | ||
| hasValue?: boolean | ||
| field_value?: string | ||
| } |
103 changes: 103 additions & 0 deletions
103
frontend/common/utils/__tests__/mergeMetadataFields.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,103 @@ | ||
| import { mergeMetadataFields } from 'common/utils/mergeMetadataFields' | ||
| import { | ||
| MetadataField, | ||
| MetadataModelField, | ||
| PagedResponse, | ||
| } from 'common/types/responses' | ||
|
|
||
| const createFieldList = ( | ||
| fields: Partial<MetadataField>[], | ||
| ): PagedResponse<MetadataField> => ({ | ||
| results: fields.map((f, idx) => ({ | ||
| description: 'Test description', | ||
| id: idx + 1, | ||
| name: `Field ${idx + 1}`, | ||
| organisation: 1, | ||
| type: 'str', | ||
| ...f, | ||
| })) as MetadataField[], | ||
| }) | ||
|
|
||
| const createModelFieldList = ( | ||
| modelFields: Partial<MetadataModelField>[], | ||
| ): PagedResponse<MetadataModelField> => ({ | ||
| results: modelFields.map((mf, idx) => ({ | ||
| content_type: 100, | ||
| field: idx + 1, | ||
| id: `${idx + 10}`, | ||
| is_required_for: [], | ||
| ...mf, | ||
| })) as MetadataModelField[], | ||
| }) | ||
|
|
||
| describe('mergeMetadataFields', () => { | ||
| it('merges field definitions with existing values', () => { | ||
| const fieldList = createFieldList([{ id: 1, name: 'Field 1' }]) | ||
| const modelFieldList = createModelFieldList([ | ||
| { content_type: 100, field: 1, id: '10', is_required_for: [] }, | ||
| ]) | ||
| const entityData = { | ||
| metadata: [{ field_value: 'existing value', model_field: '10' }], | ||
| } | ||
|
|
||
| const result = mergeMetadataFields( | ||
| fieldList, | ||
| modelFieldList, | ||
| entityData, | ||
| 100, | ||
| ) | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(result[0].field_value).toBe('existing value') | ||
| expect(result[0].hasValue).toBe(true) | ||
| }) | ||
|
|
||
| it('sets empty field_value when no existing value or null entity', () => { | ||
| const fieldList = createFieldList([{ id: 1, name: 'Field 1' }]) | ||
| const modelFieldList = createModelFieldList([ | ||
| { content_type: 100, field: 1, id: '10', is_required_for: [] }, | ||
| ]) | ||
|
|
||
| const result = mergeMetadataFields(fieldList, modelFieldList, null, 100) | ||
|
|
||
| expect(result[0].field_value).toBe('') | ||
| expect(result[0].hasValue).toBe(false) | ||
| }) | ||
|
|
||
| it('marks field as required when is_required_for has entries', () => { | ||
| const fieldList = createFieldList([{ id: 1, name: 'Required Field' }]) | ||
| const modelFieldList = createModelFieldList([ | ||
| { | ||
| content_type: 100, | ||
| field: 1, | ||
| id: '10', | ||
| is_required_for: [{ content_type: 50 }], | ||
| }, | ||
| ]) | ||
|
|
||
| const result = mergeMetadataFields(fieldList, modelFieldList, null, 100) | ||
|
|
||
| expect(result[0].isRequiredFor).toBe(true) | ||
| }) | ||
|
|
||
| it('sorts required fields first', () => { | ||
| const fieldList = createFieldList([ | ||
| { id: 1, name: 'Optional' }, | ||
| { id: 2, name: 'Required' }, | ||
| ]) | ||
| const modelFieldList = createModelFieldList([ | ||
| { content_type: 100, field: 1, id: '10', is_required_for: [] }, | ||
| { | ||
| content_type: 100, | ||
| field: 2, | ||
| id: '11', | ||
| is_required_for: [{ content_type: 50 }], | ||
| }, | ||
| ]) | ||
|
|
||
| const result = mergeMetadataFields(fieldList, modelFieldList, null, 100) | ||
|
|
||
| expect(result[0].name).toBe('Required') | ||
| expect(result[1].name).toBe('Optional') | ||
| }) | ||
| }) |
90 changes: 90 additions & 0 deletions
90
frontend/common/utils/__tests__/metadataValidation.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,90 @@ | ||
| import { getGlobalMetadataValidationState } from 'common/utils/metadataValidation' | ||
| import { CustomMetadataField } from 'common/types/metadata-field' | ||
|
|
||
| const createField = ( | ||
| partialField: Partial<CustomMetadataField> = {}, | ||
| ): CustomMetadataField => ({ | ||
| description: 'A test field', | ||
| field_value: '', | ||
| hasValue: false, | ||
| id: 1, | ||
| isRequiredFor: false, | ||
| metadataModelFieldId: 1, | ||
| name: 'Test Field', | ||
| organisation: 1, | ||
| type: 'str', | ||
| ...partialField, | ||
| }) | ||
|
|
||
| describe('getGlobalMetadataValidationState', () => { | ||
| it('returns all zeros for empty fields array', () => { | ||
| const result = getGlobalMetadataValidationState([]) | ||
|
|
||
| expect(result).toEqual({ | ||
| hasUnfilledRequired: false, | ||
| totalFilledRequired: 0, | ||
| totalRequired: 0, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns hasUnfilledRequired false when no required fields', () => { | ||
| const fields = [ | ||
| createField({ id: 1, isRequiredFor: false }), | ||
| createField({ id: 2, isRequiredFor: false }), | ||
| ] | ||
|
|
||
| const result = getGlobalMetadataValidationState(fields) | ||
|
|
||
| expect(result).toEqual({ | ||
| hasUnfilledRequired: false, | ||
| totalFilledRequired: 0, | ||
| totalRequired: 0, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns hasUnfilledRequired false when required field is filled', () => { | ||
| const fields = [ | ||
| createField({ field_value: 'some value', id: 1, isRequiredFor: true }), | ||
| ] | ||
|
|
||
| const result = getGlobalMetadataValidationState(fields) | ||
|
|
||
| expect(result).toEqual({ | ||
| hasUnfilledRequired: false, | ||
| totalFilledRequired: 1, | ||
| totalRequired: 1, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns hasUnfilledRequired true when some required fields are unfilled', () => { | ||
| const fields = [ | ||
| createField({ field_value: 'filled', id: 1, isRequiredFor: true }), | ||
| createField({ field_value: '', id: 2, isRequiredFor: true }), | ||
| createField({ field_value: '', id: 3, isRequiredFor: false }), | ||
| ] | ||
|
|
||
| const result = getGlobalMetadataValidationState(fields) | ||
|
|
||
| expect(result).toEqual({ | ||
| hasUnfilledRequired: true, | ||
| totalFilledRequired: 1, | ||
| totalRequired: 2, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns hasUnfilledRequired false when all required fields are filled', () => { | ||
| const fields = [ | ||
| createField({ field_value: 'filled', id: 1, isRequiredFor: true }), | ||
| createField({ field_value: 'also filled', id: 2, isRequiredFor: true }), | ||
| createField({ field_value: '', id: 3, isRequiredFor: false }), | ||
| ] | ||
|
|
||
| const result = getGlobalMetadataValidationState(fields) | ||
|
|
||
| expect(result).toEqual({ | ||
| hasUnfilledRequired: false, | ||
| totalFilledRequired: 2, | ||
| totalRequired: 2, | ||
| }) | ||
| }) | ||
| }) |
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.