Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typit

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typit - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

.d.ts/object-type-definition-util.d.ts

2

.d.ts/array-type.d.ts
import { Type } from "./type";
export declare class ArrayType<E = any> extends Type<E[]> {
protected arrayType: Type;
constructor(arrayType?: Type<E>, isOptional?: boolean);
constructor(arrayType?: Type<E>);
getTypeName(): string;

@@ -6,0 +6,0 @@ checkConformity(input: any): boolean;

@@ -5,3 +5,3 @@ import { Type } from "./type";

protected acceptableValues: E[];
constructor(acceptableValues: E[], typeName?: string, isOptional?: boolean);
constructor(acceptableValues: E[], typeName?: string);
getTypeName(): string;

@@ -8,0 +8,0 @@ checkConformity(input: any): boolean;

import { ObjectType } from "./object-type";
export declare class IntersectionType extends ObjectType {
constructor(types: ObjectType[], isOptional?: boolean);
constructor(types: ObjectType[]);
}

@@ -7,4 +7,6 @@ export { Type } from "./type";

export { ObjectTypeDefinition } from "./object-type-definition";
export { ObjectTypeDefinitionUtil } from "./object-type-definition-util";
export { EnumType } from "./enum-type";
export { OptionalType } from "./optional-type";
export { IntersectionType, IntersectionType as MultiType, IntersectionType as AndType } from "./intersection-type";
export { UnionType, UnionType as OrType } from "./union-type";
import { Type } from "./type";
import { ObjectTypeDefinition } from "./object-type-definition";
import { MalformedObjectError } from "./malformed-object-error";
export declare class ObjectType extends Type {
protected typeName: string | undefined;
protected typeName: string;
protected typeDefinition: ObjectTypeDefinition;
constructor(typeDefinition?: ObjectTypeDefinition, typeName?: string, isOptional?: boolean);
static typeDefinitionToReadableJSON(typeDefinition: ObjectTypeDefinition): any;
static typeDefinitionToString(typeDefinition: ObjectTypeDefinition): string;
typeDefinitionToReadableJSON(): any;
typeDefinitionToString(): string;
constructor(typeDefinition?: ObjectTypeDefinition, typeName?: string);
getTypeName(): string;
getObjectTypeDefinition(): ObjectTypeDefinition;
checkConformity(input: any, typeDefinition?: ObjectTypeDefinition): boolean;
exhaustivelyCheckConformity(input: any, typeDefinition?: ObjectTypeDefinition): boolean;
getObjectTypeDefinition(): ObjectTypeDefinition;
listNonConformities(input: any, typeDefinition?: ObjectTypeDefinition): MalformedObjectError[];
}

@@ -5,12 +5,9 @@ import { Type } from "./type";

static readonly ANY: SpecialType<any>;
static readonly OPTIONAL_ANY: SpecialType<any>;
static readonly VOID: SpecialType<void>;
static readonly OPTIONAL_VOID: SpecialType<void>;
static readonly UNDEFINED: SpecialType<undefined>;
static readonly OPTIONAL_UNDEFINED: SpecialType<undefined>;
static readonly NULL: SpecialType<null>;
static readonly OPTIONAL_NULL: SpecialType<null>;
static readonly NON_PRESENT: SpecialType<null>;
protected typeName: string;
protected validator: Validator;
protected constructor(name: string, isOptional: boolean, validator: Validator);
protected constructor(name: string, validator: Validator);
getTypeName(): string;

@@ -17,0 +14,0 @@ checkConformity(input: any): boolean;

@@ -5,10 +5,7 @@ import { Type } from "./type";

static readonly NUMBER: StandardType<number>;
static readonly OPTIONAL_NUMBER: StandardType<number>;
static readonly BOOLEAN: StandardType<boolean>;
static readonly OPTIONAL_BOOLEAN: StandardType<boolean>;
static readonly STRING: StandardType<string>;
static readonly OPTIONAL_STRING: StandardType<string>;
protected typeName: string;
protected validator: Validator;
protected constructor(name: string, isOptional: boolean, validator: Validator);
protected constructor(name: string, validator: Validator);
getTypeName(): string;

@@ -15,0 +12,0 @@ checkConformity(input: any): boolean;

export declare abstract class Type<E = any> {
protected optional: boolean;
protected constructor(isOptional: boolean);
protected constructor();
isOptional(): boolean;
abstract getTypeName(): string;
abstract checkConformity(input: any): boolean;
checkConformityOfAll(...input: any[]): boolean;
abstract exhaustivelyCheckConformity(input: any): boolean;
exhaustivelyCheckConformityOfAll(...input: any[]): boolean;
sanitize(input: any): E;
}

@@ -5,3 +5,3 @@ import { Type } from "./type";

protected acceptableTypes: Type[];
constructor(types: Type[], isOptional?: boolean);
constructor(types: Type[]);
getTypeName(): string;

@@ -8,0 +8,0 @@ checkConformity(input: any): boolean;

@@ -27,3 +27,3 @@ // For a detailed explanation regarding each configuration property, visit:

// The directory where Jest should output its coverage files
coverageDirectory: "ts/test/coverage",
coverageDirectory: "ts/tests/coverage",

@@ -117,3 +117,3 @@ // An array of regexp pattern strings used to skip coverage collection

roots: [
"<rootDir>/js/test"
"<rootDir>/js/tests"
],

@@ -120,0 +120,0 @@

@@ -6,4 +6,4 @@ "use strict";

class ArrayType extends type_1.Type {
constructor(arrayType = special_type_1.SpecialType.ANY, isOptional = false) {
super(isOptional);
constructor(arrayType = special_type_1.SpecialType.ANY) {
super();
this.arrayType = arrayType;

@@ -10,0 +10,0 @@ }

@@ -5,4 +5,4 @@ "use strict";

class EnumType extends type_1.Type {
constructor(acceptableValues, typeName, isOptional = false) {
super(isOptional);
constructor(acceptableValues, typeName) {
super();
this.acceptableValues = acceptableValues;

@@ -9,0 +9,0 @@ if (typeName !== undefined)

@@ -13,4 +13,4 @@ "use strict";

class IntersectionType extends object_type_1.ObjectType {
constructor(types, isOptional = false) {
super(Merj.merge(types.map((type) => type.getObjectTypeDefinition())), "", isOptional);
constructor(types) {
super(Merj.merge(types.map((type) => type.getObjectTypeDefinition())), "");
this.typeName = "";

@@ -17,0 +17,0 @@ for (let type of types) {

@@ -13,4 +13,8 @@ "use strict";

exports.ObjectType = object_type_1.ObjectType;
var object_type_definition_util_1 = require("./object-type-definition-util");
exports.ObjectTypeDefinitionUtil = object_type_definition_util_1.ObjectTypeDefinitionUtil;
var enum_type_1 = require("./enum-type");
exports.EnumType = enum_type_1.EnumType;
var optional_type_1 = require("./optional-type");
exports.OptionalType = optional_type_1.OptionalType;
var intersection_type_1 = require("./intersection-type");

@@ -17,0 +21,0 @@ exports.IntersectionType = intersection_type_1.IntersectionType;

@@ -0,1 +1,30 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const type_inferer_1 = require("./type-inferer");
class MalformedObjectError extends Error {
constructor(path, expectedType, actualValue, actualType = type_inferer_1.TypeInferer.infer(actualValue)) {
super();
this.path = path;
this.readablePath = "*";
this.expectedType = expectedType;
this.actualType = actualType;
this.actualValue = actualValue;
for (let pathItem of path)
this.readablePath += "." + pathItem;
}
prependPath(...path) {
path.push(...this.path);
return new MalformedObjectError(path, this.expectedType, this.actualValue);
}
toJSON() {
return {
path: this.path,
readablePath: this.readablePath,
expectedType: this.expectedType,
actualType: this.actualType,
actualValue: this.actualValue
};
}
}
exports.MalformedObjectError = MalformedObjectError;
//# sourceMappingURL=malformed-object-error.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const type_1 = require("./type");
const malformed_object_error_1 = require("./malformed-object-error");
const iter_over_1 = require("iter-over");
const type_1 = require("./type");
const special_type_1 = require("./special-type");
class ObjectType extends type_1.Type {
constructor(typeDefinition = {}, typeName, isOptional = false) {
super(isOptional);
constructor(typeDefinition = {}, typeName = "object") {
super();
this.typeDefinition = typeDefinition;
this.typeName = typeName;
}
static typeDefinitionToReadableJSON(typeDefinition) {
let iterator = new iter_over_1.ObjectIterator(typeDefinition);
let result = {};
for (let element of iterator) {
if (element === undefined)
throw new Error("ERR | Attempted to use an undefined type definition.");
if (element.value.getTypeName !== undefined)
result[element.key] = element.value.getTypeName();
else
result[element.key] = ObjectType.typeDefinitionToReadableJSON(element.value);
}
return result;
}
static typeDefinitionToString(typeDefinition) {
let structureToStringArray = (struct) => {
let iterator = new iter_over_1.ObjectIterator(struct);
let resultLines = [];
for (let element of iterator) {
if (element === undefined)
throw new Error("ERR | Attempted to use an undefined type definition.");
if (element.value.getTypeName !== undefined) {
let typeName = element.value.getTypeName();
resultLines.push("| " + element.key + ": " + typeName);
}
else {
resultLines.push("| " + element.key + ": object \u23ce");
let nestedResultLines = structureToStringArray(element.value);
for (let line of nestedResultLines)
resultLines.push("| " + line);
}
}
return resultLines;
};
return structureToStringArray(typeDefinition).join("\n");
}
typeDefinitionToReadableJSON() {
return ObjectType.typeDefinitionToReadableJSON(this.typeDefinition);
}
typeDefinitionToString() {
return ObjectType.typeDefinitionToString(this.typeDefinition);
}
getTypeName() {
return "object" + (this.typeName !== undefined ? " (" + this.typeName + ")" : "");
return this.typeName;
}
getObjectTypeDefinition() {
return this.typeDefinition;
}
checkConformity(input, typeDefinition = this.typeDefinition) {

@@ -103,4 +61,37 @@ if (!(typeof input === "object") || (input === null))

}
getObjectTypeDefinition() {
return this.typeDefinition;
}
listNonConformities(input, typeDefinition = this.typeDefinition) {
let nonConformities = [];
if (!(typeof input === "object") || (input === null)) {
nonConformities.push(new malformed_object_error_1.MalformedObjectError([], new ObjectType(), input));
}
else {
let iterator = new iter_over_1.ObjectIterator(typeDefinition);
for (let typeOrTypeDefinition of iterator) {
let typePropertyName = typeOrTypeDefinition.key;
if (typeOrTypeDefinition.value.getTypeName !== undefined) {
let type = typeOrTypeDefinition.value;
if (input.hasOwnProperty(typePropertyName)) {
if (!type.checkConformity(input[typePropertyName])) {
nonConformities.push(new malformed_object_error_1.MalformedObjectError([typePropertyName], type, input[typePropertyName]));
}
}
else if (!type.isOptional()) {
nonConformities.push(new malformed_object_error_1.MalformedObjectError([typePropertyName], type, input[typePropertyName], special_type_1.SpecialType.NON_PRESENT));
}
}
else {
let nestedNonConformities = this.listNonConformities(input[typePropertyName], typeOrTypeDefinition.value);
for (let nestedNonConformity of nestedNonConformities) {
nonConformities.push(nestedNonConformity.prependPath(typePropertyName));
}
}
}
}
return nonConformities;
}
}
exports.ObjectType = ObjectType;
//# sourceMappingURL=object-type.js.map

@@ -5,4 +5,4 @@ "use strict";

class SpecialType extends type_1.Type {
constructor(name, isOptional, validator) {
super(isOptional);
constructor(name, validator) {
super();
this.typeName = name;

@@ -21,11 +21,8 @@ this.validator = validator;

}
SpecialType.ANY = new SpecialType("any", false, (input) => true);
SpecialType.OPTIONAL_ANY = new SpecialType("any", true, (input) => true);
SpecialType.VOID = new SpecialType("void", false, (input) => false);
SpecialType.OPTIONAL_VOID = new SpecialType("void", true, (input) => false);
SpecialType.UNDEFINED = new SpecialType("undefined", false, (input) => input === undefined);
SpecialType.OPTIONAL_UNDEFINED = new SpecialType("undefined", true, (input) => input === undefined);
SpecialType.NULL = new SpecialType("null", false, (input) => input === null);
SpecialType.OPTIONAL_NULL = new SpecialType("null", true, (input) => input === null);
SpecialType.ANY = new SpecialType("any", (input) => true);
SpecialType.VOID = new SpecialType("void", (input) => false);
SpecialType.UNDEFINED = new SpecialType("undefined", (input) => input === undefined);
SpecialType.NULL = new SpecialType("null", (input) => input === null);
SpecialType.NON_PRESENT = new SpecialType("non-present", (input) => false);
exports.SpecialType = SpecialType;
//# sourceMappingURL=special-type.js.map

@@ -5,4 +5,4 @@ "use strict";

class StandardType extends type_1.Type {
constructor(name, isOptional, validator) {
super(isOptional);
constructor(name, validator) {
super();
this.typeName = name;

@@ -21,9 +21,6 @@ this.validator = validator;

}
StandardType.NUMBER = new StandardType("number", false, (input) => (typeof input === "number"));
StandardType.OPTIONAL_NUMBER = new StandardType("number", true, (input) => (typeof input === "number"));
StandardType.BOOLEAN = new StandardType("boolean", false, (input) => (typeof input === "boolean"));
StandardType.OPTIONAL_BOOLEAN = new StandardType("boolean", true, (input) => (typeof input === "boolean"));
StandardType.STRING = new StandardType("string", false, (input) => (typeof input === "string"));
StandardType.OPTIONAL_STRING = new StandardType("string", true, (input) => (typeof input === "string"));
StandardType.NUMBER = new StandardType("number", (input) => (typeof input === "number"));
StandardType.BOOLEAN = new StandardType("boolean", (input) => (typeof input === "boolean"));
StandardType.STRING = new StandardType("string", (input) => (typeof input === "string"));
exports.StandardType = StandardType;
//# sourceMappingURL=standard-type.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Type {
constructor(isOptional) {
this.optional = isOptional;
}
constructor() { }
isOptional() {
return this.optional;
return false;
}
checkConformityOfAll(...input) {
return input.every((item) => this.checkConformity(item));
}
exhaustivelyCheckConformityOfAll(...input) {
return input.every((item) => this.exhaustivelyCheckConformity(item));
}
sanitize(input) {

@@ -14,3 +18,3 @@ if (this.checkConformity(input))

else {
throw new Error("ERR | Attempted to sanitize content that did not match the desired type signature. " +
throw new TypeError("ERR | Attempted to sanitize content that did not match the desired type signature. " +
"Type of " + input.toString() + "was not '" + this.getTypeName() + "'");

@@ -17,0 +21,0 @@ }

@@ -5,4 +5,4 @@ "use strict";

class UnionType extends type_1.Type {
constructor(types, isOptional = false) {
super(isOptional);
constructor(types) {
super();
this.acceptableTypes = types;

@@ -9,0 +9,0 @@ this.typeName = "";

{
"name": "typit",
"version": "0.5.0",
"version": "0.6.0",
"description": "Fully recursive runtime typechecking.",

@@ -30,3 +30,3 @@ "main": "js/main",

"dependencies": {
"iter-over": "^1.1.2",
"iter-over": "^1.1.3",
"merj": "^0.1.0"

@@ -33,0 +33,0 @@ },

@@ -14,3 +14,3 @@ /*

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @version v0.6.0
* @since v0.1.0

@@ -28,3 +28,3 @@ */

/**
* Initializes a new ArrayType with a given member type and optionality.
* Initializes a new ArrayType with a given member type.
*

@@ -35,7 +35,6 @@ * The `arrayType` parameter defaults to `SpecialType.ANY` so that if no arguments are passed to this constructor,

* @param arrayType The type of the members of the array.
* @param isOptional Whether or not this type should be optional when present in an {@link ObjectType}.
*/
public constructor(arrayType: Type<E> = SpecialType.ANY, isOptional: boolean = false) {
public constructor(arrayType: Type<E> = SpecialType.ANY) {
super(isOptional);
super();

@@ -58,6 +57,8 @@ this.arrayType = arrayType;

/**
* Checks that the provided input is an array, and that the array's contents conform to the given type of this
* ArrayType.
* Returns true if and only if the provided input is an array, and the array's contents conform to the given type of
* this ArrayType.
*
* @param input Any variable to check for conformity to this ArrayType.
* @return true if and only if the provided input is an array, and the array's contents conform to the given type of
* this ArrayType.
*/

@@ -88,2 +89,4 @@ public checkConformity(input: any): boolean {

* @param input Any variable to exhaustively check for conformity to this ArrayType.
* @return true if and only if the provided input is an array, and the array's contents conform to the given type of
* this ArrayType.
*/

@@ -90,0 +93,0 @@ public exhaustivelyCheckConformity(input: any): boolean {

@@ -13,3 +13,3 @@ /*

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @version v0.6.0
* @since v0.4.0

@@ -34,4 +34,3 @@ */

/**
* Initializes a new EnumType with the provided list of acceptable values, an optional type name, and a given
* optionality.
* Initializes a new EnumType with the provided list of acceptable values and an optional type name.
*

@@ -43,7 +42,6 @@ * If a type name is not provided, the type name will be set simply to "Enum".

* @param typeName An optional name for this EnumType.
* @param isOptional Whether or not this type should be optional when present in an {@link ObjectType}.
*/
public constructor(acceptableValues: E[], typeName?: string, isOptional: boolean = false) {
public constructor(acceptableValues: E[], typeName?: string) {
super(isOptional);
super();

@@ -50,0 +48,0 @@ this.acceptableValues = acceptableValues;

@@ -34,3 +34,3 @@ /*

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @version v0.6.0
* @since v0.4.0

@@ -40,6 +40,10 @@ */

// DOC-ME [6/15/19 @ 7:13 PM] - Documentation required!
public constructor(types: ObjectType[], isOptional: boolean = false) {
/**
* Initializes a new IntersectionType with the provided ObjectTypes.
*
* @param types The {@link Type}s to merge in order to form the intersection of said Types.
*/
public constructor(types: ObjectType[]) {
super(Merj.merge(types.map((type: ObjectType) => type.getObjectTypeDefinition())), "", isOptional);
super(Merj.merge(types.map((type: ObjectType) => type.getObjectTypeDefinition())), "");

@@ -46,0 +50,0 @@ this.typeName = "";

@@ -11,3 +11,3 @@ /*

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.3.0
* @version v0.6.0
* @since v0.2.0

@@ -22,3 +22,5 @@ */

export { ObjectTypeDefinition } from "./object-type-definition";
export { ObjectTypeDefinitionUtil } from "./object-type-definition-util";
export { EnumType } from "./enum-type";
export { OptionalType } from "./optional-type";

@@ -25,0 +27,0 @@ export {

@@ -7,3 +7,4 @@ /*

// import { Type } from "./type";
import { Type } from "./type";
import { TypeInferer } from "./type-inferer";

@@ -15,29 +16,88 @@ /**

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @since v0.5.0
* @version v0.6.0
* @since v0.6.0
*/
// export class MalformedObjectError extends Error {
//
// // DOC-ME [6/15/19 @ 7:13 PM] - Documentation required!
// public readonly path: string[];
//
// // DOC-ME [6/15/19 @ 7:13 PM] - Documentation required!
// public readonly readablePath: string;
//
// // DOC-ME [6/15/19 @ 7:13 PM] - Documentation required!
// public readonly expectedType: Type;
//
// // DOC-ME [6/15/19 @ 7:13 PM] - Documentation required!
// public readonly actualType: Type;
//
// // DOC-ME [6/15/19 @ 7:13 PM] - Documentation required!
// public readonly actualValue: any;
//
// // DOC-ME [6/15/19 @ 7:13 PM] - Documentation required!
// public constructor(path: string[], expectedType: Type, actualValue: any) {
//
// super();
//
// }
//
// }
export class MalformedObjectError extends Error {
/**
* The object path at which the non-conforming value was found.
*/
public readonly path: string[];
/**
* The human-readable object path at which the non-conforming value was found.
*/
public readonly readablePath: string;
/**
* The {@link Type} that was expected of the received value.
*/
public readonly expectedType: Type;
/**
* The actual type of the value that was received.
*/
public readonly actualType: Type;
/**
* The actual value received that failed to conform to the expected {@link Type}.
*/
public readonly actualValue: any;
/**
* Initializes a new MalformedObjectError with an error path, expected {@link Type}, and the actual value received.
*
* @param path The object path at which the non-conforming value was found.
* @param expectedType The Type that was expected of the provided value.
* @param actualValue The actual value that was received that failed to conform to the expected Type.
* @param actualType An optional actual type of the received value if it is already known.
*/
public constructor(path: string[], expectedType: Type, actualValue: any, actualType: Type = TypeInferer.infer(actualValue)) {
super();
this.path = path;
this.readablePath = "*";
this.expectedType = expectedType;
this.actualType = actualType
this.actualValue = actualValue;
for (let pathItem of path) this.readablePath += "." + pathItem;
}
/**
* Creates and returns a new MalformedObjectError with the provided path prepended to it.
*
* This method does not modify the existing MalformedObjectError, merely creates a new one.
*
* @param path The path to prepend onto the beginning of this MalformedObjectError's path.
*/
public prependPath(...path: string[]): MalformedObjectError {
path.push(...this.path);
return new MalformedObjectError(path, this.expectedType, this.actualValue);
}
/**
* Returns the non-error content of this MalformedObjectError as a JSON object.
*
* @return The non-error content of this MalformedObjectError as a JSON object.
*/
public toJSON(): any {
return {
path: this.path,
readablePath: this.readablePath,
expectedType: this.expectedType,
actualType: this.actualType,
actualValue: this.actualValue
};
}
}

@@ -10,6 +10,6 @@ /*

/**
* TODO
* An interface defining the outline by which complex object types can be typechecked.
*
* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.3.0
* @version v0.6.0
* @since v0.1.0

@@ -19,5 +19,7 @@ */

// DOC-ME [6/15/19 @ 7:13 PM] - Documentation required!
/**
* Each property must either be a checkable {@link Type} or a recursing ObjectTypeDefinition.
*/
readonly [property: string]: Type | ObjectTypeDefinition;
}

@@ -7,5 +7,7 @@ /*

import { ObjectIterator } from "iter-over";
import { Type } from "./type";
import { ObjectTypeDefinition } from "./object-type-definition";
import { MalformedObjectError } from "./malformed-object-error";
import { ObjectIterator } from "iter-over";
import { SpecialType } from "./special-type";

@@ -16,3 +18,3 @@ /**

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @version v0.6.0
* @since v0.1.0

@@ -25,3 +27,3 @@ */

*/
protected typeName: string | undefined;
protected typeName: string;

@@ -34,16 +36,16 @@ /**

/**
* Initializes a new ObjectType with a given {@link ObjectTypeDefinition}, type name, and optionality.
* Initializes a new ObjectType with a given {@link ObjectTypeDefinition} and type name.
*
* The type definition argument is optional, and if not passed will default to an empty type definition, meaning
* that any provided inputs will succeed given that they are objects of any form.
* that any provided inputs will succeed given that they are objects of any form. Keep in mind that this includes
* arrays.
*
* The type name is optional as well and
* The type name is optional as well and if not provided will default to "object".
*
* @param typeDefinition
* @param typeName
* @param isOptional
* @param typeDefinition The ObjectTypeDefinition to use to check the conformity of provided inputs.
* @param typeName The name to use for this ObjectType. Defaults to "object".
*/
public constructor(typeDefinition: ObjectTypeDefinition = {}, typeName?: string, isOptional: boolean = false) {
public constructor(typeDefinition: ObjectTypeDefinition = {}, typeName: string = "object") {
super(isOptional);
super();

@@ -55,73 +57,2 @@ this.typeDefinition = typeDefinition;

// DOC-ME [6/15/19 @ 5:53 PM] - Documentation required!
public static typeDefinitionToReadableJSON(typeDefinition: ObjectTypeDefinition): any {
let iterator: ObjectIterator<Type | ObjectTypeDefinition> = new ObjectIterator<Type | ObjectTypeDefinition>(typeDefinition);
let result: any = {};
for (let element of iterator) {
if (element === undefined) throw new Error("ERR | Attempted to use an undefined type definition.");
if (element.value.getTypeName !== undefined) result[element.key] = (element.value as Type).getTypeName();
else result[element.key] = ObjectType.typeDefinitionToReadableJSON(element.value as ObjectTypeDefinition);
}
return result;
}
// DOC-ME [6/15/19 @ 5:53 PM] - Documentation required!
public static typeDefinitionToString(typeDefinition: ObjectTypeDefinition): string {
let structureToStringArray: (struct: ObjectTypeDefinition) => string[] = (struct: ObjectTypeDefinition): string[] => {
let iterator: ObjectIterator<Type | ObjectTypeDefinition> = new ObjectIterator<Type | ObjectTypeDefinition>(struct);
let resultLines: string[] = [];
for (let element of iterator) {
if (element === undefined) throw new Error("ERR | Attempted to use an undefined type definition.");
if (element.value.getTypeName !== undefined) {
let typeName: string = (element.value as Type).getTypeName();
resultLines.push("| " + element.key + ": " + typeName);
} else {
resultLines.push("| " + element.key + ": object \u23ce");
let nestedResultLines: string[] = structureToStringArray(element.value as ObjectTypeDefinition);
for (let line of nestedResultLines) resultLines.push("| " + line);
}
}
return resultLines;
};
return structureToStringArray(typeDefinition).join("\n");
}
// DOC-ME [6/15/19 @ 5:53 PM] - Documentation required!
public typeDefinitionToReadableJSON(): any {
return ObjectType.typeDefinitionToReadableJSON(this.typeDefinition);
}
// DOC-ME [6/15/19 @ 5:53 PM] - Documentation required!
public typeDefinitionToString(): string {
return ObjectType.typeDefinitionToString(this.typeDefinition);
}
/**

@@ -134,17 +65,13 @@ * Returns the string name of this ObjectType.

return "object" + (this.typeName !== undefined ? " (" + this.typeName + ")" : "");
return this.typeName;
}
// DOC-ME [6/15/19 @ 5:53 PM] - Documentation required!
public getObjectTypeDefinition(): ObjectTypeDefinition {
return this.typeDefinition;
}
/**
* TODO
* Returns true if and only if the provided input conforms to this ObjectTypes's {@link ObjectTypeDefinition}.
*
* @param input Any variable to check for conformity to this ObjectType.
* @param typeDefinition The type definition to use to check the given input. This is used for recursing into
* objects, most outside uses will not pass an argument for this parameter.
* @return true if and only if the provided input conforms to this ObjectTypes's ObjectTypeDefinition.
*/

@@ -189,5 +116,12 @@ public checkConformity(input: any, typeDefinition: ObjectTypeDefinition = this.typeDefinition): boolean {

/**
* TODO
* Returns true if and only if the provided input conforms to this ObjectTypes's {@link ObjectTypeDefinition} and
* contains no extra properties.
*
* In other words, the provided input must bi-directionally match the ObjectTypeDefinition.
*
* @param input Any variable to exhaustively check for conformity to this ObjectType.
* @param typeDefinition The type definition to use to check the given input. This is used for recursing into
* objects, most outside uses will not pass an argument for this parameter.
* @return true if and only if the provided input conforms to this ObjectTypes's ObjectTypeDefinition and contains
* no extra values.
*/

@@ -227,2 +161,86 @@ public exhaustivelyCheckConformity(input: any, typeDefinition: ObjectTypeDefinition = this.typeDefinition): boolean {

/**
* Returns the {@link ObjectTypeDefinition} that this ObjectType uses to check passed inputs.
*
* @return The ObjectTypeDefinition that this ObjectType uses to check passed inputs.
*/
public getObjectTypeDefinition(): ObjectTypeDefinition {
return this.typeDefinition;
}
/**
* Generates and returns an array of {@link MalformedObjectError}s based on the input given and this ObjectType's
* {@link ObjectTypeDefinition}.
*
* @param input Any variable for which to generate a list of non-conformities.
* @param typeDefinition The type definition to use to check the given input. This is used for recursing into
* objects, most outside uses will not pass an argument for this parameter.
* @return An array of MalformedObjectErrors containing all of the non-conformities found in the provided
* input.
*/
public listNonConformities(input: any, typeDefinition: ObjectTypeDefinition = this.typeDefinition): MalformedObjectError[] {
let nonConformities: MalformedObjectError[] = [];
if (!(typeof input === "object") || (input === null)) {
// The base object wasn't an object, report on the base object and return.
nonConformities.push(new MalformedObjectError([], new ObjectType(), input));
} else {
let iterator: ObjectIterator<Type | ObjectTypeDefinition> = new ObjectIterator<Type | ObjectTypeDefinition>(typeDefinition);
for (let typeOrTypeDefinition of iterator) {
let typePropertyName: string = typeOrTypeDefinition.key;
// We're dealing with a Type, not an ObjectTypeDefinition.
if (typeOrTypeDefinition.value.getTypeName !== undefined) {
let type: Type = typeOrTypeDefinition.value as Type;
// If the provided input has a property by the name defined in the type definition...
if (input.hasOwnProperty(typePropertyName)) {
// If the input's equivalent property does not conform to the type definition of said property...
if (!type.checkConformity(input[typePropertyName])) {
nonConformities.push(new MalformedObjectError([typePropertyName], type, input[typePropertyName]));
}
// If the value/type was not optionally defined...
} else if (!type.isOptional()) {
nonConformities.push(new MalformedObjectError([typePropertyName], type, input[typePropertyName], SpecialType.NON_PRESENT));
}
// We're dealing with a ObjectTypeDefinition, not an Type.
} else {
let nestedNonConformities: MalformedObjectError[] =
this.listNonConformities(input[typePropertyName], typeOrTypeDefinition.value as ObjectTypeDefinition);
for (let nestedNonConformity of nestedNonConformities) {
nonConformities.push(nestedNonConformity.prependPath(typePropertyName));
}
}
}
}
return nonConformities;
}
// TODO [6/15/19 @ 11:55 PM] - Add an 'exhaustivelyListNonConformities' method.
}

@@ -15,3 +15,3 @@ /*

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @version v0.6.0
* @since v0.1.0

@@ -21,56 +21,40 @@ */

// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
/**
* The 'any' type, to which every value conforms.
*/
public static readonly ANY: SpecialType<any> = new SpecialType(
"any",
false,
(input: any): boolean => true
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
public static readonly OPTIONAL_ANY: SpecialType<any> = new SpecialType(
"any",
true,
(input: any): boolean => true
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
/**
* The 'void' type, to which no value conforms.
*/
public static readonly VOID: SpecialType<void> = new SpecialType(
"void",
false,
(input: any): boolean => false
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
public static readonly OPTIONAL_VOID: SpecialType<void> = new SpecialType(
"void",
true,
(input: any): boolean => false
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
/**
* The 'undefined' type, to which (present) explicitly undefined values conform.
*/
public static readonly UNDEFINED: SpecialType<undefined> = new SpecialType(
"undefined",
false,
(input: any): boolean => input === undefined
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
public static readonly OPTIONAL_UNDEFINED: SpecialType<undefined> = new SpecialType(
"undefined",
true,
(input: any): boolean => input === undefined
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
/**
* The 'null' type, to which null values conform.
*/
public static readonly NULL: SpecialType<null> = new SpecialType(
"null",
false,
(input: any): boolean => input === null
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
public static readonly OPTIONAL_NULL: SpecialType<null> = new SpecialType(
"null",
true,
(input: any): boolean => input === null
/**
* The 'non-present' type, to which no value conforms.
*/
public static readonly NON_PRESENT: SpecialType<null> = new SpecialType(
"non-present",
(input: any): boolean => false
);

@@ -88,6 +72,13 @@

// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
protected constructor(name: string, isOptional: boolean, validator: Validator) {
/**
* Initializes a new SpecialType with the provided name and validator function.
*
* This method is protected and most likely will not need to be called by any child classes.
*
* @param name The name of the newly initialized SpecialType.
* @param validator The validator function of the newly initialized SpecialType.
*/
protected constructor(name: string, validator: Validator) {
super(isOptional);
super();

@@ -111,5 +102,8 @@ this.typeName = name;

/**
* TODO
* Returns true if and only if the provided input conforms to the validator function of whichever SpecialType
* is being used.
*
* @param input Any variable to check for conformity to this SpecialType.
* @return true if and only if the provided input conforms to the validator function of whichever SpecialType
* is being used.
*/

@@ -123,5 +117,8 @@ public checkConformity(input: any): boolean {

/**
* TODO
* Due to the fact that an exhaustive check of SpecialTypes wouldn't do anything different than the normal
* conformity check, this method simply redirects to {@link SpecialType#checkConformity}.
*
* @param input Any variable to exhaustively check for conformity to this SpecialType.
* @return true if and only if the provided input conforms to the validator function of whichever SpecialType
* is being used.
*/

@@ -128,0 +125,0 @@ public exhaustivelyCheckConformity(input: any): boolean {

@@ -15,3 +15,3 @@ /*

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @version v0.6.0
* @since v0.1.0

@@ -21,44 +21,26 @@ */

// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
/**
* The 'number' type, to which numeric values conform.
*/
public static readonly NUMBER: StandardType<number> = new StandardType(
"number",
false,
(input: any): boolean => (typeof input === "number")
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
public static readonly OPTIONAL_NUMBER: StandardType<number> = new StandardType(
"number",
true,
(input: any): boolean => (typeof input === "number")
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
/**
* The 'boolean' type, to which truthy and falsy values conform.
*/
public static readonly BOOLEAN: StandardType<boolean> = new StandardType(
"boolean",
false,
(input: any): boolean => (typeof input === "boolean")
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
public static readonly OPTIONAL_BOOLEAN: StandardType<boolean> = new StandardType(
"boolean",
true,
(input: any): boolean => (typeof input === "boolean")
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
/**
* The 'string' type, to which string values conform.
*/
public static readonly STRING: StandardType<string> = new StandardType(
"string",
false,
(input: any): boolean => (typeof input === "string")
);
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
public static readonly OPTIONAL_STRING: StandardType<string> = new StandardType(
"string",
true,
(input: any): boolean => (typeof input === "string")
);
/**

@@ -74,6 +56,13 @@ * The name of this type.

// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
protected constructor(name: string, isOptional: boolean, validator: Validator) {
/**
* Initializes a new StandardType with the provided name and validator function.
*
* This method is protected and most likely will not need to be called by any child classes.
*
* @param name The name of the newly initialized StandardType.
* @param validator The validator function of the newly initialized StandardType.
*/
protected constructor(name: string, validator: Validator) {
super(isOptional);
super();

@@ -97,6 +86,8 @@ this.typeName = name;

/**
* TODO
* Returns true if and only if the provided input conforms to the validator function of whichever StandardType
* is being used.
*
* @param input Any variable to check for conformity to this StandardType.
* @return
* @return true if and only if the provided input conforms to the validator function of whichever StandardType
* is being used.
*/

@@ -110,5 +101,8 @@ public checkConformity(input: any): boolean {

/**
* TODO
* Due to the fact that an exhaustive check of StandardTypes wouldn't do anything different than the normal
* conformity check, this method simply redirects to {@link StandardType#checkConformity}.
*
* @param input Any variable to exhaustively check for conformity to this StandardType.
* @return true if and only if the provided input conforms to the validator function of whichever StandardType
* is being used.
*/

@@ -115,0 +109,0 @@ public exhaustivelyCheckConformity(input: any): boolean {

@@ -11,3 +11,3 @@ /*

* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @version v0.6.0
* @since v0.1.0

@@ -18,43 +18,65 @@ */

/**
* Whether or not this value is optional.
* Initializes a new Type.
*/
protected constructor() { /* Do nothing. */ }
/**
* Returns the optionality of this Type.
*
* @see Type#isOptional
* @return true if this Type is optional.
*/
protected optional: boolean;
public isOptional(): boolean {
return false;
}
/**
* Initializes a new Type with the given optionality.
* Returns the string name of this Type.
*
* @param isOptional true if this Type should be optional.
* @return The string name of this Type.
*/
protected constructor(isOptional: boolean) {
public abstract getTypeName(): string;
this.optional = isOptional;
/**
* Returns true if and only if the input value conforms to this Type.
*
* @param input Any variable to check for conformity to this Type.
* @return true if and only if the input value conforms to this Type
*/
public abstract checkConformity(input: any): boolean;
/**
* Returns true if and only if all members passed as arguments conform to this Type.
*
* @param input Any number of variables to check for conformity to this Type.
* @return true if and only if all members passed as arguments conform to this Type.
*/
public checkConformityOfAll(...input: any[]): boolean {
return input.every((item: any): boolean => this.checkConformity(item));
}
/**
* Returns the optionality of this Type.
* Returns true if and only if the input value exhaustively conforms to this Type.
*
* Optionality refers to whether or not the given type must appear at all on the object which is being checked for
* a value of 'this' Type. Keep in mind that optionality does not refer to whether or not accessing the variable
* will return 'undefined'. As counter-intuitive as it may seem, a value may be defined as 'undefined'. To better
* represent this problem, an example:
* Note that the exact definition of "exhaustively conformity" differs between Types.
*
* let obj = {
* myVal: undefined
* }
* @param input Any variable to exhaustively check for conformity to this Type.
* @return true if and only if the input value exhaustively conforms to this Type.
*/
public abstract exhaustivelyCheckConformity(input: any): boolean;
/**
* Returns true if and only if all members passed as arguments exhaustively conform to this Type.
*
* console.log(obj.myVal); // returns 'undefined'
* console.log(obj.yourVal); // returns 'undefined'
* Note that the exact definition of "exhaustively conformity" differs between Types.
*
* Given the above demonstration, you can see that both 'non-present' (also called 'blank', etc) variables, as well
* as 'present' and defined variables will evaluate to 'undefined'. With this understanding, it might now be easier
* to understand what optionality is: if a value is optional it can either be 'present' and of the Type represented
* by this class, or it can be entirely 'non-present' - both of which cases would ensure conformity to this type
* (given that this type is defined as optional).
* @param input Any number of variables to exhaustively check for conformity to this Type.
* @return true if and only if all members passed as arguments exhaustively conform to this Type.
*/
public isOptional(): boolean {
public exhaustivelyCheckConformityOfAll(...input: any[]): boolean {
return this.optional;
return input.every((item: any): boolean => this.exhaustivelyCheckConformity(item));

@@ -64,15 +86,8 @@ }

/**
* Returns the name of this Type.
*/
public abstract getTypeName(): string;
/**
* TODO
* Either returns the given input if it correctly conforms to this {@link Type}, or throws a TypeError.
*
* @param input Any variable to check for conformity to this Type.
* @param input The input to sanitize.
* @return The given input if it correctly conforms to this Type.
* @throws A TypeError if the provided input does not correctly conform to this Type.
*/
public abstract checkConformity(input: any): boolean;
public abstract exhaustivelyCheckConformity(input: any): boolean;
public sanitize(input: any): E {

@@ -83,3 +98,3 @@

throw new Error("ERR | Attempted to sanitize content that did not match the desired type signature. " +
throw new TypeError("ERR | Attempted to sanitize content that did not match the desired type signature. " +
"Type of " + input.toString() + "was not '" + this.getTypeName() + "'");

@@ -86,0 +101,0 @@

@@ -10,6 +10,6 @@ /*

/**
* A type that can be one of a set of types.
* A {@link Type} that can be one of a set of Types.
*
* @author Trevor Sears <trevorsears.main@gmail.com>
* @version v0.5.0
* @version v0.6.0
* @since v0.4.0

@@ -24,9 +24,15 @@ */

// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
/**
* A list of acceptable {@link Type}s for this UnionType.
*/
protected acceptableTypes: Type[];
// DOC-ME [6/15/19 @ 7:14 PM] - Documentation required!
public constructor(types: Type[], isOptional: boolean = false) {
/**
* Initializes a new UnionType for the provided list of {@link Type}s.
*
* @param types The Types to union for this UnionType.
*/
public constructor(types: Type[]) {
super(isOptional);
super();

@@ -46,3 +52,5 @@ this.acceptableTypes = types;

/**
* Returns the name of this Type.
* Returns the string name of this Type.
*
* @return The string name of this Type.
*/

@@ -56,5 +64,11 @@ public getTypeName(): string {

/**
* TODO
* Returns true if and only if the provided input variable conforms to one or more of the types within this
* UnionType.
*
* This method makes no guarantees that the provided input does or does not conform to more than one of the types
* within this UnionType. For such functionality see {@link UnionType#exhaustivelyCheckConformity}.
*
* @param input Any variable to check for conformity to this UnionType.
* @return true if and only if the provided input variable conforms to one or more of the types within this
* UnionType.
*/

@@ -68,5 +82,8 @@ public checkConformity(input: any): boolean {

/**
* TODO
* Returns true if and only if the provided input variable conforms to exactly one of the types within this
* UnionType.
*
* @param input Any variable to exhaustively check for conformity to this UnionType.
* @return true if and only if the provided input variable conforms to exactly one of the types within this
* UnionType.
*/

@@ -73,0 +90,0 @@ public exhaustivelyCheckConformity(input: any): boolean {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc