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

optimal

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

optimal - npm Package Compare versions

Comparing version 4.1.2 to 4.2.0

6

CHANGELOG.md

@@ -0,1 +1,7 @@

## 4.2.0 - 2020-01-26
#### 🚀 Updates
- Reworked default values to handle undefined and null by default, and in a much better fashion.
### 4.1.2 - 2020-01-26

@@ -2,0 +8,0 @@

42

esm/index.js

@@ -80,7 +80,2 @@ function _defineProperty(obj, key, value) {

if ("production" !== process.env.NODE_ENV) {
this.invariant(typeof defaultValue !== 'undefined', "A default value for type \"" + type + "\" is required.");
this.addCheck(this.checkType);
}
if (typeof defaultValue === 'function' && !bypassFactory) {

@@ -112,3 +107,3 @@ this.defaultValueFactory = defaultValue;

var undefs = andKeys.filter(function (key) {
return typeof struct[key] === 'undefined' || struct[key] === null;
return struct[key] === undefined || struct[key] === null;
});

@@ -152,2 +147,7 @@

var value = this.defaultValueFactory ? this.defaultValueFactory((_ref2 = (_this$schema2 = this.schema) === null || _this$schema2 === void 0 ? void 0 : _this$schema2.struct) !== null && _ref2 !== void 0 ? _ref2 : {}) : this.defaultValue;
if (value === null) {
return null;
}
return this.cast(value);

@@ -282,3 +282,3 @@ };

if (typeof value === 'undefined') {
if (value === undefined) {
if (!this.isRequired) {

@@ -307,9 +307,12 @@ value = this.defaultValue;

}
} else {
this.checkType(path, value);
}
var finalValue = value;
this.schema.currentPath = path;
this.schema.currentValue = value;
value = this.doRun(value, path);
value = this.validate(value, path);
return this.cast(value);
this.schema.currentValue = finalValue;
finalValue = this.doRun(finalValue, path);
finalValue = this.validate(finalValue, path);
return this.cast(finalValue);
};

@@ -329,2 +332,3 @@

var nextValue = value;
this.checkType(path, value);
this.checks.forEach(function (checker) {

@@ -651,2 +655,6 @@ var result = checker.call(_this5, path, nextValue);

_proto.cast = function cast(value) {
if (value === undefined) {
return [];
}
return Array.isArray(value) ? value : [value];

@@ -693,3 +701,3 @@ };

function BooleanPredicate(defaultValue) {
return _Predicate.call(this, 'boolean', defaultValue || false) || this;
return _Predicate.call(this, 'boolean', defaultValue) || this;
}

@@ -845,3 +853,3 @@

function NumberPredicate(defaultValue) {
return _Predicate.call(this, 'number', defaultValue || 0) || this;
return _Predicate.call(this, 'number', defaultValue) || this;
}

@@ -869,3 +877,3 @@

_proto.cast = function cast(value) {
return Number(value);
return value === undefined ? 0 : Number(value);
};

@@ -1002,3 +1010,3 @@

_this = _CollectionPredicate.call(this, 'object', defaultValue || {}) || this;
_this = _CollectionPredicate.call(this, 'object', defaultValue) || this;

@@ -1161,3 +1169,3 @@ _defineProperty(_assertThisInitialized(_this), "contents", null);

function StringPredicate(defaultValue) {
return _CollectionPredicate.call(this, 'string', defaultValue || '') || this;
return _CollectionPredicate.call(this, 'string', defaultValue) || this;
}

@@ -1172,3 +1180,3 @@

_proto.cast = function cast(value) {
return String(value);
return value === undefined ? '' : String(value);
};

@@ -1175,0 +1183,0 @@

@@ -84,7 +84,2 @@ 'use strict';

if ("production" !== process.env.NODE_ENV) {
this.invariant(typeof defaultValue !== 'undefined', "A default value for type \"" + type + "\" is required.");
this.addCheck(this.checkType);
}
if (typeof defaultValue === 'function' && !bypassFactory) {

@@ -116,3 +111,3 @@ this.defaultValueFactory = defaultValue;

var undefs = andKeys.filter(function (key) {
return typeof struct[key] === 'undefined' || struct[key] === null;
return struct[key] === undefined || struct[key] === null;
});

@@ -156,2 +151,7 @@

var value = this.defaultValueFactory ? this.defaultValueFactory((_ref2 = (_this$schema2 = this.schema) === null || _this$schema2 === void 0 ? void 0 : _this$schema2.struct) !== null && _ref2 !== void 0 ? _ref2 : {}) : this.defaultValue;
if (value === null) {
return null;
}
return this.cast(value);

@@ -286,3 +286,3 @@ };

if (typeof value === 'undefined') {
if (value === undefined) {
if (!this.isRequired) {

@@ -311,9 +311,12 @@ value = this.defaultValue;

}
} else {
this.checkType(path, value);
}
var finalValue = value;
this.schema.currentPath = path;
this.schema.currentValue = value;
value = this.doRun(value, path);
value = this.validate(value, path);
return this.cast(value);
this.schema.currentValue = finalValue;
finalValue = this.doRun(finalValue, path);
finalValue = this.validate(finalValue, path);
return this.cast(finalValue);
};

@@ -333,2 +336,3 @@

var nextValue = value;
this.checkType(path, value);
this.checks.forEach(function (checker) {

@@ -655,2 +659,6 @@ var result = checker.call(_this5, path, nextValue);

_proto.cast = function cast(value) {
if (value === undefined) {
return [];
}
return Array.isArray(value) ? value : [value];

@@ -697,3 +705,3 @@ };

function BooleanPredicate(defaultValue) {
return _Predicate.call(this, 'boolean', defaultValue || false) || this;
return _Predicate.call(this, 'boolean', defaultValue) || this;
}

@@ -849,3 +857,3 @@

function NumberPredicate(defaultValue) {
return _Predicate.call(this, 'number', defaultValue || 0) || this;
return _Predicate.call(this, 'number', defaultValue) || this;
}

@@ -873,3 +881,3 @@

_proto.cast = function cast(value) {
return Number(value);
return value === undefined ? 0 : Number(value);
};

@@ -1006,3 +1014,3 @@

_this = _CollectionPredicate.call(this, 'object', defaultValue || {}) || this;
_this = _CollectionPredicate.call(this, 'object', defaultValue) || this;

@@ -1165,3 +1173,3 @@ _defineProperty(_assertThisInitialized(_this), "contents", null);

function StringPredicate(defaultValue) {
return _CollectionPredicate.call(this, 'string', defaultValue || '') || this;
return _CollectionPredicate.call(this, 'string', defaultValue) || this;
}

@@ -1176,3 +1184,3 @@

_proto.cast = function cast(value) {
return String(value);
return value === undefined ? '' : String(value);
};

@@ -1179,0 +1187,0 @@

@@ -7,3 +7,3 @@ import Schema from './Schema';

export default class Predicate<T> {
defaultValue?: T;
defaultValue?: T | null;
schema?: Schema<{}>;

@@ -19,3 +19,3 @@ type: SupportedType;

protected noErrorPrefix: boolean;
constructor(type: SupportedType, defaultValue: DefaultValue<T>, bypassFactory?: boolean);
constructor(type: SupportedType, defaultValue?: DefaultValue<T>, bypassFactory?: boolean);
/**

@@ -28,3 +28,3 @@ * Map a list of names that must be defined alongside this field.

*/
cast(value: unknown): NonUndefined<T>;
cast(value: unknown): T;
/**

@@ -37,3 +37,3 @@ * Set a callback to run custom logic.

*/
default(): T;
default(): T | null;
/**

@@ -40,0 +40,0 @@ * Set a message to log when this field is present.

import Predicate from '../Predicate';
import { DefaultValue, NonUndefined } from '../types';
import { DefaultValue } from '../types';
export default class BooleanPredicate<T extends boolean = boolean> extends Predicate<T> {
constructor(defaultValue?: DefaultValue<T>);
cast(value: unknown): NonUndefined<T>;
cast(value: unknown): T;
onlyFalse(): BooleanPredicate<false>;

@@ -7,0 +7,0 @@ onlyTrue(): BooleanPredicate<true>;

import Predicate from '../Predicate';
import { DefaultValue, NonUndefined } from '../types';
import { DefaultValue } 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): NonUndefined<T>;
cast(value: unknown): T;
float(): this;

@@ -8,0 +8,0 @@ gt(min: number, inclusive?: boolean): this;

import CollectionPredicate from './Collection';
import { DefaultValue, NonUndefined } from '../types';
import { DefaultValue } from '../types';
export default class StringPredicate<T extends string = string> extends CollectionPredicate<T> {
constructor(defaultValue?: DefaultValue<T>);
camelCase(): this;
cast(value: unknown): NonUndefined<T>;
cast(value: unknown): T;
contains(token: string, index?: number): this;

@@ -8,0 +8,0 @@ kebabCase(): this;

@@ -14,3 +14,3 @@ import Predicate from './Predicate';

export declare type DefaultValueFactory<T> = (struct: any) => T;
export declare type DefaultValue<T> = T | DefaultValueFactory<T>;
export declare type DefaultValue<T> = T | DefaultValueFactory<T> | null;
export declare type NonUndefined<T> = T extends undefined ? never : T;

@@ -17,0 +17,0 @@ export interface OptimalOptions {

{
"name": "optimal",
"version": "4.1.2",
"version": "4.2.0",
"description": "A system for building and validating defined object structures.",

@@ -5,0 +5,0 @@ "main": "./lib/index.js",

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