@finnair/v-validation
Advanced tools
Comparing version 3.0.0-alpha.1 to 3.0.0-alpha.2
@@ -6,2 +6,20 @@ # Change Log | ||
# [3.0.0-alpha.2](https://github.com/finnair/v-validation/compare/v2.0.0...v3.0.0-alpha.2) (2022-09-28) | ||
### Features | ||
* Use SyncPromise also in schema and luxon ([792fbb4](https://github.com/finnair/v-validation/commit/792fbb4c78d07ea085d669312fef5e44e7a397fd)) | ||
### Performance Improvements | ||
* Optimize CompositionValidator by using async/await instead of nested callbacks ([026f2c3](https://github.com/finnair/v-validation/commit/026f2c3d23d4dbb644338d3d127e83dfbee3bd9c)) | ||
* Optimize loops ([8c5c915](https://github.com/finnair/v-validation/commit/8c5c915794fb39ab6fd41ec7d3ac4e4f028ed7fa)) | ||
* Optimize NextValidator by removing unnecessary callback ([4599c04](https://github.com/finnair/v-validation/commit/4599c04c435871ec6eda5fdf3167267256fd5808)) | ||
# [3.0.0-alpha.1](https://github.com/finnair/v-validation/compare/v2.0.0...v3.0.0-alpha.1) (2022-09-26) | ||
@@ -8,0 +26,0 @@ |
@@ -235,3 +235,3 @@ import { Path } from '@finnair/path'; | ||
constructor(validators: Validator | Validator[]); | ||
validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<ValidationResult>; | ||
validatePath(value: any, path: Path, ctx: ValidationContext): Promise<ValidationResult>; | ||
} | ||
@@ -295,3 +295,2 @@ export declare class OneOfValidator extends Validator { | ||
export declare class StringValidator extends Validator { | ||
constructor(); | ||
validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<ValidationResult>; | ||
@@ -318,3 +317,2 @@ } | ||
export declare class NotBlankValidator extends Validator { | ||
constructor(); | ||
validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<ValidationResult>; | ||
@@ -383,3 +381,3 @@ } | ||
export declare class DateValidator extends Validator { | ||
private readonly dateType; | ||
readonly dateType: string; | ||
constructor(dateType: string); | ||
@@ -389,3 +387,3 @@ validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<ValidationResult>; | ||
export declare class PatternValidator extends Validator { | ||
private readonly regExp; | ||
readonly regExp: RegExp; | ||
constructor(pattern: string | RegExp, flags?: string); | ||
@@ -407,4 +405,4 @@ validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<ValidationResult>; | ||
export declare class ValueMapper extends Validator { | ||
private readonly fn; | ||
private readonly error?; | ||
readonly fn: MappingFn; | ||
readonly error?: any; | ||
constructor(fn: MappingFn, error?: any); | ||
@@ -411,0 +409,0 @@ validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<ValidationResult>; |
@@ -441,25 +441,30 @@ "use strict"; | ||
} | ||
const convertedObject = {}; | ||
const cycleResult = ctx.registerObject(value, path, convertedObject); | ||
const context = { | ||
path, | ||
ctx, | ||
filter, | ||
convertedObject: {}, | ||
violations: [], | ||
}; | ||
const cycleResult = ctx.registerObject(value, path, context.convertedObject); | ||
if (cycleResult) { | ||
return ctx.promise(cycleResult); | ||
} | ||
let violations = []; | ||
const propertyResults = []; | ||
for (const key in this.properties) { | ||
propertyResults.push(validateProperty(key, value[key], this.properties[key])); | ||
propertyResults.push(validateProperty(key, value[key], this.properties[key], context)); | ||
} | ||
for (const key in this.localProperties) { | ||
propertyResults.push(validateProperty(key, value[key], this.localProperties[key])); | ||
propertyResults.push(validateProperty(key, value[key], this.localProperties[key], context)); | ||
} | ||
for (const key in value) { | ||
if (!this.properties[key] && !this.localProperties[key]) { | ||
propertyResults.push(validateAdditionalProperty(key, value[key], this.additionalProperties)); | ||
propertyResults.push(validateAdditionalProperty(key, value[key], this.additionalProperties, context)); | ||
} | ||
} | ||
let validationChain = Promise.all(propertyResults).then(_ => { | ||
if (violations.length === 0) { | ||
return ctx.successPromise(convertedObject); | ||
if (context.violations.length === 0) { | ||
return ctx.successPromise(context.convertedObject); | ||
} | ||
return ctx.failurePromise(violations, convertedObject); | ||
return ctx.failurePromise(context.violations, context.convertedObject); | ||
}); | ||
@@ -475,59 +480,62 @@ if (this.nextValidator) { | ||
return validationChain; | ||
function validateProperty(key, currentValue, validator) { | ||
if (!filter(key)) { | ||
return Promise.resolve(ctx.success(undefined)); | ||
} | ||
} | ||
exports.ObjectValidator = ObjectValidator; | ||
function validateProperty(key, currentValue, validator, context) { | ||
if (!context.filter(key)) { | ||
return context.ctx.successPromise(undefined); | ||
} | ||
// Assign for property order | ||
context.convertedObject[key] = undefined; | ||
return validator.validatePath(currentValue, context.path.property(key), context.ctx).then(result => { | ||
if (result.isSuccess()) { | ||
const newValue = result.getValue(); | ||
if (newValue !== undefined) { | ||
context.convertedObject[key] = newValue; | ||
} | ||
// Assign for property order | ||
convertedObject[key] = undefined; | ||
return validator.validatePath(currentValue, path.property(key), ctx).then(result => { | ||
else { | ||
delete context.convertedObject[key]; | ||
} | ||
} | ||
else { | ||
delete context.convertedObject[key]; | ||
context.violations = context.violations.concat(result.getViolations()); | ||
} | ||
return result; | ||
}); | ||
} | ||
function validateAdditionalProperty(key, originalValue, additionalProperties, context) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!context.filter(key)) { | ||
return Promise.resolve(context.ctx.success(undefined)); | ||
} | ||
const keyPath = context.path.property(key); | ||
let currentValue = originalValue; | ||
let validKey = false; | ||
let result; | ||
for (let i = 0; i < additionalProperties.length; i++) { | ||
const entryValidator = additionalProperties[i]; | ||
result = yield entryValidator.keyValidator.validatePath(key, keyPath, context.ctx); | ||
if (result.isSuccess()) { | ||
validKey = true; | ||
result = yield validateProperty(key, currentValue, entryValidator.valueValidator, context); | ||
if (result.isSuccess()) { | ||
const newValue = result.getValue(); | ||
if (newValue !== undefined) { | ||
convertedObject[key] = newValue; | ||
} | ||
else { | ||
delete convertedObject[key]; | ||
} | ||
currentValue = result.getValue(); | ||
} | ||
else { | ||
delete convertedObject[key]; | ||
violations = violations.concat(result.getViolations()); | ||
return result; | ||
} | ||
return result; | ||
}); | ||
} | ||
} | ||
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 (let i = 0; i < additionalProperties.length; i++) { | ||
const entryValidator = additionalProperties[i]; | ||
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); | ||
} | ||
} | ||
}); | ||
if (!validKey) { | ||
if (additionalProperties.length == 1 && result) { | ||
// Only one kind of key accepted -> give out violations related to that | ||
context.violations = context.violations.concat(result.getViolations()); | ||
} | ||
else { | ||
return validateProperty(key, originalValue, lenientUnknownPropertyValidator, context); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
exports.ObjectValidator = ObjectValidator; | ||
/** | ||
@@ -631,8 +639,3 @@ * Converts a primitive `value` into an object `{ property: value }`. This normalizer can be used | ||
if (firstResult.isSuccess()) { | ||
return this.nextValidator.validatePath(firstResult.getValue(), path, ctx).then(nextResult => { | ||
if (nextResult.isSuccess()) { | ||
return nextResult; | ||
} | ||
return nextResult; | ||
}); | ||
return this.nextValidator.validatePath(firstResult.getValue(), path, ctx); | ||
} | ||
@@ -668,15 +671,15 @@ return firstResult; | ||
validatePath(value, path, ctx) { | ||
const validators = this.validators; | ||
return validateNext(value, 0); | ||
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); | ||
} | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let currentValue = value; | ||
for (let i = 0; i < this.validators.length; i++) { | ||
const result = yield this.validators[i].validatePath(currentValue, path, ctx); | ||
if (result.isSuccess()) { | ||
currentValue = result.getValue(); | ||
} | ||
else { | ||
return result; | ||
}); | ||
} | ||
} | ||
return ctx.successPromise(currentValue); | ||
} | ||
return ctx.success(currentValue); | ||
}); | ||
} | ||
@@ -933,5 +936,2 @@ } | ||
class StringValidator extends Validator { | ||
constructor() { | ||
super(); | ||
} | ||
validatePath(value, path, ctx) { | ||
@@ -1008,5 +1008,2 @@ if (isNullOrUndefined(value)) { | ||
class NotBlankValidator extends Validator { | ||
constructor() { | ||
super(); | ||
} | ||
validatePath(value, path, ctx) { | ||
@@ -1044,2 +1041,4 @@ if (isNullOrUndefined(value)) { | ||
this.falsePattern = falsePattern; | ||
Object.freeze(this.truePattern); | ||
Object.freeze(this.falsePattern); | ||
Object.freeze(this); | ||
@@ -1289,2 +1288,3 @@ } | ||
this.dateType = dateType; | ||
Object.freeze(this); | ||
} | ||
@@ -1316,2 +1316,4 @@ validatePath(value, path, ctx) { | ||
this.regExp = pattern instanceof RegExp ? pattern : new RegExp(pattern, flags); | ||
Object.freeze(this.regExp); | ||
Object.freeze(this); | ||
} | ||
@@ -1364,2 +1366,3 @@ validatePath(value, path, ctx) { | ||
} | ||
Object.freeze(this); | ||
} | ||
@@ -1379,2 +1382,3 @@ validatePath(value, path, ctx) { | ||
this.error = error; | ||
Object.freeze(this); | ||
} | ||
@@ -1431,2 +1435,3 @@ validatePath(value, path, ctx) { | ||
this.validator = maybeAllOfValidator(allOf); | ||
Object.freeze(this); | ||
} | ||
@@ -1433,0 +1438,0 @@ validatePath(value, path, ctx) { |
{ | ||
"name": "@finnair/v-validation", | ||
"version": "3.0.0-alpha.1", | ||
"version": "3.0.0-alpha.2", | ||
"private": false, | ||
@@ -27,3 +27,3 @@ "description": "V-validation core package", | ||
"dependencies": { | ||
"@finnair/path": "^3.0.0-alpha.1", | ||
"@finnair/path": "^3.0.0-alpha.2", | ||
"@types/deep-equal": "1.0.1", | ||
@@ -34,3 +34,3 @@ "@types/uuid-validate": "0.0.1", | ||
}, | ||
"gitHead": "34e2b0697aaee247e03b7a65d677c7b766a7fbca" | ||
"gitHead": "d42bd7583bb684429e48ce8e5db3938cb95ffd5f" | ||
} |
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
134903
2246
Updated@finnair/path@^3.0.0-alpha.2