-
Notifications
You must be signed in to change notification settings - Fork 128
refactor(condo): DOMA-12673 remove validate.js #7626
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
Open
vovaaxeapolla
wants to merge
2
commits into
main
Choose a base branch
from
condo/refactor/DOMA-12673/remove-validate-js
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
165 changes: 106 additions & 59 deletions
165
apps/condo/domains/common/utils/validation.utils.spec.js
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 |
|---|---|---|
| @@ -1,77 +1,124 @@ | ||
| const { hasValidJsonStructure } = require('./validation.utils') | ||
| const { | ||
| hasRequiredJsonObject, | ||
| hasOptionalJsonObject, | ||
| } = require('./validation.utils') | ||
|
|
||
| describe('hasValidJsonStructure()', () => { | ||
| describe('dv', () => { | ||
| test('no dv', () => { | ||
| describe('JSON object validators', () => { | ||
| const makeArgs = (fieldPath, value, errors) => ({ | ||
| resolvedData: { [fieldPath]: value }, | ||
| fieldPath, | ||
| addFieldValidationError: (err) => errors.push(err), | ||
| }) | ||
|
|
||
| describe('hasRequiredJsonObject()', () => { | ||
| test('should fail when field is missing and return error', () => { | ||
| const errors = [] | ||
| const fieldPath = 'meta' | ||
| const addFieldValidationError = (err) => errors.push(err) | ||
| const args = { resolvedData: { [fieldPath]: {} }, fieldPath, addFieldValidationError } | ||
| const args = { | ||
| resolvedData: {}, | ||
| fieldPath: 'meta', | ||
| addFieldValidationError: (e) => errors.push(e), | ||
| } | ||
|
|
||
| hasValidJsonStructure(args, true, 1, {}) | ||
| const result = hasRequiredJsonObject(args) | ||
|
|
||
| expect(result).toBe(false) | ||
| expect(errors).toEqual([ | ||
| '[json:unknownDataVersion:meta] Unknown `dv` attr inside JSON Object', | ||
| '[required:noValue:meta] Value is required', | ||
| ]) | ||
| }) | ||
| test('wrong dv', () => { | ||
|
|
||
| test('should fail when value is not object and return error', () => { | ||
| const errors = [] | ||
| const args = makeArgs('meta', 'string', errors) | ||
|
|
||
| const result = hasRequiredJsonObject(args) | ||
|
|
||
| expect(result).toBe(false) | ||
| expect(errors).toEqual([ | ||
| '[json:expectObject:meta] Expect JSON Object', | ||
| ]) | ||
| }) | ||
|
|
||
| test('should fail when dv is wrong and return error', () => { | ||
| const errors = [] | ||
| const fieldPath = 'meta' | ||
| const addFieldValidationError = (err) => errors.push(err) | ||
| const args = { resolvedData: { [fieldPath]: { dv: 2 } }, fieldPath, addFieldValidationError } | ||
| const args = makeArgs('meta', { dv: 2 }, errors) | ||
|
|
||
| hasValidJsonStructure(args, false, 1, {}) | ||
| const result = hasRequiredJsonObject(args) | ||
|
|
||
| expect(result).toBe(false) | ||
| expect(errors).toEqual([ | ||
| '[json:unknownDataVersion:meta] Unknown `dv` attr inside JSON Object', | ||
| ]) | ||
| }) | ||
|
|
||
| test('should pass when object is valid and return true', () => { | ||
| const errors = [] | ||
| const args = makeArgs('meta', { dv: 1, name: 'test' }, errors) | ||
|
|
||
| const result = hasRequiredJsonObject(args) | ||
|
|
||
| expect(result).toBe(true) | ||
| expect(errors).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('fieldConstrains', () => { | ||
| describe('string type check', () => { | ||
| test('for no value', () => { | ||
| const errors = [] | ||
| const fieldPath = 'meta' | ||
| const addFieldValidationError = (err) => errors.push(err) | ||
| const args = { resolvedData: { [fieldPath]: { dv: 1 } }, fieldPath, addFieldValidationError } | ||
|
|
||
| hasValidJsonStructure(args, false, 1, { | ||
| name: { type: 'string' }, | ||
| }) | ||
|
|
||
| expect(errors).toEqual([]) | ||
| }) | ||
| test('for empty value', () => { | ||
| const errors = [] | ||
| const fieldPath = 'meta' | ||
| const addFieldValidationError = (err) => errors.push(err) | ||
| const args = { resolvedData: { [fieldPath]: { dv: 1, name: '' } }, fieldPath, addFieldValidationError } | ||
|
|
||
| hasValidJsonStructure(args, false, 1, { | ||
| name: { type: 'string' }, | ||
| }) | ||
|
|
||
| expect(errors).toEqual([]) | ||
| }) | ||
| test('for number value', () => { | ||
| const errors = [] | ||
| const fieldPath = 'meta' | ||
| const addFieldValidationError = (err) => errors.push(err) | ||
| const args = { | ||
| resolvedData: { [fieldPath]: { dv: 1, name: 3221 } }, | ||
| fieldPath, | ||
| addFieldValidationError, | ||
| } | ||
|
|
||
| hasValidJsonStructure(args, false, 1, { | ||
| name: { type: 'string' }, | ||
| }) | ||
|
|
||
| expect(errors).toEqual([ | ||
| '[json:wrongDataVersionFormat:meta] Field \'name\': Name must be of type string', | ||
| ]) | ||
| }) | ||
| describe('hasOptionalJsonObject()', () => { | ||
| test('should pass when field is missing and return true', () => { | ||
| const errors = [] | ||
| const args = { | ||
| resolvedData: {}, | ||
| fieldPath: 'meta', | ||
| addFieldValidationError: (e) => errors.push(e), | ||
| } | ||
|
|
||
| const result = hasOptionalJsonObject(args) | ||
|
|
||
| expect(result).toBe(true) | ||
| expect(errors).toEqual([]) | ||
| }) | ||
|
|
||
| test('should fail when value is not object and return error', () => { | ||
| const errors = [] | ||
| const args = makeArgs('meta', 123, errors) | ||
|
|
||
| const result = hasOptionalJsonObject(args) | ||
|
|
||
| expect(result).toBe(false) | ||
| expect(errors).toEqual([ | ||
| '[json:expectObject:meta] Expect JSON Object', | ||
| ]) | ||
| }) | ||
|
|
||
| test('should fail when dv is wrong and return error', () => { | ||
| const errors = [] | ||
| const args = makeArgs('meta', { dv: 3 }, errors) | ||
|
|
||
| const result = hasOptionalJsonObject(args) | ||
|
|
||
| expect(result).toBe(false) | ||
| expect(errors).toEqual([ | ||
| '[json:unknownDataVersion:meta] Unknown `dv` attr inside JSON Object', | ||
| ]) | ||
| }) | ||
|
|
||
| test('should pass when object is valid and return true', () => { | ||
| const errors = [] | ||
| const args = makeArgs('meta', { dv: 1 }, errors) | ||
|
|
||
| const result = hasOptionalJsonObject(args) | ||
|
|
||
| expect(result).toBe(true) | ||
| expect(errors).toEqual([]) | ||
| }) | ||
|
|
||
| test('should pass when object has extra fields and return true', () => { | ||
| const errors = [] | ||
| const args = makeArgs('meta', { dv: 1, foo: 'bar' }, errors) | ||
|
|
||
| const result = hasOptionalJsonObject(args) | ||
|
|
||
| expect(result).toBe(true) | ||
| expect(errors).toEqual([]) | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
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.
Avoid appending a misleading second validation error.
On Line 94-95,
hasRequiredJsonObject(args)already records a specific error; addingJSON_EXPECT_OBJECT_ERRORon everyfalseresult can produce duplicate or incorrect messaging (notably fordvmismatch cases).Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents