Skip to content

Commit

Permalink
feat(validator): update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm authored and alimd committed Jan 3, 2023
1 parent 9e577ca commit d49929f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/validator/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export type JsonSchema = {
};

export type ValidType = {
[key: string]: ValidType | string | number | boolean | null | undefined;
[key: string]: ValidType | string | number | boolean | null;
};
109 changes: 97 additions & 12 deletions demo/validator/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ import {validator} from '@alwatr/validator';
console.log('basic test');
console.log(
validator<
{num: number; str: string; bool: boolean; _null: null; undef: undefined; ali: 'ali'; five: 5; true: true}
{num: number; str: string; bool: boolean; _null: null; ali: 'ali'; five: 5; true: true}
>(
{num: Number, str: String, bool: Boolean, _null: null, undef: undefined, ali: 'ali', five: 5, true: true},
{num: 123, str: 'test', bool: false, _null: null, undef: undefined, ali: 'ali', five: 5, true: true},
{num: Number, str: String, bool: Boolean, _null: null, ali: 'ali', five: 5, true: true},
{num: 123, str: 'test', bool: false, _null: null, ali: 'ali', five: 5, true: true},
),
);

console.log('sanitize value test');
console.log(
validator<
{num: number; str: string; bool: boolean; _null: null; undef: undefined; ali: 'ali'; five: 5; true: true}
{num: number; str: string; bool: boolean; _null: null; ali: 'ali'; five: 5; true: true}
>(
{num: Number, str: String, bool: Boolean, _null: null, undef: undefined, ali: 'ali', five: 5, true: true},
{num: '123', str: 'test', bool: 'false', _null: null, undef: undefined, ali: 'ali', five: 5, true: true},
{num: Number, str: String, bool: Boolean, _null: null, ali: 'ali', five: 5, true: true},
{num: '123', str: 'test', bool: 'false', _null: null, ali: 'ali', five: 5, true: true},
),
);

console.log('nested value test');
console.log(
validator<
{a: {num: number; str: string; bool: boolean; _null: null; undef: undefined; ali: 'ali'; five: 5; true: true}}
{a: {num: number; str: string; bool: boolean; _null: null; ali: 'ali'; five: 5; true: true}}
>(
{a: {num: Number, str: String, bool: Boolean, _null: null, undef: undefined, ali: 'ali', five: 5, true: true}},
{a: {num: '123', str: 'test', bool: 'false', _null: null, undef: undefined, ali: 'ali', five: 5, true: true}},
{a: {num: Number, str: String, bool: Boolean, _null: null, ali: 'ali', five: 5, true: true}},
{a: {num: '123', str: 'test', bool: 'false', _null: null, ali: 'ali', five: 5, true: true}},
),
);

Expand All @@ -37,11 +37,96 @@ try {
{num: number}
>(
{num: Number},
{num: 'asd'},
{num: 'test'},
),
);
new Error('validator_not_work');
throw new Error('validator_not_work');
}
catch (err) {
console.log('test ok, error cause: ', (err as Error).cause);
if ((err as Error).message !== 'validator_not_work') {
console.log('test ok, error message `%s`, error cause: %s', (err as Error).message, (err as Error).cause);
}
else {
throw err;
}
}

try {
console.log(
validator<
{num: boolean}
>(
{num: Boolean},
{num: 'true'},
),
);
throw new Error('validator_not_work');
}
catch (err) {
if ((err as Error).message !== 'validator_not_work') {
console.log('test ok, error message `%s`, error cause: %s', (err as Error).message, (err as Error).cause);
}
else {
throw err;
}
}

try {
console.log(
validator<
{num: null}
>(
{num: null},
{num: 'test'},
),
);
throw new Error('validator_not_work');
}
catch (err) {
if ((err as Error).message !== 'validator_not_work') {
console.log('test ok, error message `%s`, error cause: %s', (err as Error).message, (err as Error).cause);
}
else {
throw err;
}
}

try {
console.log(
validator<
{num: number}
>(
{num: Number},
{num: 'test'},
),
);
throw new Error('validator_not_work');
}
catch (err) {
if ((err as Error).message !== 'validator_not_work') {
console.log('test ok, error message `%s`, error cause: %s', (err as Error).message, (err as Error).cause);
}
else {
throw err;
}
}

try {
console.log(
validator<
{num: string}
>(
{num: 'test'},
{num: 'tes'},
),
);
throw new Error('validator_not_work');
}
catch (err) {
if ((err as Error).message !== 'validator_not_work') {
console.log('test ok, error message `%s`, error cause: %s', (err as Error).message, (err as Error).cause);
}
else {
throw err;
}
}

0 comments on commit d49929f

Please sign in to comment.