-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from webitel/feature/search-bar-error-color
feature: variable search validator pattern fixed to not pass spaces n…
- Loading branch information
Showing
5 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
Validator for variable search string | ||
```javascript | ||
import variableSearchValidator from '@webitel/ui-sdk/src/validators/variableSearchValidator/variableSearchValidator'; | ||
|
||
isValidKeyValuePair("height=100"); // true | ||
isValidKeyValuePair("1 1=2 2"); // true | ||
isValidKeyValuePair("you height=5 feet 5½ inches (166 cm)"); // true | ||
isValidKeyValuePair("expected_output=valid_number")); // true | ||
isValidKeyValuePair("height= 100"); // false | ||
isValidKeyValuePair("height =100"); // false | ||
isValidKeyValuePair("height = 100"); // false | ||
isValidKeyValuePair("height=5 feet 5½ inches (166 cm), age=1231, fin=123231"); // false | ||
``` |
48 changes: 48 additions & 0 deletions
48
src/validators/variableSearchValidator/__tests__/variableSearchValidator.spec.js
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,48 @@ | ||
import variableSearchValidator from '../variableSearchValidator'; | ||
|
||
describe('Variable search validator', () => { | ||
it('Ordinary search', () => { | ||
const searchValue = 'height=100'; | ||
expect(variableSearchValidator(searchValue)).toBe(true); | ||
}); | ||
it('Search with spacing in value and key', () => { | ||
const searchValue = 'your height=5 feet 5½ inches (166 cm)'; | ||
expect(variableSearchValidator(searchValue)).toBe(true); | ||
}); | ||
it('Search with spacing in value', () => { | ||
const searchValue = 'height=5 feet 5½ inches (166 cm)'; | ||
expect(variableSearchValidator(searchValue)).toBe(true); | ||
}); | ||
it('Search with spacing in value and spaces before key and after value', () => { | ||
const searchValue = ' height=5 feet 5½ inches (166 cm) '; | ||
expect(variableSearchValidator(searchValue)).toBe(true); | ||
}); | ||
it('Search with snake_case', () => { | ||
const searchValue = 'expected_output=valid_number'; | ||
expect(variableSearchValidator(searchValue)).toBe(true); | ||
}); | ||
it('Wrong search with spacing after "=" sign', () => { | ||
const searchValue = 'height= 100'; | ||
expect(variableSearchValidator(searchValue)).toBe(false); | ||
}); | ||
it('Wrong search with spacing before "=" sign', () => { | ||
const searchValue = 'height =100'; | ||
expect(variableSearchValidator(searchValue)).toBe(false); | ||
}); | ||
it('Wrong search with spacing near "=" sign', () => { | ||
const searchValue = 'height = 100'; | ||
expect(variableSearchValidator(searchValue)).toBe(false); | ||
}); | ||
it('Space search', () => { | ||
const searchValue = ' '; | ||
expect(variableSearchValidator(searchValue)).toBe(false); | ||
}); | ||
it('Empty search', () => { | ||
const searchValue = ''; | ||
expect(variableSearchValidator(searchValue)).toBe(true); | ||
}); | ||
it('Multiple "=" search', () => { | ||
const searchValue = 'height=5 feet 5½ inches (166 cm), age=1231, fin=123231'; | ||
expect(variableSearchValidator(searchValue)).toBe(false); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
src/validators/variableSearchValidator/variableSearchValidator.js
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,10 @@ | ||
export default function variableSearchValidator(input) { | ||
if (input) { | ||
const splitInput = input.split('='); | ||
const [key, value] = splitInput; | ||
if (key.endsWith(' ') || value.startsWith(' ') || splitInput.length !== 2) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
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