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

@finnair/v-validation

Package Overview
Dependencies
Maintainers
3
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@finnair/v-validation - npm Package Compare versions

Comparing version 0.7.0 to 0.8.0

8

CHANGELOG.md

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

# [0.8.0](https://github.com/finnair/v-validation/compare/v0.7.0...v0.8.0) (2020-09-09)
**Note:** Version bump only for package @finnair/v-validation
# [0.7.0](https://github.com/finnair/v-validation/compare/v0.6.2...v0.7.0) (2020-08-26)

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

69

dist/schema.js
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -37,32 +46,36 @@ exports.SchemaValidator = exports.DiscriminatorViolation = exports.ModelRef = void 0;

}
async validatePath(value, path, ctx) {
return this.validateClass(value, path, ctx);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return this.validateClass(value, path, ctx);
});
}
async validateClass(value, path, ctx, expectedType) {
if (validators_1.isNullOrUndefined(value)) {
return ctx.failure(validators_1.defaultViolations.notNull(path), value);
}
// 1) Validate discriminator
let type;
let typePath = path;
if (validators_1.isString(this.discriminator)) {
type = value[this.discriminator];
typePath = path.property(this.discriminator);
}
else {
type = this.discriminator(value);
}
const validator = this.validators[type];
if (!validator) {
return ctx.failure(new DiscriminatorViolation(typePath, type, Object.keys(this.validators)), type);
}
// 2) Validate that the type is assignable to the expected type (polymorphism)
if (expectedType) {
const expectedParent = this.validators[expectedType];
if (!this.isSubtypeOf(validator, expectedParent)) {
return ctx.failure(new validators_1.TypeMismatch(path, expectedType, type), type);
validateClass(value, path, ctx, expectedType) {
return __awaiter(this, void 0, void 0, function* () {
if (validators_1.isNullOrUndefined(value)) {
return ctx.failure(validators_1.defaultViolations.notNull(path), value);
}
}
// 3) Validate value
return validator.validatePath(value, path, ctx);
// 1) Validate discriminator
let type;
let typePath = path;
if (validators_1.isString(this.discriminator)) {
type = value[this.discriminator];
typePath = path.property(this.discriminator);
}
else {
type = this.discriminator(value);
}
const validator = this.validators[type];
if (!validator) {
return ctx.failure(new DiscriminatorViolation(typePath, type, Object.keys(this.validators)), type);
}
// 2) Validate that the type is assignable to the expected type (polymorphism)
if (expectedType) {
const expectedParent = this.validators[expectedType];
if (!this.isSubtypeOf(validator, expectedParent)) {
return ctx.failure(new validators_1.TypeMismatch(path, expectedType, type), type);
}
}
// 3) Validate value
return validator.validatePath(value, path, ctx);
});
}

@@ -69,0 +82,0 @@ of(name) {

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -305,9 +314,11 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

}
async validatePath(value, path, ctx) {
return this.fn(value, path, ctx);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return this.fn(value, path, ctx);
});
}
}
exports.ValidatorFnWrapper = ValidatorFnWrapper;
const lenientUnknownPropertyValidator = new ValidatorFnWrapper(async (value, path, ctx) => ctx.failure(exports.defaultViolations.unknownProperty(path), value));
const strictUnknownPropertyValidator = new ValidatorFnWrapper(async (value, path, ctx) => ctx.failure(exports.defaultViolations.unknownPropertyDenied(path), value));
const lenientUnknownPropertyValidator = new ValidatorFnWrapper((value, path, ctx) => __awaiter(void 0, void 0, void 0, function* () { return ctx.failure(exports.defaultViolations.unknownProperty(path), value); }));
const strictUnknownPropertyValidator = new ValidatorFnWrapper((value, path, ctx) => __awaiter(void 0, void 0, void 0, function* () { return ctx.failure(exports.defaultViolations.unknownPropertyDenied(path), value); }));
function mergeProperties(from, to) {

@@ -367,91 +378,97 @@ if (from) {

}
async validateFilteredPath(value, path, ctx, filter) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (typeof value !== 'object') {
return ctx.failure(exports.defaultViolations.object(path), value);
}
const convertedObject = {};
const cycleResult = ctx.registerObject(value, path, convertedObject);
if (cycleResult) {
return cycleResult;
}
let violations = [];
const propertyResults = [];
for (const key in this.properties) {
propertyResults.push(validateProperty(key, value[key], this.properties[key]));
}
for (const key in this.localProperties) {
propertyResults.push(validateProperty(key, value[key], this.localProperties[key]));
}
for (const key in value) {
if (!this.properties[key] && !this.localProperties[key]) {
propertyResults.push(validateAdditionalProperty(key, value[key], this.additionalProperties));
validateFilteredPath(value, path, ctx, filter) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
}
let validationChain = Promise.all(propertyResults).then(_ => {
if (violations.length === 0) {
return ctx.success(convertedObject);
if (typeof value !== 'object') {
return ctx.failure(exports.defaultViolations.object(path), value);
}
return ctx.failure(violations, convertedObject);
});
if (this.nextValidator) {
validationChain = validationChain.then(result => (result.isSuccess() ? this.nextValidator.validatePath(result.getValue(), path, ctx) : result));
}
if (this.localNextValidator) {
validationChain = validationChain.then(result => (result.isSuccess() ? this.localNextValidator.validatePath(result.getValue(), path, ctx) : result));
}
return validationChain;
function validateProperty(key, currentValue, validator) {
if (!filter(key)) {
return Promise.resolve(ctx.success(undefined));
const convertedObject = {};
const cycleResult = ctx.registerObject(value, path, convertedObject);
if (cycleResult) {
return cycleResult;
}
// Assign for property order
convertedObject[key] = undefined;
return validator.validatePath(currentValue, path.property(key), ctx).then(result => {
if (result.isSuccess()) {
const newValue = result.getValue();
if (newValue !== undefined) {
convertedObject[key] = newValue;
}
else {
delete convertedObject[key];
}
let violations = [];
const propertyResults = [];
for (const key in this.properties) {
propertyResults.push(validateProperty(key, value[key], this.properties[key]));
}
for (const key in this.localProperties) {
propertyResults.push(validateProperty(key, value[key], this.localProperties[key]));
}
for (const key in value) {
if (!this.properties[key] && !this.localProperties[key]) {
propertyResults.push(validateAdditionalProperty(key, value[key], this.additionalProperties));
}
else {
delete convertedObject[key];
violations = violations.concat(result.getViolations());
}
let validationChain = Promise.all(propertyResults).then(_ => {
if (violations.length === 0) {
return ctx.success(convertedObject);
}
return result;
return ctx.failure(violations, convertedObject);
});
}
async function validateAdditionalProperty(key, originalValue, additionalProperties) {
const keyPath = path.property(key);
let currentValue = originalValue;
let validKey = false;
let result;
for (const entryValidator of additionalProperties) {
result = await entryValidator.keyValidator.validatePath(key, keyPath, ctx);
if (result.isSuccess()) {
validKey = true;
result = await validateProperty(key, currentValue, entryValidator.valueValidator);
if (this.nextValidator) {
const validator = this.nextValidator;
validationChain = validationChain.then(result => (result.isSuccess() ? validator.validatePath(result.getValue(), path, ctx) : result));
}
if (this.localNextValidator) {
const validator = this.localNextValidator;
validationChain = validationChain.then(result => (result.isSuccess() ? validator.validatePath(result.getValue(), path, ctx) : result));
}
return validationChain;
function validateProperty(key, currentValue, validator) {
if (!filter(key)) {
return Promise.resolve(ctx.success(undefined));
}
// Assign for property order
convertedObject[key] = undefined;
return validator.validatePath(currentValue, path.property(key), ctx).then(result => {
if (result.isSuccess()) {
currentValue = result.getValue();
const newValue = result.getValue();
if (newValue !== undefined) {
convertedObject[key] = newValue;
}
else {
delete convertedObject[key];
}
}
else {
return result;
delete convertedObject[key];
violations = violations.concat(result.getViolations());
}
}
return result;
});
}
if (!validKey) {
if (additionalProperties.length == 1 && result) {
// Only one kind of key accepted -> give out violations related to that
violations = violations.concat(result.getViolations());
}
else {
return validateProperty(key, originalValue, lenientUnknownPropertyValidator);
}
function validateAdditionalProperty(key, originalValue, additionalProperties) {
return __awaiter(this, void 0, void 0, function* () {
const keyPath = path.property(key);
let currentValue = originalValue;
let validKey = false;
let result;
for (const entryValidator of additionalProperties) {
result = yield entryValidator.keyValidator.validatePath(key, keyPath, ctx);
if (result.isSuccess()) {
validKey = true;
result = yield validateProperty(key, currentValue, entryValidator.valueValidator);
if (result.isSuccess()) {
currentValue = result.getValue();
}
else {
return result;
}
}
}
if (!validKey) {
if (additionalProperties.length == 1 && result) {
// Only one kind of key accepted -> give out violations related to that
violations = violations.concat(result.getViolations());
}
else {
return validateProperty(key, originalValue, lenientUnknownPropertyValidator);
}
}
});
}
}
});
}

@@ -470,12 +487,14 @@ }

}
async validatePath(value, path, ctx) {
if (value === undefined) {
return ctx.success(undefined);
}
if (typeof value !== 'object' || value === null) {
const object = {};
object[this.property] = value;
return ctx.success(object);
}
return ctx.success(value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (value === undefined) {
return ctx.success(undefined);
}
if (typeof value !== 'object' || value === null) {
const object = {};
object[this.property] = value;
return ctx.success(object);
}
return ctx.success(value);
});
}

@@ -496,30 +515,32 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!Array.isArray(value)) {
return ctx.failure(new TypeMismatch(path, 'array'), value);
}
const convertedArray = [];
const cycleResult = ctx.registerObject(value, path, convertedArray);
if (cycleResult) {
return cycleResult;
}
const array = value;
let violations = [];
const promises = array.map((value, i) => this.items.validatePath(value, path.index(i), ctx).then(result => {
if (result.isSuccess()) {
convertedArray[i] = result.getValue();
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
else {
violations = violations.concat(result.getViolations());
if (!Array.isArray(value)) {
return ctx.failure(new TypeMismatch(path, 'array'), value);
}
return result;
}));
return Promise.all(promises).then(_ => {
if (violations.length == 0) {
return ctx.success(convertedArray);
const convertedArray = [];
const cycleResult = ctx.registerObject(value, path, convertedArray);
if (cycleResult) {
return cycleResult;
}
return ctx.failure(violations, value);
const array = value;
let violations = [];
const promises = array.map((value, i) => this.items.validatePath(value, path.index(i), ctx).then(result => {
if (result.isSuccess()) {
convertedArray[i] = result.getValue();
}
else {
violations = violations.concat(result.getViolations());
}
return result;
}));
return Promise.all(promises).then(_ => {
if (violations.length == 0) {
return ctx.success(convertedArray);
}
return ctx.failure(violations, value);
});
});

@@ -533,10 +554,15 @@ }

}
async validatePath(value, path, ctx) {
if (value === undefined) {
return super.validatePath([], path, ctx);
}
if (Array.isArray(value)) {
return super.validatePath(value, path, ctx);
}
return super.validatePath([value], path, ctx);
validatePath(value, path, ctx) {
const _super = Object.create(null, {
validatePath: { get: () => super.validatePath }
});
return __awaiter(this, void 0, void 0, function* () {
if (value === undefined) {
return _super.validatePath.call(this, [], path, ctx);
}
if (Array.isArray(value)) {
return _super.validatePath.call(this, value, path, ctx);
}
return _super.validatePath.call(this, [value], path, ctx);
});
}

@@ -551,13 +577,15 @@ }

}
async validatePath(value, path, ctx) {
return this.firstValidator.validatePath(value, path, ctx).then(firstResult => {
if (firstResult.isSuccess()) {
return this.nextValidator.validatePath(firstResult.getValue(), path, ctx).then(nextResult => {
if (nextResult.isSuccess()) {
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return this.firstValidator.validatePath(value, path, ctx).then(firstResult => {
if (firstResult.isSuccess()) {
return this.nextValidator.validatePath(firstResult.getValue(), path, ctx).then(nextResult => {
if (nextResult.isSuccess()) {
return nextResult;
}
return nextResult;
}
return nextResult;
});
}
return firstResult;
});
}
return firstResult;
});
});

@@ -572,8 +600,10 @@ }

}
async validatePath(value, path, ctx) {
return this.validator.validatePath(value, path, ctx).then(result => {
if (result.isSuccess()) {
return ctx.success(value);
}
return result;
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return this.validator.validatePath(value, path, ctx).then(result => {
if (result.isSuccess()) {
return ctx.success(value);
}
return result;
});
});

@@ -589,16 +619,20 @@ }

}
async validatePath(value, path, ctx) {
const validators = this.validators;
return validateNext(value, 0);
async function validateNext(currentValue, i) {
if (i < validators.length) {
return validators[i].validatePath(currentValue, path, ctx).then(result => {
if (result.isSuccess()) {
return validateNext(result.getValue(), i + 1);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
const validators = this.validators;
return validateNext(value, 0);
function validateNext(currentValue, i) {
return __awaiter(this, void 0, void 0, function* () {
if (i < validators.length) {
return validators[i].validatePath(currentValue, path, ctx).then(result => {
if (result.isSuccess()) {
return validateNext(result.getValue(), i + 1);
}
return result;
});
}
return result;
return ctx.success(currentValue);
});
}
return ctx.success(currentValue);
}
});
}

@@ -612,22 +646,24 @@ }

}
async validatePath(value, path, ctx) {
let matches = 0;
let newValue = null;
return Promise.all(validateAll(this.validators)).then(_ => {
if (matches === 1) {
return ctx.success(newValue);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
let matches = 0;
let newValue = null;
return Promise.all(validateAll(this.validators)).then(_ => {
if (matches === 1) {
return ctx.success(newValue);
}
return ctx.failure(exports.defaultViolations.oneOf(matches, path), value);
});
function validateAll(validators) {
return validators.map(validator => {
return validator.validatePath(value, path, ctx).then(result => {
if (result.isSuccess()) {
matches++;
newValue = result.getValue();
}
return result;
});
});
}
return ctx.failure(exports.defaultViolations.oneOf(matches, path), value);
});
function validateAll(validators) {
return validators.map(validator => {
return validator.validatePath(value, path, ctx).then(result => {
if (result.isSuccess()) {
matches++;
newValue = result.getValue();
}
return result;
});
});
}
}

@@ -642,13 +678,15 @@ }

}
async validatePath(value, path, ctx) {
for (let i = 0; i < this.conditionals.length; i++) {
const conditional = this.conditionals[i];
if (conditional.fn(value, path, ctx)) {
return conditional.validator.validatePath(value, path, ctx);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
for (let i = 0; i < this.conditionals.length; i++) {
const conditional = this.conditionals[i];
if (conditional.fn(value, path, ctx)) {
return conditional.validator.validatePath(value, path, ctx);
}
}
}
if (this.elseValidator) {
return this.elseValidator.validatePath(value, path, ctx);
}
return ctx.success(value);
if (this.elseValidator) {
return this.elseValidator.validatePath(value, path, ctx);
}
return ctx.success(value);
});
}

