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

@finnair/v-validation-core

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@finnair/v-validation-core - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

@@ -6,2 +6,10 @@ # Change Log

# [0.3.0](https://github.com/finnair/v-validation/compare/v0.2.0...v0.3.0) (2020-04-16)
**Note:** Version bump only for package @finnair/v-validation-core
# [0.2.0](https://github.com/finnair/v-validation/compare/v0.1.4...v0.2.0) (2020-04-15)

@@ -8,0 +16,0 @@

export declare type PathComponent = number | string;
export declare class Path {
static readonly ROOT: Path;
private readonly path;

@@ -10,11 +11,29 @@ private constructor();

get length(): number;
get(index: number): PathComponent;
componentAt(index: number): PathComponent;
[Symbol.iterator](): IterableIterator<PathComponent>;
unset(object: any): any;
set(object: any, value: any): any;
get(root: any): any;
unset(root: any): any;
set(root: any, value: any): any;
/**
* @deprecated Use Path.ROOT instead
*/
static newRoot(): Path;
static property(property: string): Path;
static index(index: number): Path;
static of(...path: PathComponent[]): Path;
private static validateComponent;
private static validateIndex;
private static validateProperty;
}
/**
* @deprecated Use Path.ROOT instead
*/
export declare const ROOT: Path;
/**
* @deprecated Use Path.property instead
*/
export declare function property(property: string): Path;
/**
* @deprecated Use Path.index instead
*/
export declare function index(index: number): Path;

@@ -9,5 +9,7 @@ "use strict";

