Comparing version 4.0.1 to 4.1.0
@@ -0,1 +1,13 @@ | ||
## 4.1.0 - 2020-01-20 | ||
#### 🚀 Updates | ||
- Added `Schema#currentPath`, `currentValue`, `initialStruct`, `parentPath`, and `parentStruct` | ||
properties. Should make custom tree checks much easier. | ||
- Added `Predicate#doRun`, so that sub-classes can hook into the run process. | ||
#### 🐞 Fixes | ||
- **[shape,tuple]** Fixed an issue where common predicate checks were not always running. | ||
### 4.0.1 - 2020-01-19 | ||
@@ -2,0 +14,0 @@ |
@@ -108,13 +108,13 @@ function _defineProperty(obj, key, value) { | ||
var checkedKeys = [_this.key(path)].concat(keys); | ||
var struct = (_ref = (_this$schema = _this.schema) === null || _this$schema === void 0 ? void 0 : _this$schema.struct) !== null && _ref !== void 0 ? _ref : {}; | ||
var undefs = checkedKeys.filter(function (key) { | ||
var andKeys = [_this.key(path)].concat(keys); | ||
var struct = (_ref = (_this$schema = _this.schema) === null || _this$schema === void 0 ? void 0 : _this$schema.parentStruct) !== null && _ref !== void 0 ? _ref : {}; | ||
var undefs = andKeys.filter(function (key) { | ||
return typeof struct[key] === 'undefined' || struct[key] === null; | ||
}); | ||
if (undefs.length === checkedKeys.length) { | ||
if (undefs.length === andKeys.length) { | ||
return; | ||
} | ||
_this.invariant(undefs.length === 0, "All of these fields must be defined: " + checkedKeys.join(', ')); | ||
_this.invariant(undefs.length === 0, "All of these fields must be defined: " + andKeys.join(', ')); | ||
}); | ||
@@ -254,3 +254,3 @@ } | ||
var orKeys = [_this4.key(path)].concat(keys); | ||
var struct = (_ref4 = (_this4$schema = _this4.schema) === null || _this4$schema === void 0 ? void 0 : _this4$schema.struct) !== null && _ref4 !== void 0 ? _ref4 : {}; | ||
var struct = (_ref4 = (_this4$schema = _this4.schema) === null || _this4$schema === void 0 ? void 0 : _this4$schema.parentStruct) !== null && _ref4 !== void 0 ? _ref4 : {}; | ||
var defs = orKeys.filter(function (key) { | ||
@@ -299,3 +299,3 @@ return typeof struct[key] !== 'undefined' && struct[key] !== null; | ||
if (this.isNullable) { | ||
return value; | ||
return null; | ||
} | ||
@@ -308,2 +308,5 @@ | ||
this.schema.currentPath = path; | ||
this.schema.currentValue = value; | ||
value = this.doRun(value, path); | ||
value = this.validate(value, path); | ||
@@ -348,3 +351,3 @@ return this.cast(value); | ||
var xorKeys = [_this6.key(path)].concat(keys); | ||
var struct = (_ref5 = (_this6$schema = _this6.schema) === null || _this6$schema === void 0 ? void 0 : _this6$schema.struct) !== null && _ref5 !== void 0 ? _ref5 : {}; | ||
var struct = (_ref5 = (_this6$schema = _this6.schema) === null || _this6$schema === void 0 ? void 0 : _this6$schema.parentStruct) !== null && _ref5 !== void 0 ? _ref5 : {}; | ||
var defs = xorKeys.filter(function (key) { | ||
@@ -400,2 +403,6 @@ return typeof struct[key] !== 'undefined' && struct[key] !== null; | ||
_proto.doRun = function doRun(value, path) { | ||
return value; | ||
}; | ||
return Predicate; | ||
@@ -450,6 +457,14 @@ }(); | ||
_defineProperty(this, "currentStruct", {}); | ||
_defineProperty(this, "currentPath", ''); | ||
_defineProperty(this, "currentValue", null); | ||
_defineProperty(this, "filePath", ''); | ||
_defineProperty(this, "initialStruct", {}); | ||
_defineProperty(this, "parentPath", ''); | ||
_defineProperty(this, "parentStruct", {}); | ||
_defineProperty(this, "struct", {}); | ||
@@ -490,3 +505,4 @@ | ||
this.struct = struct; | ||
this.initialStruct = _extends({}, struct); | ||
this.struct = _extends({}, struct); | ||
@@ -502,3 +518,5 @@ var unknownFields = _extends({}, struct); | ||
if (predicate instanceof Predicate || isObject(predicate) && predicate.constructor.name.endsWith('Predicate')) { | ||
_this.currentStruct[key] = predicate.run(value, path, _this); | ||
_this.parentPath = baseKey; | ||
_this.parentStruct = _this.initialStruct; | ||
_this.struct[key] = predicate.run(value, path, _this); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
@@ -512,3 +530,3 @@ throw new Error("Unknown blueprint for \"" + path + "\". Must be a predicate."); | ||
if (this.unknown) { | ||
Object.assign(this.currentStruct, unknownFields); | ||
Object.assign(this.struct, unknownFields); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
@@ -518,3 +536,3 @@ logUnknown(unknownFields, pathPrefix); | ||
return this.currentStruct; | ||
return this.struct; | ||
}; | ||
@@ -1080,3 +1098,3 @@ | ||
_proto.run = function run(value, path, schema) { | ||
_proto.doRun = function doRun(value, path) { | ||
var _this3 = this; | ||
@@ -1090,7 +1108,10 @@ | ||
var struct = {}; | ||
var struct = _extends({}, value); | ||
this.schema.parentPath = path; | ||
this.schema.parentStruct = struct; | ||
Object.keys(this.contents).forEach(function (baseKey) { | ||
var key = baseKey; | ||
var content = _this3.contents[key]; | ||
struct[key] = content.run(value === null || value === void 0 ? void 0 : value[key], path + "." + key, schema); | ||
struct[key] = content.run(value === null || value === void 0 ? void 0 : value[key], path + "." + key, _this3.schema); | ||
delete unknownFields[key]; | ||
@@ -1276,3 +1297,9 @@ }); | ||
_proto.run = function run(value, path) { | ||
_proto.typeAlias = function typeAlias() { | ||
return "[" + this.contents.map(function (item) { | ||
return item.typeAlias(); | ||
}).join(', ') + "]"; | ||
}; | ||
_proto.doRun = function doRun(value, path) { | ||
var _this2 = this; | ||
@@ -1293,8 +1320,2 @@ | ||
_proto.typeAlias = function typeAlias() { | ||
return "[" + this.contents.map(function (item) { | ||
return item.typeAlias(); | ||
}).join(', ') + "]"; | ||
}; | ||
return TuplePredicate; | ||
@@ -1301,0 +1322,0 @@ }(Predicate); |
@@ -112,13 +112,13 @@ 'use strict'; | ||
var checkedKeys = [_this.key(path)].concat(keys); | ||
var struct = (_ref = (_this$schema = _this.schema) === null || _this$schema === void 0 ? void 0 : _this$schema.struct) !== null && _ref !== void 0 ? _ref : {}; | ||
var undefs = checkedKeys.filter(function (key) { | ||
var andKeys = [_this.key(path)].concat(keys); | ||
var struct = (_ref = (_this$schema = _this.schema) === null || _this$schema === void 0 ? void 0 : _this$schema.parentStruct) !== null && _ref !== void 0 ? _ref : {}; | ||
var undefs = andKeys.filter(function (key) { | ||
return typeof struct[key] === 'undefined' || struct[key] === null; | ||
}); | ||
if (undefs.length === checkedKeys.length) { | ||
if (undefs.length === andKeys.length) { | ||
return; | ||
} | ||
_this.invariant(undefs.length === 0, "All of these fields must be defined: " + checkedKeys.join(', ')); | ||
_this.invariant(undefs.length === 0, "All of these fields must be defined: " + andKeys.join(', ')); | ||
}); | ||
@@ -258,3 +258,3 @@ } | ||
var orKeys = [_this4.key(path)].concat(keys); | ||
var struct = (_ref4 = (_this4$schema = _this4.schema) === null || _this4$schema === void 0 ? void 0 : _this4$schema.struct) !== null && _ref4 !== void 0 ? _ref4 : {}; | ||
var struct = (_ref4 = (_this4$schema = _this4.schema) === null || _this4$schema === void 0 ? void 0 : _this4$schema.parentStruct) !== null && _ref4 !== void 0 ? _ref4 : {}; | ||
var defs = orKeys.filter(function (key) { | ||
@@ -303,3 +303,3 @@ return typeof struct[key] !== 'undefined' && struct[key] !== null; | ||
if (this.isNullable) { | ||
return value; | ||
return null; | ||
} | ||
@@ -312,2 +312,5 @@ | ||
this.schema.currentPath = path; | ||
this.schema.currentValue = value; | ||
value = this.doRun(value, path); | ||
value = this.validate(value, path); | ||
@@ -352,3 +355,3 @@ return this.cast(value); | ||
var xorKeys = [_this6.key(path)].concat(keys); | ||
var struct = (_ref5 = (_this6$schema = _this6.schema) === null || _this6$schema === void 0 ? void 0 : _this6$schema.struct) !== null && _ref5 !== void 0 ? _ref5 : {}; | ||
var struct = (_ref5 = (_this6$schema = _this6.schema) === null || _this6$schema === void 0 ? void 0 : _this6$schema.parentStruct) !== null && _ref5 !== void 0 ? _ref5 : {}; | ||
var defs = xorKeys.filter(function (key) { | ||
@@ -404,2 +407,6 @@ return typeof struct[key] !== 'undefined' && struct[key] !== null; | ||
_proto.doRun = function doRun(value, path) { | ||
return value; | ||
}; | ||
return Predicate; | ||
@@ -454,6 +461,14 @@ }(); | ||
_defineProperty(this, "currentStruct", {}); | ||
_defineProperty(this, "currentPath", ''); | ||
_defineProperty(this, "currentValue", null); | ||
_defineProperty(this, "filePath", ''); | ||
_defineProperty(this, "initialStruct", {}); | ||
_defineProperty(this, "parentPath", ''); | ||
_defineProperty(this, "parentStruct", {}); | ||
_defineProperty(this, "struct", {}); | ||
@@ -494,3 +509,4 @@ | ||
this.struct = struct; | ||
this.initialStruct = _extends({}, struct); | ||
this.struct = _extends({}, struct); | ||
@@ -506,3 +522,5 @@ var unknownFields = _extends({}, struct); | ||
if (predicate instanceof Predicate || isObject(predicate) && predicate.constructor.name.endsWith('Predicate')) { | ||
_this.currentStruct[key] = predicate.run(value, path, _this); | ||
_this.parentPath = baseKey; | ||
_this.parentStruct = _this.initialStruct; | ||
_this.struct[key] = predicate.run(value, path, _this); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
@@ -516,3 +534,3 @@ throw new Error("Unknown blueprint for \"" + path + "\". Must be a predicate."); | ||
if (this.unknown) { | ||
Object.assign(this.currentStruct, unknownFields); | ||
Object.assign(this.struct, unknownFields); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
@@ -522,3 +540,3 @@ logUnknown(unknownFields, pathPrefix); | ||
return this.currentStruct; | ||
return this.struct; | ||
}; | ||
@@ -1084,3 +1102,3 @@ | ||
_proto.run = function run(value, path, schema) { | ||
_proto.doRun = function doRun(value, path) { | ||
var _this3 = this; | ||
@@ -1094,7 +1112,10 @@ | ||
var struct = {}; | ||
var struct = _extends({}, value); | ||
this.schema.parentPath = path; | ||
this.schema.parentStruct = struct; | ||
Object.keys(this.contents).forEach(function (baseKey) { | ||
var key = baseKey; | ||
var content = _this3.contents[key]; | ||
struct[key] = content.run(value === null || value === void 0 ? void 0 : value[key], path + "." + key, schema); | ||
struct[key] = content.run(value === null || value === void 0 ? void 0 : value[key], path + "." + key, _this3.schema); | ||
delete unknownFields[key]; | ||
@@ -1280,3 +1301,9 @@ }); | ||
_proto.run = function run(value, path) { | ||
_proto.typeAlias = function typeAlias() { | ||
return "[" + this.contents.map(function (item) { | ||
return item.typeAlias(); | ||
}).join(', ') + "]"; | ||
}; | ||
_proto.doRun = function doRun(value, path) { | ||
var _this2 = this; | ||
@@ -1297,8 +1324,2 @@ | ||
_proto.typeAlias = function typeAlias() { | ||
return "[" + this.contents.map(function (item) { | ||
return item.typeAlias(); | ||
}).join(', ') + "]"; | ||
}; | ||
return TuplePredicate; | ||
@@ -1305,0 +1326,0 @@ }(Predicate); |
import Schema from './Schema'; | ||
import { SupportedType, CheckerCallback, CustomCallback, FuncOf, DefaultValue, DefaultValueFactory } from './types'; | ||
import { SupportedType, CheckerCallback, CustomCallback, FuncOf, DefaultValue, DefaultValueFactory, NonUndefined } from './types'; | ||
export interface TemporalStruct { | ||
@@ -26,3 +26,3 @@ [key: string]: unknown; | ||
*/ | ||
cast(value: unknown): T; | ||
cast(value: unknown): NonUndefined<T>; | ||
/** | ||
@@ -77,3 +77,3 @@ * Set a callback to run custom logic. | ||
*/ | ||
run(initialValue: T | undefined, path: string, schema: Schema<{}>): T | null; | ||
run(initialValue: T | undefined, path: string, schema: Schema<{}>): NonUndefined<T> | null; | ||
/** | ||
@@ -108,4 +108,8 @@ * Return a human readable type name. | ||
protected key(path: string): string; | ||
/** | ||
* Helper method for sub-classes to provide custom run logic. | ||
*/ | ||
protected doRun(value: T, path: string): T; | ||
} | ||
export declare function custom<T, S extends object = {}>(callback: CustomCallback<T, S>, defaultValue: DefaultValue<T>): Predicate<T>; | ||
export declare function func<T extends FuncOf = FuncOf>(defaultValue?: T | null): Predicate<T | null>; |
import Predicate from '../Predicate'; | ||
import { DefaultValue } from '../types'; | ||
import { DefaultValue, NonUndefined } from '../types'; | ||
export default class BooleanPredicate<T extends boolean = boolean> extends Predicate<T> { | ||
constructor(defaultValue?: DefaultValue<T>); | ||
cast(value: unknown): T; | ||
cast(value: unknown): NonUndefined<T>; | ||
onlyFalse(): BooleanPredicate<false>; | ||
@@ -7,0 +7,0 @@ onlyTrue(): BooleanPredicate<true>; |
import Predicate from '../Predicate'; | ||
import { DefaultValue } from '../types'; | ||
import { DefaultValue, NonUndefined } from '../types'; | ||
export default class NumberPredicate<T extends number = number> extends Predicate<T> { | ||
constructor(defaultValue?: DefaultValue<T>); | ||
between(min: number, max: number, inclusive?: boolean): this; | ||
cast(value: unknown): T; | ||
cast(value: unknown): NonUndefined<T>; | ||
float(): this; | ||
@@ -8,0 +8,0 @@ gt(min: number, inclusive?: boolean): this; |
import Predicate from '../Predicate'; | ||
import Schema from '../Schema'; | ||
import { Blueprint } from '../types'; | ||
@@ -10,4 +9,4 @@ export default class ShapePredicate<T extends object> extends Predicate<T> { | ||
exact(): this; | ||
run(value: T | undefined, path: string, schema: Schema<{}>): T; | ||
protected doRun(value: T, path: string): T; | ||
} | ||
export declare function shape<T extends object>(contents: Blueprint<T>): ShapePredicate<T>; |
import CollectionPredicate from './Collection'; | ||
import { DefaultValue } from '../types'; | ||
import { DefaultValue, NonUndefined } from '../types'; | ||
export default class StringPredicate<T extends string = string> extends CollectionPredicate<T> { | ||
constructor(defaultValue?: DefaultValue<T>); | ||
camelCase(): this; | ||
cast(value: unknown): T; | ||
cast(value: unknown): NonUndefined<T>; | ||
contains(token: string, index?: number): this; | ||
@@ -8,0 +8,0 @@ kebabCase(): this; |
@@ -7,3 +7,2 @@ import Predicate from '../Predicate'; | ||
default(): T; | ||
run(value: T | undefined, path: string): T | null; | ||
/** | ||
@@ -13,3 +12,4 @@ * Return the type name as an array of type items. | ||
typeAlias(): string; | ||
protected doRun(value: T, path: string): T; | ||
} | ||
export declare function tuple<T extends unknown[] = unknown[]>(contents: InferTupleContents<T>): TuplePredicate<T>; |
import { Blueprint } from './types'; | ||
export default class Schema<T extends object> { | ||
blueprint: Blueprint<T>; | ||
currentStruct: Partial<T>; | ||
currentPath: string; | ||
currentValue: unknown; | ||
filePath: string; | ||
initialStruct: Partial<T>; | ||
parentPath: string; | ||
parentStruct: unknown; | ||
struct: Partial<T>; | ||
@@ -7,0 +11,0 @@ schemaName: string; |
@@ -15,2 +15,3 @@ import Predicate from './Predicate'; | ||
export declare type DefaultValue<T> = T | DefaultValueFactory<T>; | ||
export declare type NonUndefined<T> = T extends undefined ? never : T; | ||
export interface OptimalOptions { | ||
@@ -17,0 +18,0 @@ file?: string; |
{ | ||
"name": "optimal", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "A system for building and validating defined object structures.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
103427
2569