@@ -682,15 +720,17 @@ elseIf(fn, ...allOf) {

}
async validatePath(value, path, ctx) {
if (ctx.options.group) {
for (let i = 0; i < this.whenGroups.length; i++) {
const whenGroup = this.whenGroups[i];
if (ctx.options.group.includes(whenGroup.group)) {
return whenGroup.validator.validatePath(value, path, ctx);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (ctx.options.group) {
for (let i = 0; i < this.whenGroups.length; i++) {
const whenGroup = this.whenGroups[i];
if (ctx.options.group.includes(whenGroup.group)) {
return whenGroup.validator.validatePath(value, path, ctx);
}
}
}
}
if (this.otherwiseValidator) {
return this.otherwiseValidator.validatePath(value, path, ctx);
}
return ctx.success(value);
if (this.otherwiseValidator) {
return this.otherwiseValidator.validatePath(value, path, ctx);
}
return ctx.success(value);
});
}

@@ -725,43 +765,45 @@ whenGroup(group, ...allOf) {

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!(value instanceof Map)) {
return ctx.failure(new TypeMismatch(path, 'Map'), value);
}
const map = value;
const convertedMap = this.jsonSafeMap ? new JsonMap() : new Map();
const promises = [];
let violations = [];
let i = 0;
map.forEach((value, key) => {
const entryPath = path.index(i++);
// TODO: Refactor key path as index(0)
const keyPromise = this.keys.validatePath(key, entryPath.property('key'), ctx);
// TODO: Refactor value path as index(1)
const valuePromise = this.values.validatePath(value, entryPath.property('value'), ctx);
promises.push(Promise.all([keyPromise, valuePromise]).then(results => {
const keyResult = results[0];
const valueResult = results[1];
const keySuccess = handleResult(keyResult);
const valueSuccess = handleResult(valueResult);
if (keySuccess && valueSuccess) {
convertedMap.set(keyResult.getValue(), valueResult.getValue());
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!(value instanceof Map)) {
return ctx.failure(new TypeMismatch(path, 'Map'), value);
}
const map = value;
const convertedMap = this.jsonSafeMap ? new JsonMap() : new Map();
const promises = [];
let violations = [];
let i = 0;
map.forEach((value, key) => {
const entryPath = path.index(i++);
// TODO: Refactor key path as index(0)
const keyPromise = this.keys.validatePath(key, entryPath.property('key'), ctx);
// TODO: Refactor value path as index(1)
const valuePromise = this.values.validatePath(value, entryPath.property('value'), ctx);
promises.push(Promise.all([keyPromise, valuePromise]).then(results => {
const keyResult = results[0];
const valueResult = results[1];
const keySuccess = handleResult(keyResult);
const valueSuccess = handleResult(valueResult);
if (keySuccess && valueSuccess) {
convertedMap.set(keyResult.getValue(), valueResult.getValue());
}
}));
});
return Promise.all(promises).then(_ => {
if (violations.length > 0) {
return ctx.failure(violations, value);
}
}));
});
return Promise.all(promises).then(_ => {
if (violations.length > 0) {
return ctx.failure(violations, value);
return ctx.success(convertedMap);
});
function handleResult(result) {
if (result.isFailure()) {
violations = violations.concat(result.getViolations());
return false;
}
return true;
}
return ctx.success(convertedMap);
});
function handleResult(result) {
if (result.isFailure()) {
violations = violations.concat(result.getViolations());
return false;
}
return true;
}
}

