Skip to content

Commit

Permalink
Update version 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhngch committed Oct 17, 2020
1 parent 2559872 commit 1f1f64a
Show file tree
Hide file tree
Showing 17 changed files with 1,007 additions and 68 deletions.
471 changes: 462 additions & 9 deletions Readme.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions example/test1.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// node -r esm example/test1.js
import Type, { func, number, optional, string,
isArrayElement,
max, min, length, email, regex, isOptional, integer } from '../src/type.js'
Expand Down
1 change: 1 addition & 0 deletions example/test2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// node -r esm example/test2.js
import Type, { func, number, optional, string,
isArrayElement,
max, min, length, email, regex, isOptional, integer } from '../src/type.js'
Expand Down
43 changes: 43 additions & 0 deletions example/test3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// node -r esm example/test3.js
import Type, { string, Self } from '../src/type.js'
// const Person = new Type({
// name: string,
// friend: Self,
// });
// const me = {
// name: 'Peter',
// friend: {
// name: 'Bod',
// friend: {
// name: 'John',
// }
// }
// };
// const errors = Person.validate(me);
// console.log(Person.schema);
// console.log(errors);

const Person = new Type({
name: string,
friends: [Self],
});
const me = {
name: 'Peter',
friends: [
{
name: 'Bod',
},
{
name: 'Ana',
friends: [
{
name: 'John',
},
],
},
],
};

const errors = Person.validate(me);
console.log(Person.schema);
console.log(errors);
24 changes: 24 additions & 0 deletions example/test4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// node -r esm example/test4.js
// test class
import Type, { promise } from '../src/type.js';

class Person {
constructor(name) {
this.name = name;
}
};

const PersonType = new Type({
bestFriend: Object,
age: Number,
myFriends: [Person],
});

const me = {
bestFriend: new Person('Hana'), // instanceof class Object
age: new Number(123), // instanceof class Number
myFriends: [new Person('John'), new Person('Peter')],
};

const errors = PersonType.validate(me); // no errors
console.log(errors);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-validate-type",
"description": "Validate type in javascript at runtime",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"scripts": {
"test": "jest",
Expand Down Expand Up @@ -36,6 +36,7 @@
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^24.0.2",
"esm": "^3.2.25",
"jest": "^26.4.2",
"open-cli": "^6.0.1"
}
Expand Down
42 changes: 30 additions & 12 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import {
isNumber,
isString,
isFunction,
isDate,
isPromise,
isNull,
isUndefined,
isInteger,
} from './util';

export * from './util';

const MAX_TYPE = 2048;
const ALL_TYPE = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048];

export const Self = -1;
export const optional = 1;
export const boolean = 2;
export const number = 4;
Expand All @@ -25,8 +27,7 @@ export const Undefined = 64;
export const array = 128;
export const object = 256;
export const func = 512;
export const date = 1024;
export const promise = 2048;
export const promise = 1024;

export const max = (value, a) => a >= value;
export const min = (value, a) => a <= value;
Expand All @@ -51,7 +52,6 @@ const validateObj = {
[array]: isArray,
[object]: isObject,
[func]: isFunction,
[date]: isDate,
[promise]: isPromise,
};

Expand Down Expand Up @@ -103,6 +103,18 @@ export class Type {
if (type === optional) {
return;
}
if (!value && type === Self) {
return;
}
if (isFunction(type)) {
if (!(value instanceof type)) {
this.listError.push({
key,
message: `'${value} is not instance of class ${type.name}'`,
});
}
return;
}
if (isArray(type) && isArray(type[0])) {
// function test eg: max, min, length, isString, isOptional, ...
if (isFunction(type[0][0])) {
Expand Down Expand Up @@ -171,21 +183,27 @@ export class Type {
});
}
if (!isArray(value)) {
this.listError.push({
key,
message: `value of '${key}' is not array`,
});
if (type && type[0] !== Self) {
this.listError.push({
key,
message: `value of '${key}' is not array`,
});
}
} else {
value.forEach((v) => {
this.validateRecusive(v, type[0], key, valueParam);
});
}
} else if (isObject(value)) {
if (!isObject(type)) {
this.listError.push({
key,
message: `type not found on ${key}`,
});
if (type === Self) {
this.validateRecusive(value, this.schema, keyParam, valueParam);
} else {
this.listError.push({
key,
message: `type not found on ${key}`,
});
}
} else {
// set value key same key in type
Object.keys(type).forEach((keyOfKey) => {
Expand Down
5 changes: 0 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ export function isArray(value) {
return Array.isArray(value);
}

// check is new Date()
export function isDate(value) {
return value instanceof Date;
}

export function isNaN(value) {
return typeof value === 'number' && Number.isNaN(value);
}
Expand Down
6 changes: 2 additions & 4 deletions test/array-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import {
Type,
string,
isOptional,
isArray,
isString,
isArrayElement,
} from '../src/type';

import {
isArray, isString,
} from '../src/util';

it('Test array', () => {
const Person = new Type({
name: string,
Expand Down
29 changes: 29 additions & 0 deletions test/class-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
Type,
isArray,
} from '../src/type';

it('Test array', () => {
class Person {
constructor(name) {
this.name = name;
}
}

const NewType = new Type({
bestFriend: Object,
age: Number,
myFriends: [Person],
});

const me = {
bestFriend: new Person('Hana'),
// eslint-disable-next-line no-new-wrappers
age: new Number(123),
myFriends: [new Person('John'), new Person('Peter')],
};

const errors = NewType.validate(me);
expect(isArray(errors)).toBe(true);
expect(errors.length).toBe(0);
});
5 changes: 1 addition & 4 deletions test/condition-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import {
length,
regex,
isOptional,
} from '../src/type';

import {
isArray,
} from '../src/util';
} from '../src/type';

it('Test condition in array function', () => {
// check age only in 10, 12 or 15
Expand Down
27 changes: 21 additions & 6 deletions test/error-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import {
string,
integer,
isArrayElement,
isArray,
isString,
} from '../src/type';

import {
isArray, isString,
} from '../src/util';

it('Test condition regex function error', () => {
const A = new Type([[isArrayElement], [isString]]);

Expand Down Expand Up @@ -116,19 +114,20 @@ it('Test assert function', () => {
});

const me = {
books: 1,
books: 1, // error type
};
expect(Person.assert.bind(Person, me)).toThrow(Error);
});

it('Test assert function success', () => {
const Person = new Type({
books: [string], // type not found
books: [string],
});

const me = {
books: ['Code complete'],
};
// not throw any errors
expect(Person.assert.bind(Person, me)).not.toThrow(Error);
});

Expand Down Expand Up @@ -199,3 +198,19 @@ it('Test empty type', () => {
expect(errors.length).toBe(1);
expect(errors[0].key).toBe('profile');
});

it('Test error class type', () => {
class Person {}

const NewType = new Type({
bestFriend: Person,
});

const me = {
bestFriend: 123,
};

const errors = NewType.validate(me);
expect(isArray(errors)).toBe(true);
expect(errors.length).toBe(1);
});
5 changes: 1 addition & 4 deletions test/object-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import {
string,
number,
optional,
} from '../src/type';

import {
isArray,
} from '../src/util';
} from '../src/type';

it('Test object', () => {
const Person = new Type({
Expand Down
15 changes: 1 addition & 14 deletions test/primitive-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import {
integer,
Null,
Undefined,
date,
func,
promise,
optional,
} from '../src/type';

import {
isArray,
} from '../src/util';
} from '../src/type';

it('Test string', () => {
const Pet = new Type(string);
Expand Down Expand Up @@ -79,15 +75,6 @@ it('Test Undefined', () => {
expect(errors.length).toBe(0);
});

it('Test Date', () => {
const A = new Type(date);
const a = new Date();

const errors = A.validate(a);
expect(isArray(errors)).toBe(true);
expect(errors.length).toBe(0);
});

it('Test func', () => {
const A = new Type(func);
const a = () => {};
Expand Down
Loading

0 comments on commit 1f1f64a

Please sign in to comment.