Skip to content

Commit

Permalink
Merge pull request #170 from webitel/feature/search-bar-error-color
Browse files Browse the repository at this point in the history
feature: variable search validator pattern fixed to not pass spaces n…
  • Loading branch information
dlohvinov authored Oct 26, 2023
2 parents 30d4e04 + 652a225 commit 5b7be04
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/validators/variableSearchValidator.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/validators/variableSearchValidator/Readme.md
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
```
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 src/validators/variableSearchValidator/variableSearchValidator.js
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;
}
4 changes: 4 additions & 0 deletions styleguide.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module.exports = {
name: 'Websocket Validator',
content: 'src/validators/websocketValidator/Readme.md',
},
{
name: 'Variable Search Validator',
content: 'src/validators/variableSearchValidator/Readme.md',
},
],
},
],
Expand Down

0 comments on commit 5b7be04

Please sign in to comment.