@@ -776,29 +818,34 @@ }

}
async validatePath(value, path, ctx) {
if (value instanceof Map) {
return super.validatePath(value, path, ctx);
}
if (Array.isArray(value)) {
let violations = [];
const map = new Map();
for (let i = 0; i < value.length; i++) {
const entry = value[i];
if (Array.isArray(entry)) {
if (entry.length >= 1 && entry.length <= 2) {
map.set(entry[0], entry[1]);
validatePath(value, path, ctx) {
const _super = Object.create(null, {
validatePath: { get: () => super.validatePath }
});
return __awaiter(this, void 0, void 0, function* () {
if (value instanceof Map) {
return _super.validatePath.call(this, value, path, ctx);
}
if (Array.isArray(value)) {
let violations = [];
const map = new Map();
for (let i = 0; i < value.length; i++) {
const entry = value[i];
if (Array.isArray(entry)) {
if (entry.length >= 1 && entry.length <= 2) {
map.set(entry[0], entry[1]);
}
else {
violations.push(new SizeViolation(path.index(i), 1, 2));
}
}
else {
violations.push(new SizeViolation(path.index(i), 1, 2));
violations.push(new TypeMismatch(path.index(i), 'Array'));
}
}
else {
violations.push(new TypeMismatch(path.index(i), 'Array'));
if (violations.length > 0) {
return ctx.failure(violations, value);
}
return _super.validatePath.call(this, map, path, ctx);
}
if (violations.length > 0) {
return ctx.failure(violations, value);
}
return super.validatePath(map, path, ctx);
}
return ctx.failure(new TypeMismatch(path, 'Map OR array of [key, value] arrays'), value);
return ctx.failure(new TypeMismatch(path, 'Map OR array of [key, value] arrays'), value);
});
}

@@ -817,4 +864,6 @@ }

class AnyValidator extends Validator {
async validatePath(value, path, ctx) {
return ctx.success(value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return ctx.success(value);
});
}

@@ -836,10 +885,12 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (isString(value)) {
return ctx.success(value);
}
return ctx.failure(exports.defaultViolations.string(value, path), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (isString(value)) {
return ctx.success(value);
}
return ctx.failure(exports.defaultViolations.string(value, path), value);
});
}

@@ -849,13 +900,15 @@ }

class StringNormalizer extends Validator {
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (isString(value)) {
return ctx.success(value);
}
if (isSimplePrimitive(value)) {
return ctx.success(String(value));
}
return ctx.failure(new TypeMismatch(path, 'primitive value', value), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (isString(value)) {
return ctx.success(value);
}
if (isSimplePrimitive(value)) {
return ctx.success(String(value));
}
return ctx.failure(new TypeMismatch(path, 'primitive value', value), value);
});
}

@@ -865,4 +918,6 @@ }

class NotNullOrUndefinedValidator extends Validator {
async validatePath(value, path, ctx) {
return isNullOrUndefined(value) ? ctx.failure(exports.defaultViolations.notNull(path), value) : ctx.success(value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return isNullOrUndefined(value) ? ctx.failure(exports.defaultViolations.notNull(path), value) : ctx.success(value);
});
}

@@ -872,4 +927,6 @@ }

class IsNullOrUndefinedValidator extends Validator {
async validatePath(value, path, ctx) {
return isNullOrUndefined(value) ? ctx.success(value) : ctx.failure(new TypeMismatch(path, 'NullOrUndefined', value), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return isNullOrUndefined(value) ? ctx.success(value) : ctx.failure(new TypeMismatch(path, 'NullOrUndefined', value), value);
});
}

@@ -879,4 +936,6 @@ }

class NotEmptyValidator extends Validator {
async validatePath(value, path, ctx) {
return !isNullOrUndefined(value) && isNumber(value.length) && value.length > 0 ? ctx.success(value) : ctx.failure(exports.defaultViolations.notEmpty(path), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return !isNullOrUndefined(value) && isNumber(value.length) && value.length > 0 ? ctx.success(value) : ctx.failure(exports.defaultViolations.notEmpty(path), value);
});
}

