Skip to content

Commit

Permalink
minor updates and distro
Browse files Browse the repository at this point in the history
  • Loading branch information
calvintwr committed Dec 9, 2021
1 parent 264d91c commit 9928334
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function simpleAPIChecks(stringPayload, arrayPayload) {

```ts
is(stringVar).not().boolean() // true

// is equivalent to

import { not } from 'issable'
Expand Down Expand Up @@ -81,5 +82,5 @@ import { define } from 'issable'
return true
},
})
})
```
65 changes: 62 additions & 3 deletions dist/browser/issable.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ var is = function is(candidate, name) {
_name: name,
throw: true,
operator: 'is',
exact: false,
_exact: false,
_statusCode: 400,
_msg: '',
_checker: function _checker(typing) {
try {
you_are_not_1.default.is(typing, this.candidate, this._name, this._msg);
} catch (err) {
if (this.throw === false) return false;
if (this.operator === 'not') return true;

this._errorModifier(err);
Expand All @@ -52,6 +53,7 @@ var is = function is(candidate, name) {
if (this.operator === 'is') {
return true;
} else if (this.operator === 'not') {
if (this.throw === false) return false;
var err = new you_are_not_1.default.NotTypeError("".concat(this._name, " is of type ").concat(typing, "."));

this._errorModifier(err);
Expand All @@ -61,44 +63,75 @@ var is = function is(candidate, name) {
throw Error('Fatal: Operator should only be `is` or `not`.');
}
},

/** Check for string. */
string: function string() {
return this._checker('string');
},

/** Check for `number`. NaN is not a `number`.*/
number: function number() {
return this._checker('number');
},

/** Check for `array`. `Array` will not be `object`. */
array: function array() {
return this._checker('array');
},

/** Check for `boolean`. */
boolean: function boolean() {
return this._checker('boolean');
},

/** Check for `function`. */
function: function _function() {
return this._checker('function');
},

/** Check for `null`. `null` will not be object. */
null: function _null() {
return this._checker('null');
},

/** Check for `undefined`. */
undefined: function undefined() {
return this._checker('undefined');
},

/** Check for `symbol`. */
symbol: function symbol() {
return this._checker('symbol');
},

/** Check for `NaN`. */
nan: function nan() {
return this._checker('nan');
},

/** Check for `undefined` and `null`. */
optional: function optional() {
return this._checker('optional');
},

/** Check for `number` that is an integer and not float. */
integer: function integer() {
return this._checker('integer');
},

/**
* Check for `object`. `Array` and `null` are not treated as object, and will fail this.
* @param schema [Optional] Provide a schema to check. For example, { name: 'John', age: 20 } will pass schema { name: 'string' , age: 'number'}
* For optionals in schema, denote with '?'. For e.g., { 'optionalKey?': 'boolean' }.
* If a value can be expected to be in multiple types, denote using an array: For e.g, { 'multipleTypes': ['string', 'number'] }
*/
object: function object(schema) {
if (typeof schema === 'undefined') {
try {
return you_are_not_1.default.is('object', this.candidate, this._name, this._msg);
} catch (err) {
if (this.throw === false) return false;

this._errorModifier(err);

throw err;
Expand All @@ -109,16 +142,21 @@ var is = function is(candidate, name) {
you_are_not_1.default.is('object', this.candidate);
you_are_not_1.default.is('object', schema);
} catch (err) {
// TODO: modify
this._errorModifier(err);

err.statusCode = 500;
err.message = 'Internal error: Candidate or schema is not object.';
throw err;
}

if (this.operator === 'is') {
var result;

try {
result = you_are_not_1.default.scrub(name || 'Anonymous Object', schema, candidate, this.exact);
result = you_are_not_1.default.scrub(name || 'Anonymous Object', schema, candidate, this._exact);
} catch (err) {
if (this.throw === false) return false;

this._errorModifier(err);

throw err;
Expand All @@ -136,32 +174,53 @@ var is = function is(candidate, name) {
return this.candidate;
}

if (this.throw === false) return false;
var err = new you_are_not_1.default.NotTypeError("Payload is not the same as defined schema for ".concat(this._name));

this._errorModifier(err);

throw err;
}
},

/** Custom checks
* @param typing The name of the custom check defined.
*/
custom: function custom(typing) {
return this._checker(typing);
},

/** Disable throwing of error. Will return false instead. */
safe: function safe() {
this.throw = false;
return this;
},

/** Exact match for `object` check. */
exact: function exact() {
this._exact = true;
return this;
},

/** Status code for API response. Will be returned in the `statusCode` property of the Error object thrown. */
statusCode: function statusCode(code) {
this._statusCode = code;
return this;
},

/** Another syntax method. E.g, is(someVar).not().number() */
not: function not() {
this.operator = 'not';
return this;
},

/** Custom message if error is thrown. */
msg: function msg(errorMessage) {
this._msg = errorMessage;
return this;
},

/** Name of parameter. An alternate way: is(someVar, 'fooPayLoad') is equivalent to is(someVar).name('fooPayload') */
name: function name(_name) {
this._name = _name;
return this;
Expand Down
2 changes: 1 addition & 1 deletion dist/browser/issable.min.js

Large diffs are not rendered by default.

58 changes: 56 additions & 2 deletions dist/node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,54 @@ declare const is: (candidate: any, name?: string) => {
_name: string;
throw: boolean;
operator: string;
exact: boolean;
_exact: boolean;
_statusCode: number;
_msg: string;
_checker(typing: string): boolean;
/** Check for string. */
string(): boolean;
/** Check for `number`. NaN is not a `number`.*/
number(): boolean;
/** Check for `array`. `Array` will not be `object`. */
array(): boolean;
/** Check for `boolean`. */
boolean(): boolean;
/** Check for `function`. */
function(): boolean;
/** Check for `null`. `null` will not be object. */
null(): boolean;
/** Check for `undefined`. */
undefined(): boolean;
/** Check for `symbol`. */
symbol(): boolean;
/** Check for `NaN`. */
nan(): boolean;
/** Check for `undefined` and `null`. */
optional(): boolean;
/** Check for `number` that is an integer and not float. */
integer(): boolean;
/**
* Check for `object`. `Array` and `null` are not treated as object, and will fail this.
* @param schema [Optional] Provide a schema to check. For example, { name: 'John', age: 20 } will pass schema { name: 'string' , age: 'number'}
* For optionals in schema, denote with '?'. For e.g., { 'optionalKey?': 'boolean' }.
* If a value can be expected to be in multiple types, denote using an array: For e.g, { 'multipleTypes': ['string', 'number'] }
*/
object(schema?: object | undefined): any;
/** Custom checks
* @param typing The name of the custom check defined.
*/
custom(typing: string): boolean;
/** Disable throwing of error. Will return false instead. */
safe(): typeof this;
/** Exact match for `object` check. */
exact(): typeof this;
/** Status code for API response. Will be returned in the `statusCode` property of the Error object thrown. */
statusCode(code: number): typeof this;
/** Another syntax method. E.g, is(someVar).not().number() */
not(): typeof this;
/** Custom message if error is thrown. */
msg(errorMessage: string): any;
/** Name of parameter. An alternate way: is(someVar, 'fooPayLoad') is equivalent to is(someVar).name('fooPayload') */
name(name: string): any;
_errorModifier(error: NotTypeError): NotTypeError;
};
Expand All @@ -43,27 +70,54 @@ declare const not: (candidate: any, name?: string | undefined) => {
_name: string;
throw: boolean;
operator: string;
exact: boolean;
_exact: boolean;
_statusCode: number;
_msg: string;
_checker(typing: string): boolean;
/** Check for string. */
string(): boolean;
/** Check for `number`. NaN is not a `number`.*/
number(): boolean;
/** Check for `array`. `Array` will not be `object`. */
array(): boolean;
/** Check for `boolean`. */
boolean(): boolean;
/** Check for `function`. */
function(): boolean;
/** Check for `null`. `null` will not be object. */
null(): boolean;
/** Check for `undefined`. */
undefined(): boolean;
/** Check for `symbol`. */
symbol(): boolean;
/** Check for `NaN`. */
nan(): boolean;
/** Check for `undefined` and `null`. */
optional(): boolean;
/** Check for `number` that is an integer and not float. */
integer(): boolean;
/**
* Check for `object`. `Array` and `null` are not treated as object, and will fail this.
* @param schema [Optional] Provide a schema to check. For example, { name: 'John', age: 20 } will pass schema { name: 'string' , age: 'number'}
* For optionals in schema, denote with '?'. For e.g., { 'optionalKey?': 'boolean' }.
* If a value can be expected to be in multiple types, denote using an array: For e.g, { 'multipleTypes': ['string', 'number'] }
*/
object(schema?: object | undefined): any;
/** Custom checks
* @param typing The name of the custom check defined.
*/
custom(typing: string): boolean;
/** Disable throwing of error. Will return false instead. */
safe(): typeof this;
/** Exact match for `object` check. */
exact(): typeof this;
/** Status code for API response. Will be returned in the `statusCode` property of the Error object thrown. */
statusCode(code: number): typeof this;
/** Another syntax method. E.g, is(someVar).not().number() */
not(): typeof this;
/** Custom message if error is thrown. */
msg(errorMessage: string): any;
/** Name of parameter. An alternate way: is(someVar, 'fooPayLoad') is equivalent to is(someVar).name('fooPayload') */
name(name: string): any;
_errorModifier(error: NotTypeError): NotTypeError;
};
Expand Down
2 changes: 1 addition & 1 deletion dist/node/index.d.ts.map

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

Loading

0 comments on commit 9928334

Please sign in to comment.