Skip to content

Commit

Permalink
Merge 05339c2 into fef9311
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzitw committed Jun 29, 2019
2 parents fef9311 + 05339c2 commit e7e2632
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ node_modules/
.vscode
.idea

# Test coverage directory
coverage

# Distribution files
dist
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
sudo: false
language: node_js
node_js:
- '10'
branches:
only:
- master
cache:
directories:
- node_modules
before_install:
- npm update
install:
- npm install
script:
- npm run test
- npm run test:coveralls
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Semantic Validator
# Semantic Validator

A functional semantic validator tool for validation of several types and rich content,
inspired by prop-types from React.
Expand Down Expand Up @@ -117,7 +117,7 @@ op.or(validator1, validator2, ...validators)
```
Example:
```javascript
const validate = op.or(is.nil(), is.integer());
const validate = op.or(is.nul(), is.integer());
validate(null); // => true
validate(123); // => true
validate('abc'); // => false
Expand Down Expand Up @@ -337,14 +337,14 @@ validate(undefined); // => true
validate(123); // => false
```

**is: nil**
**is: nul**
Will be valid when the value is null.
```javascript
is.nil()
is.nul()
```
Example:
```javascript
const validate = is.nil();
const validate = is.nul();
validate(null); // => true
validate(123); // => false
```
Expand Down
8 changes: 7 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ module.exports = {
'ts-jest': {
tsConfig: '<rootDir>/test/tsconfig.json'
}
}
},
collectCoverage: true,
collectCoverageFrom: [
'<rootDir>/src/op/**/*.ts',
'<rootDir>/src/is/**/*.ts',
],
coverageReporters: ['lcov', 'text-summary'],
};
42 changes: 41 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "semantic-validator-dev",
"version": "0.0.1",
"version": "0.0.2",
"description": "A functional semantic validator tool for validation of several types and rich content.",
"private": true,
"author": "ChenZi <chenzi@ubun.tw>",
Expand All @@ -11,9 +11,11 @@
},
"scripts": {
"start": "ts-node --files -O '{\"module\":\"commonjs\"}' -r tsconfig-paths/register ./src",
"lint": "tsc --noEmit && eslint {src,test}/**/* --quiet",
"test": "jest --ForceExit",
"test:coveralls": "cat ./coverage/lcov.info | coveralls",
"build": "rm -rf ./dist && tsc --build && cp ./package.prod.json ./dist/package.json && cp ./README.md ./dist/README.md && cp ./LICENSE ./dist/LICENSE",
"publish": "npm publish dist",
"test": "jest"
"publish": "npm publish dist"
},
"main": "index.js",
"devDependencies": {
Expand All @@ -22,6 +24,7 @@
"@types/validator": "^10.11.1",
"@typescript-eslint/eslint-plugin": "^1.11.0",
"@typescript-eslint/parser": "^1.11.0",
"coveralls": "^3.0.4",
"eslint": "^6.0.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-import-resolver-typescript": "^1.1.1",
Expand Down
2 changes: 1 addition & 1 deletion package.prod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "semantic-validator",
"version": "0.0.1",
"version": "0.0.2",
"description": "A functional semantic validator tool for validation of several types and rich content.",
"author": "ChenZi <chenzi@ubun.tw>",
"license": "MIT",
Expand Down
40 changes: 6 additions & 34 deletions src/is/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
} from '../definition';
import {
isSameValueZero,
validateNumber,
} from '../utils';
import {
definedValidator,
notDefinedValidator,
nulValidator,
nilValidator,
booleanValidator,
numberValidator,
Expand Down Expand Up @@ -37,6 +37,8 @@ export const defined = (): Validator => definedValidator;

export const notDefined = (): Validator => notDefinedValidator;

export const nul = (): Validator => nulValidator;

export const nil = (): Validator => nilValidator;

export const bool = (): Validator => booleanValidator;
Expand All @@ -53,39 +55,9 @@ export const symbol = (): Validator => symbolValidator;

export const instanceOf = (constructor: any): Validator => val => (val instanceof constructor);

export const float = (
options?: {
equalTo?: number;
greaterThan?: number;
atLeast?: number;
lessThan?: number;
atMost?: number;
between?: [number, number];
fromTo?: [number, number];
},
): Validator => {
if ((options === undefined) || (Object.keys(options).length === 0)) {
return floatValidator;
}
return (val: any) => (Number.isFinite(val) && validateNumber(val, options));
};

export const integer = (
options?: {
equalTo?: number;
greaterThan?: number;
atLeast?: number;
lessThan?: number;
atMost?: number;
between?: [number, number];
fromTo?: [number, number];
},
): Validator => {
if ((options === undefined) || (Object.keys(options).length === 0)) {
return integerValidator;
}
return (val: any) => (Number.isInteger(val) && validateNumber(val, options));
};
export const float = (): Validator => floatValidator;

export const integer = (): Validator => integerValidator;

export const array = (): Validator => arrayValidator;

Expand Down
32 changes: 0 additions & 32 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,3 @@ export const isShapeOrExactValidation = (validation: any): validation is object
|| (validation === null)
|| Object.keys(validation).every(validator => typeof validator === 'function')
);

export const validateNumber = (
val: number,
options?: {
equalTo?: number;
greaterThan?: number;
atLeast?: number;
lessThan?: number;
atMost?: number;
between?: [number, number];
fromTo?: [number, number];
},
): boolean => {
if ((options === undefined) || (Object.keys(options).length === 0)) {
return true;
}
return (true
&& ((options && options.equalTo !== undefined) ? (val === options.equalTo) : true)
&& ((options && options.greaterThan !== undefined) ? (val > options.greaterThan) : true)
&& ((options && options.atLeast !== undefined) ? (val >= options.atLeast) : true)
&& ((options && options.lessThan !== undefined) ? (val < options.lessThan) : true)
&& ((options && options.atMost !== undefined) ? (val <= options.atMost) : true)
&& ((options && options.between !== undefined) ? (true
&& (val > Math.min(...options.between))
&& (val < Math.max(...options.between))
) : true)
&& ((options && options.fromTo !== undefined) ? (true
&& (val >= Math.min(...options.fromTo))
&& (val <= Math.max(...options.fromTo))
) : true)
);
};
6 changes: 5 additions & 1 deletion src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ export const notDefinedValidator = (
(val: any): boolean => (typeof val === 'undefined')
);

export const nilValidator = (
export const nulValidator = (
(val: any): val is null => (val === null)
);

export const nilValidator = (
(val: any): val is (undefined | null) => ((typeof val === 'undefined') || (val === null))
);

export const booleanValidator = (
(val: any): val is boolean => (typeof val === 'boolean')
);
Expand Down
4 changes: 2 additions & 2 deletions test/cases/some-simple-posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { op, is } from 'semantic-validator';
describe('normal case 01', (): void => {
const validator = op.every(
op.shape({
id: is.integer({ atLeast: 1 }),
id: is.integer(),
content: is.string(),
attachement: op.or(
is.nil(),
is.nul(),
op.exact({
type: is.same('image'),
url: is.string(),
Expand Down
13 changes: 12 additions & 1 deletion test/is/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
oneOf,
defined,
notDefined,
nul,
nil,
bool,
number,
Expand Down Expand Up @@ -78,12 +79,22 @@ describe('basic validator creators', () => {
});
});

describe('is: nul', () => {
it('should return true when the value is nul', () => {
expect(nul()(null)).toBe(true);
});
it('should return false when the value is not nul', () => {
expect(nul()(undefined)).toBe(false);
expect(nul()(true)).toBe(false);
});
});

describe('is: nil', () => {
it('should return true when the value is nil', () => {
expect(nil()(null)).toBe(true);
expect(nil()(undefined)).toBe(true);
});
it('should return false when the value is not nil', () => {
expect(nil()(undefined)).toBe(false);
expect(nil()(true)).toBe(false);
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/op/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ describe('basic validator operators', () => {

describe('op: or', () => {
const is = {
nil: () => (val: any) => (val === null),
nul: () => (val: any) => (val === null),
integer: () => (val: any) => Number.isInteger(val),
};
it('should return true when any validators return true', () => {
expect(or(is.nil(), is.integer())(null)).toBe(true);
expect(or(is.nil(), is.integer())(123)).toBe(true);
expect(or(is.nul(), is.integer())(null)).toBe(true);
expect(or(is.nul(), is.integer())(123)).toBe(true);
});
it('should return false when not any validators return true', () => {
expect(or(is.nil(), is.integer())('abc')).toBe(false);
expect(or(is.nul(), is.integer())('abc')).toBe(false);
});
});

Expand Down

0 comments on commit e7e2632

Please sign in to comment.