@@ -894,13 +953,15 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isNumber(value.length)) {
return ctx.failure(new TypeMismatch(path, 'value with numeric length field'), value);
}
if (value.length < this.min || value.length > this.max) {
return ctx.failure(exports.defaultViolations.size(this.min, this.max, path), value);
}
return ctx.success(value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isNumber(value.length)) {
return ctx.failure(new TypeMismatch(path, 'value with numeric length field'), value);
}
if (value.length < this.min || value.length > this.max) {
return ctx.failure(exports.defaultViolations.size(this.min, this.max, path), value);
}
return ctx.success(value);
});
}

@@ -913,14 +974,16 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notBlank(path), value);
}
if (!isString(value)) {
return ctx.failure(exports.defaultViolations.string(value, path), value);
}
const trimmed = value.trim();
if (trimmed === '') {
return ctx.failure(exports.defaultViolations.notBlank(path), value);
}
return ctx.success(value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notBlank(path), value);
}
if (!isString(value)) {
return ctx.failure(exports.defaultViolations.string(value, path), value);
}
const trimmed = value.trim();
if (trimmed === '') {
return ctx.failure(exports.defaultViolations.notBlank(path), value);
}
return ctx.success(value);
});
}

@@ -930,10 +993,12 @@ }

class BooleanValidator extends Validator {
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (typeof value === 'boolean') {
return ctx.success(value);
}
return ctx.failure(exports.defaultViolations.boolean(value, path), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (typeof value === 'boolean') {
return ctx.success(value);
}
return ctx.failure(exports.defaultViolations.boolean(value, path), value);
});
}

