This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add validator package * test: add unit tests * feat: add legacy function * fix: rename
- Loading branch information
Showing
14 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
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,23 @@ | ||
## @superset-ui/validator | ||
|
||
[![Version](https://img.shields.io/npm/v/@superset-ui/validator.svg?style=flat)](https://img.shields.io/npm/v/@superset-ui/validator.svg?style=flat) | ||
[![David (path)](https://img.shields.io/david/apache-superset/superset-ui.svg?path=packages%2Fsuperset-ui-validator&style=flat-square)](https://david-dm.org/apache-superset/superset-ui?path=packages/superset-ui-validator) | ||
|
||
Description | ||
|
||
#### Example usage | ||
|
||
```js | ||
import { xxx } from '@superset-ui/validator'; | ||
``` | ||
|
||
#### API | ||
|
||
`fn(args)` | ||
|
||
- Do something | ||
|
||
### Development | ||
|
||
`@data-ui/build-config` is used to manage the build configuration for this package including babel | ||
builds, jest testing, eslint, and prettier. |
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,29 @@ | ||
{ | ||
"name": "@superset-ui/validator", | ||
"version": "0.0.0", | ||
"description": "Superset UI validator", | ||
"sideEffects": false, | ||
"main": "lib/index.js", | ||
"module": "esm/index.js", | ||
"files": [ | ||
"esm", | ||
"lib" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/apache-superset/superset-ui.git" | ||
}, | ||
"keywords": ["superset"], | ||
"author": "Superset", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/apache-superset/superset-ui/issues" | ||
}, | ||
"homepage": "https://github.com/apache-superset/superset-ui#readme", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"peerDependencies": { | ||
"@superset-ui/translation": "^0.12.0" | ||
} | ||
} |
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,5 @@ | ||
export { default as legacyValidateInteger } from './legacyValidateInteger'; | ||
export { default as legacyValidateNumber } from './legacyValidateNumber'; | ||
export { default as validateInteger } from './validateInteger'; | ||
export { default as validateNumber } from './validateNumber'; | ||
export { default as validateNonEmpty } from './validateNonEmpty'; |
12 changes: 12 additions & 0 deletions
12
packages/superset-ui-validator/src/legacyValidateInteger.ts
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,12 @@ | ||
import { t } from '@superset-ui/translation'; | ||
|
||
/** | ||
* formerly called integer() | ||
* @param v | ||
*/ | ||
export default function legacyValidateInteger(v: unknown) { | ||
if (v && (isNaN(v as number) || parseInt(v as string, 10) !== Number(v))) { | ||
return t('is expected to be an integer'); | ||
} | ||
return false; | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/superset-ui-validator/src/legacyValidateNumber.ts
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,12 @@ | ||
import { t } from '@superset-ui/translation'; | ||
|
||
/** | ||
* formerly called numeric() | ||
* @param v | ||
*/ | ||
export default function numeric(v: unknown) { | ||
if (v && isNaN(v as number)) { | ||
return t('is expected to be a number'); | ||
} | ||
return false; | ||
} |
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,12 @@ | ||
import { t } from '@superset-ui/translation'; | ||
|
||
export default function validateInteger(v: unknown) { | ||
if ( | ||
(typeof v === 'string' && v.trim().length > 0 && Number.isInteger(Number(v.trim()))) || | ||
(typeof v === 'number' && Number.isInteger(v)) | ||
) { | ||
return false; | ||
} | ||
|
||
return t('is expected to be an integer'); | ||
} |
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,8 @@ | ||
import { t } from '@superset-ui/translation'; | ||
|
||
export default function validateNonEmpty(v: unknown) { | ||
if (v === null || typeof v === 'undefined' || v === '' || (Array.isArray(v) && v.length === 0)) { | ||
return t('cannot be empty'); | ||
} | ||
return false; | ||
} |
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,12 @@ | ||
import { t } from '@superset-ui/translation'; | ||
|
||
export default function validateInteger(v: unknown) { | ||
if ( | ||
(typeof v === 'string' && v.trim().length > 0 && Number.isFinite(Number(v.trim()))) || | ||
(typeof v === 'number' && Number.isFinite(v)) | ||
) { | ||
return false; | ||
} | ||
|
||
return t('is expected to be a number'); | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/superset-ui-validator/test/legacyValidateInteger.test.ts
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,20 @@ | ||
import { legacyValidateInteger } from '../src'; | ||
|
||
describe('legacyValidateInteger()', () => { | ||
it('returns the warning message if invalid', () => { | ||
expect(legacyValidateInteger(10.1)).toBeTruthy(); | ||
expect(legacyValidateInteger('abc')).toBeTruthy(); | ||
expect(legacyValidateInteger(Infinity)).toBeTruthy(); | ||
}); | ||
it('returns false if the input is valid', () => { | ||
// superset seems to operate on this incorrect behavior at the moment | ||
expect(legacyValidateInteger(NaN)).toBeFalsy(); | ||
expect(legacyValidateInteger(undefined)).toBeFalsy(); | ||
expect(legacyValidateInteger(null)).toBeFalsy(); | ||
expect(legacyValidateInteger('')).toBeFalsy(); | ||
|
||
expect(legacyValidateInteger(0)).toBeFalsy(); | ||
expect(legacyValidateInteger(10)).toBeFalsy(); | ||
expect(legacyValidateInteger('10')).toBeFalsy(); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/superset-ui-validator/test/legacyValidateNumber.test.ts
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,20 @@ | ||
import { legacyValidateNumber } from '../src'; | ||
|
||
describe('legacyValidateNumber()', () => { | ||
it('returns the warning message if invalid', () => { | ||
expect(legacyValidateNumber('abc')).toBeTruthy(); | ||
}); | ||
it('returns false if the input is valid', () => { | ||
// superset seems to operate on this incorrect behavior at the moment | ||
expect(legacyValidateNumber(NaN)).toBeFalsy(); | ||
expect(legacyValidateNumber(Infinity)).toBeFalsy(); | ||
expect(legacyValidateNumber(undefined)).toBeFalsy(); | ||
expect(legacyValidateNumber(null)).toBeFalsy(); | ||
expect(legacyValidateNumber('')).toBeFalsy(); | ||
|
||
expect(legacyValidateNumber(0)).toBeFalsy(); | ||
expect(legacyValidateNumber(10.1)).toBeFalsy(); | ||
expect(legacyValidateNumber(10)).toBeFalsy(); | ||
expect(legacyValidateNumber('10')).toBeFalsy(); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/superset-ui-validator/test/validateInteger.test.ts
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,18 @@ | ||
import { validateInteger } from '../src'; | ||
|
||
describe('validateInteger()', () => { | ||
it('returns the warning message if invalid', () => { | ||
expect(validateInteger(10.1)).toBeTruthy(); | ||
expect(validateInteger(NaN)).toBeTruthy(); | ||
expect(validateInteger(Infinity)).toBeTruthy(); | ||
expect(validateInteger(undefined)).toBeTruthy(); | ||
expect(validateInteger(null)).toBeTruthy(); | ||
expect(validateInteger('abc')).toBeTruthy(); | ||
expect(validateInteger('')).toBeTruthy(); | ||
}); | ||
it('returns false if the input is valid', () => { | ||
expect(validateInteger(0)).toBeFalsy(); | ||
expect(validateInteger(10)).toBeFalsy(); | ||
expect(validateInteger('10')).toBeFalsy(); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/superset-ui-validator/test/validateNonEmpty.test.ts
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,15 @@ | ||
import { validateNonEmpty } from '../src'; | ||
|
||
describe('validateNonEmpty()', () => { | ||
it('returns the warning message if invalid', () => { | ||
expect(validateNonEmpty([])).toBeTruthy(); | ||
expect(validateNonEmpty(undefined)).toBeTruthy(); | ||
expect(validateNonEmpty(null)).toBeTruthy(); | ||
expect(validateNonEmpty('')).toBeTruthy(); | ||
}); | ||
it('returns false if the input is valid', () => { | ||
expect(validateNonEmpty(0)).toBeFalsy(); | ||
expect(validateNonEmpty(10)).toBeFalsy(); | ||
expect(validateNonEmpty('abc')).toBeFalsy(); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/superset-ui-validator/test/validateNumber.test.ts
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,18 @@ | ||
import { validateNumber } from '../src'; | ||
|
||
describe('validateNumber()', () => { | ||
it('returns the warning message if invalid', () => { | ||
expect(validateNumber(NaN)).toBeTruthy(); | ||
expect(validateNumber(Infinity)).toBeTruthy(); | ||
expect(validateNumber(undefined)).toBeTruthy(); | ||
expect(validateNumber(null)).toBeTruthy(); | ||
expect(validateNumber('abc')).toBeTruthy(); | ||
expect(validateNumber('')).toBeTruthy(); | ||
}); | ||
it('returns false if the input is valid', () => { | ||
expect(validateNumber(0)).toBeFalsy(); | ||
expect(validateNumber(10.1)).toBeFalsy(); | ||
expect(validateNumber(10)).toBeFalsy(); | ||
expect(validateNumber('10')).toBeFalsy(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import { configure } from '@superset-ui/translation'; | ||
|
||
configure(); | ||
|
||
const caches = {}; | ||
|
||
class Cache { | ||
|