index(index) {
Path.validateIndex(index);
return new Path(this.path.concat(index));
}
property(property) {
Path.validateProperty(property);
return new Path(this.path.concat(property));

@@ -24,3 +26,3 @@ }

}
get(index) {
componentAt(index) {
return this.path[index];

@@ -31,6 +33,21 @@ }

}
unset(object) {
return this.set(object, undefined);
get(root) {
if (this.path.length === 0) {
return root;
}
let current = root;
let index = 0;
for (; index < this.path.length - 1 && typeof current === 'object'; index++) {
const component = this.path[index];
current = current[component];
}
if (index === this.path.length - 1 && typeof current === 'object') {
return current[this.path[this.path.length - 1]];
}
return undefined;
}
set(object, value) {
unset(root) {
return this.set(root, undefined);
}
set(root, value) {
if (this.path.length === 0) {

@@ -40,4 +57,4 @@ return value;

let index = -1;
const root = toObject(object, this.path);
let current = root;
const _root = toObject(root, this.path);
let current = _root;
for (index = 0; index < this.path.length - 1 && current; index++) {

@@ -57,3 +74,3 @@ const component = this.path[index];

}
return root;
return _root;
function toObject(current, path) {

@@ -76,20 +93,63 @@ if (typeof current === 'object') {

}
/**
* @deprecated Use Path.ROOT instead
*/
static newRoot() {
return new Path([]);
return Path.ROOT;
}
static property(property) {
return Path.ROOT.property(property);
}
static index(index) {
return Path.ROOT.index(index);
}
static of(...path) {
if (path.length === 0) {
return exports.ROOT;
return Path.ROOT;
}
path.forEach(this.validateComponent);
return new Path(path);
}
static validateComponent(component) {
if (typeof component === 'number') {
if (component < 0 || !Number.isInteger(component)) {
throw new Error('Expected component to be an integer >= 0');
}
}
else if (typeof component !== 'string') {
throw new Error(`Expected component to be a string or integer, got ${component}`);
}
}
static validateIndex(index) {
if (typeof index !== 'number') {
throw new Error(`Expected index to be a number, got ${index}`);
}
if (index < 0 || !Number.isInteger(index)) {
throw new Error('Expected index to be an integer >= 0');
}
}
static validateProperty(property) {
if (typeof property !== 'string') {
throw new Error(`Expected property to be a string, got ${property}`);
}
}
}
exports.Path = Path;
exports.ROOT = Path.newRoot();
Path.ROOT = new Path([]);
/**
* @deprecated Use Path.ROOT instead
*/
exports.ROOT = Path.ROOT;
/**
* @deprecated Use Path.property instead
*/
function property(property) {
return exports.ROOT.property(property);
return Path.property(property);
}
exports.property = property;
/**
* @deprecated Use Path.index instead
*/
function index(index) {
return exports.ROOT.index(index);
return Path.index(index);
}

@@ -96,0 +156,0 @@ exports.index = index;

@@ -8,2 +8,3 @@ "use strict";

const path_1 = require("./path");
const ROOT = path_1.Path.ROOT;
class ValidationContext {

@@ -51,3 +52,3 @@ constructor(options) {

validate(value, options) {
return this.validatePath(value, path_1.ROOT, new ValidationContext(options || {}));
return this.validatePath(value, ROOT, new ValidationContext(options || {}));
}

@@ -242,19 +243,19 @@ then(...allOf) {

exports.defaultViolations = {
date: (invalidValue, path = path_1.ROOT, type = ValidatorType.Date) => new TypeMismatch(path, type, invalidValue),
object: (path = path_1.ROOT) => new TypeMismatch(path, 'object'),
string: (invalidValue, path = path_1.ROOT) => new TypeMismatch(path, 'string', invalidValue),
boolean: (invalidValue, path = path_1.ROOT) => new TypeMismatch(path, 'boolean', invalidValue),
number: (invalidValue, format = NumberFormat.number, path = path_1.ROOT) => new TypeMismatch(path, format, invalidValue),
min: (min, inclusive, invalidValue, path = path_1.ROOT) => new MinViolation(path, min, inclusive, invalidValue),
max: (max, inclusive, invalidValue, path = path_1.ROOT) => new MaxViolation(path, max, inclusive, invalidValue),
size: (min, max, path = path_1.ROOT) => new SizeViolation(path, min, max),
notNull: (path = path_1.ROOT) => new Violation(path, ValidatorType.NotNull),
notEmpty: (path = path_1.ROOT) => new Violation(path, ValidatorType.NotEmpty),
notBlank: (path = path_1.ROOT) => new Violation(path, ValidatorType.NotBlank),
oneOf: (matches, path = path_1.ROOT) => new OneOfMismatch(path, matches),
pattern: (pattern, invalidValue, path = path_1.ROOT) => new PatternViolation(path, '' + pattern, invalidValue),
enum: (name, invalidValue, path = path_1.ROOT) => new EnumMismatch(path, name, invalidValue),
unknownProperty: (path = path_1.ROOT) => new Violation(path, ValidatorType.UnknownProperty),
unknownPropertyDenied: (path = path_1.ROOT) => new Violation(path, ValidatorType.UnknownPropertyDenied),
cycle: (path = path_1.ROOT) => new Violation(path, 'Cycle'),
date: (invalidValue, path = ROOT, type = ValidatorType.Date) => new TypeMismatch(path, type, invalidValue),
object: (path = ROOT) => new TypeMismatch(path, 'object'),
string: (invalidValue, path = ROOT) => new TypeMismatch(path, 'string', invalidValue),
boolean: (invalidValue, path = ROOT) => new TypeMismatch(path, 'boolean', invalidValue),
number: (invalidValue, format = NumberFormat.number, path = ROOT) => new TypeMismatch(path, format, invalidValue),
min: (min, inclusive, invalidValue, path = ROOT) => new MinViolation(path, min, inclusive, invalidValue),
max: (max, inclusive, invalidValue, path = ROOT) => new MaxViolation(path, max, inclusive, invalidValue),
size: (min, max, path = ROOT) => new SizeViolation(path, min, max),
notNull: (path = ROOT) => new Violation(path, ValidatorType.NotNull),
notEmpty: (path = ROOT) => new Violation(path, ValidatorType.NotEmpty),
notBlank: (path = ROOT) => new Violation(path, ValidatorType.NotBlank),
oneOf: (matches, path = ROOT) => new OneOfMismatch(path, matches),
pattern: (pattern, invalidValue, path = ROOT) => new PatternViolation(path, '' + pattern, invalidValue),
enum: (name, invalidValue, path = ROOT) => new EnumMismatch(path, name, invalidValue),
unknownProperty: (path = ROOT) => new Violation(path, ValidatorType.UnknownProperty),
unknownPropertyDenied: (path = ROOT) => new Violation(path, ValidatorType.UnknownPropertyDenied),
cycle: (path = ROOT) => new Violation(path, 'Cycle'),
};

@@ -261,0 +262,0 @@ function getPropertyValidators(properties) {

{
"name": "@finnair/v-validation-core",
"version": "0.2.0",
"version": "0.3.0",
"description": "V-validation core package",

@@ -31,3 +31,3 @@ "main": "dist/index.js",

},
"gitHead": "8e291a534a701538a8fb8a363ecbd5fddcd0ff95"
"gitHead": "9336821f893cf4584b99fc57edc411ccfd67cd6c"
}