@@ -948,25 +1013,27 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (typeof value === 'boolean') {
return ctx.success(value);
}
if (value instanceof Boolean) {
return ctx.success(value.valueOf());
}
if (isString(value)) {
if (this.truePattern.test(value)) {
return ctx.success(true);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (this.falsePattern.test(value)) {
return ctx.success(false);
if (typeof value === 'boolean') {
return ctx.success(value);
}
if (value instanceof Boolean) {
return ctx.success(value.valueOf());
}
if (isString(value)) {
if (this.truePattern.test(value)) {
return ctx.success(true);
}
if (this.falsePattern.test(value)) {
return ctx.success(false);
}
return ctx.failure(exports.defaultViolations.boolean(value, path), value);
}
else if (isNumber(value)) {
return ctx.success(!!value);
}
return ctx.failure(exports.defaultViolations.boolean(value, path), value);
}
else if (isNumber(value)) {
return ctx.success(!!value);
}
return ctx.failure(exports.defaultViolations.boolean(value, path), value);
});
}

@@ -989,20 +1056,24 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isNumber(value)) {
return ctx.failure(exports.defaultViolations.number(value, this.format, path), value);
}
return this.validateFormat(value, path, ctx);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isNumber(value)) {
return ctx.failure(exports.defaultViolations.number(value, this.format, path), value);
}
return this.validateFormat(value, path, ctx);
});
}
async validateFormat(value, path, ctx) {
switch (this.format) {
case NumberFormat.integer:
if (!Number.isInteger(value)) {
return ctx.failure(exports.defaultViolations.number(value, this.format, path), value);
}
break;
}
return ctx.success(value);
validateFormat(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
switch (this.format) {
case NumberFormat.integer:
if (!Number.isInteger(value)) {
return ctx.failure(exports.defaultViolations.number(value, this.format, path), value);
}
break;
}
return ctx.success(value);
});
}

@@ -1015,20 +1086,25 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (isNumber(value)) {
return super.validateFormat(value, path, ctx);
}
if (isString(value)) {
if (value.trim() === '') {
validatePath(value, path, ctx) {
const _super = Object.create(null, {
validateFormat: { get: () => super.validateFormat }
});
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (isNumber(value)) {
return _super.validateFormat.call(this, value, path, ctx);
}
if (isString(value)) {
if (value.trim() === '') {
return ctx.failure(exports.defaultViolations.number(value, this.format, path), value);
}
const nbr = Number(value);
if (isNumber(nbr)) {
return this.validateFormat(nbr, path, ctx);
}
return ctx.failure(exports.defaultViolations.number(value, this.format, path), value);
}
const nbr = Number(value);
if (isNumber(nbr)) {
return this.validateFormat(nbr, path, ctx);
}
return ctx.failure(exports.defaultViolations.number(value, this.format, path), value);
}
return ctx.failure(exports.defaultViolations.number(value, this.format, path), value);
});
}

