Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
feat: add validator package (#322)
Browse files Browse the repository at this point in the history
* feat: add validator package

* test: add unit tests

* feat: add legacy function

* fix: rename
  • Loading branch information
kristw authored Apr 1, 2020
1 parent 98ed48b commit ea78a4e
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/superset-ui-validator/README.md
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.
29 changes: 29 additions & 0 deletions packages/superset-ui-validator/package.json
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"
}
}
5 changes: 5 additions & 0 deletions packages/superset-ui-validator/src/index.ts
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 packages/superset-ui-validator/src/legacyValidateInteger.ts
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 packages/superset-ui-validator/src/legacyValidateNumber.ts
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;
}
12 changes: 12 additions & 0 deletions packages/superset-ui-validator/src/validateInteger.ts
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');
}
8 changes: 8 additions & 0 deletions packages/superset-ui-validator/src/validateNonEmpty.ts
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;
}
12 changes: 12 additions & 0 deletions packages/superset-ui-validator/src/validateNumber.ts
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 packages/superset-ui-validator/test/legacyValidateInteger.test.ts
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 packages/superset-ui-validator/test/legacyValidateNumber.test.ts
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 packages/superset-ui-validator/test/validateInteger.test.ts
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 packages/superset-ui-validator/test/validateNonEmpty.test.ts
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 packages/superset-ui-validator/test/validateNumber.test.ts
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();
});
});
4 changes: 4 additions & 0 deletions test/setup.ts
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 {
Expand Down

0 comments on commit ea78a4e

Please sign in to comment.