@@ -1043,18 +1119,20 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isNumber(value)) {
return ctx.failure(exports.defaultViolations.number(value, NumberFormat.number, path), value);
}
if (this.inclusive) {
if (value < this.min) {
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isNumber(value)) {
return ctx.failure(exports.defaultViolations.number(value, NumberFormat.number, path), value);
}
if (this.inclusive) {
if (value < this.min) {
return ctx.failure(exports.defaultViolations.min(this.min, this.inclusive, value, path), value);
}
}
else if (value <= this.min) {
return ctx.failure(exports.defaultViolations.min(this.min, this.inclusive, value, path), value);
}
}
else if (value <= this.min) {
return ctx.failure(exports.defaultViolations.min(this.min, this.inclusive, value, path), value);
}
return ctx.success(value);
return ctx.success(value);
});
}

@@ -1069,18 +1147,20 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isNumber(value)) {
return ctx.failure(exports.defaultViolations.number(value, NumberFormat.number, path), value);
}
if (this.inclusive) {
if (value > this.max) {
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isNumber(value)) {
return ctx.failure(exports.defaultViolations.number(value, NumberFormat.number, path), value);
}
if (this.inclusive) {
if (value > this.max) {
return ctx.failure(exports.defaultViolations.max(this.max, this.inclusive, value, path), value);
}
}
else if (value >= this.max) {
return ctx.failure(exports.defaultViolations.max(this.max, this.inclusive, value, path), value);
}
}
else if (value >= this.max) {
return ctx.failure(exports.defaultViolations.max(this.max, this.inclusive, value, path), value);
}
return ctx.success(value);
return ctx.success(value);
});
}

@@ -1095,11 +1175,13 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
const isValid = Object.values(this.enumType).includes(value);
if (isValid) {
return ctx.success(value);
}
return ctx.failure(exports.defaultViolations.enum(this.name, value, path), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
const isValid = Object.values(this.enumType).includes(value);
if (isValid) {
return ctx.success(value);
}
return ctx.failure(exports.defaultViolations.enum(this.name, value, path), value);
});
}

@@ -1115,7 +1197,9 @@ }

}
async validatePath(value, path, ctx) {
if (!this.fn(value, path, ctx)) {
return ctx.failure(new Violation(this.path ? this.path.connectTo(path) : path, this.type), value);
}
return ctx.success(value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.fn(value, path, ctx)) {
return ctx.failure(new Violation(this.path ? this.path.connectTo(path) : path, this.type), value);
}
return ctx.success(value);
});
}

@@ -1129,7 +1213,9 @@ }

}
async validatePath(value, path, ctx) {
if (deep_equal_1.default(value, this.expectedValue)) {
return ctx.success(value);
}
return ctx.failure(new HasValueViolation(path, this.expectedValue, value), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (deep_equal_1.default(value, this.expectedValue)) {
return ctx.success(value);
}
return ctx.failure(new HasValueViolation(path, this.expectedValue, value), value);
});
}

@@ -1157,29 +1243,31 @@ }

}
async validatePath(value, path, ctx) {
let violations = [];
let convertedValue;
return Promise.all(validateAll(this.validators)).then(_ => {
if (violations.length == 0) {
return ctx.success(convertedValue !== undefined ? convertedValue : value);
}
return ctx.failure(violations, value);
});
function validateAll(validators) {
return validators.map(validator => {
return validator.validatePath(value, path, ctx).then(result => {
if (!result.isSuccess()) {
violations = violations.concat(result.getViolations());
}
else {
const resultValue = result.getValue();
if (resultValue !== value) {
if (convertedValue !== undefined && !deep_equal_1.default(resultValue, convertedValue)) {
throw new Error('Conflicting conversions');
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
let violations = [];
let convertedValue;
return Promise.all(validateAll(this.validators)).then(_ => {
if (violations.length == 0) {
return ctx.success(convertedValue !== undefined ? convertedValue : value);
}
return ctx.failure(violations, value);
});
function validateAll(validators) {
return validators.map(validator => {
return validator.validatePath(value, path, ctx).then(result => {
if (!result.isSuccess()) {
violations = violations.concat(result.getViolations());
}
else {
const resultValue = result.getValue();
if (resultValue !== value) {
if (convertedValue !== undefined && !deep_equal_1.default(resultValue, convertedValue)) {
throw new Error('Conflicting conversions');
}
convertedValue = resultValue;
}
convertedValue = resultValue;
}
}
});
});
});
}
}
});
}

@@ -1193,20 +1281,22 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
let dateValue;
if (isString(value) || isNumber(value)) {
dateValue = new Date(value);
}
else {
dateValue = value;
}
if (dateValue instanceof Date) {
if (isNaN(dateValue.getTime())) {
return ctx.failure(exports.defaultViolations.date(value, path), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
return ctx.success(dateValue);
}
return ctx.failure(exports.defaultViolations.date(value, path, this.dateType), value);
let dateValue;
if (isString(value) || isNumber(value)) {
dateValue = new Date(value);
}
else {
dateValue = value;
}
if (dateValue instanceof Date) {
if (isNaN(dateValue.getTime())) {
return ctx.failure(exports.defaultViolations.date(value, path), value);
}
return ctx.success(dateValue);
}
return ctx.failure(exports.defaultViolations.date(value, path, this.dateType), value);
});
}

@@ -1220,13 +1310,15 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isString(value)) {
return ctx.failure(exports.defaultViolations.string(value, path), value);
}
if (this.regExp.test(value)) {
return ctx.success(value);
}
return ctx.failure(exports.defaultViolations.pattern(this.regExp, value, path), value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (!isString(value)) {
return ctx.failure(exports.defaultViolations.string(value, path), value);
}
if (this.regExp.test(value)) {
return ctx.success(value);
}
return ctx.failure(exports.defaultViolations.pattern(this.regExp, value, path), value);
});
}

@@ -1244,13 +1336,18 @@ toJSON() {

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (isString(value)) {
return super.validatePath(value, path, ctx);
}
if (isSimplePrimitive(value)) {
return super.validatePath(String(value), path, ctx);
}
return ctx.failure(new TypeMismatch(path, 'primitive value', value), value);
validatePath(value, path, ctx) {
const _super = Object.create(null, {
validatePath: { get: () => super.validatePath }
});
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.failure(exports.defaultViolations.notNull(path), value);
}
if (isString(value)) {
return _super.validatePath.call(this, value, path, ctx);
}
if (isSimplePrimitive(value)) {
return _super.validatePath.call(this, String(value), path, ctx);
}
return ctx.failure(new TypeMismatch(path, 'primitive value', value), value);
});
}

@@ -1269,7 +1366,9 @@ }

}
async validatePath(value, path, ctx) {
if (isNullOrUndefined(value)) {
return ctx.success(value);
}
return this.validator.validatePath(value, path, ctx);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
if (isNullOrUndefined(value)) {
return ctx.success(value);
}
return this.validator.validatePath(value, path, ctx);
});
}

@@ -1284,15 +1383,17 @@ }

}
async validatePath(value, path, ctx) {
try {
const maybePromise = this.fn(value, path, ctx);
return isPromise(maybePromise)
? await maybePromise.then(result => this.handleResult(result, value, ctx))
: this.handleResult(maybePromise, value, ctx);
}
catch (error) {
if (error instanceof ValidationError) {
return ctx.failure(error.violations, value);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
try {
const maybePromise = this.fn(value, path, ctx);
return isPromise(maybePromise)
? yield maybePromise.then(result => this.handleResult(result, value, ctx))
: this.handleResult(maybePromise, value, ctx);
}
return ctx.failure(new ErrorViolation(path, this.error || error), value);
}
catch (error) {
if (error instanceof ValidationError) {
return ctx.failure(error.violations, value);
}
return ctx.failure(new ErrorViolation(path, this.error || error), value);
}
});
}

@@ -1312,4 +1413,6 @@ handleResult(result, value, ctx) {

class IgnoreValidator extends Validator {
async validatePath(value, path, ctx) {
return ctx.success(undefined);
validatePath(value, path, ctx) {
return __awaiter(this, void 0, void 0, function* () {
return ctx.success(undefined);
});
}

@@ -1316,0 +1419,0 @@ }

{
"name": "@finnair/v-validation",
"version": "0.7.0",
"version": "0.8.0",
"private": false,

@@ -27,3 +27,3 @@ "description": "V-validation core package",

"dependencies": {
"@finnair/path": "^0.7.0",
"@finnair/path": "^0.8.0",
"@types/deep-equal": "1.0.1",

@@ -34,3 +34,3 @@ "@types/uuid-validate": "0.0.1",

},
"gitHead": "fb72cb84d63f59b6e7374f40852fb8ad212b0b53"
"gitHead": "fabb67c9516dc2215ea65e4cce1b94c86434fd18"
}
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