@finnair/v-validation
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -6,2 +6,10 @@ # Change Log | ||
# [0.9.0](https://github.com/finnair/v-validation/compare/v0.8.0...v0.9.0) (2020-09-23) | ||
**Note:** Version bump only for package @finnair/v-validation | ||
# [0.8.0](https://github.com/finnair/v-validation/compare/v0.7.0...v0.8.0) (2020-09-09) | ||
@@ -8,0 +16,0 @@ |
import { SchemaValidator, SchemaModel } from './schema'; | ||
import { Path } from '@finnair/path'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, BooleanValidator, NumberValidator, NumberNormalizer, DateValidator, ValidatorFn, ValidatorFnWrapper, MappingFn, Validator, CheckValidator, OptionalValidator, AssertTrue, IfValidator, GroupOrName, WhenGroupValidator, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, ObjectModel, ObjectValidator, ObjectNormalizer, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AllOfValidator, OneOfValidator, CompositionValidator, EnumValidator, HasValueValidator } from './validators'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, BooleanValidator, NumberValidator, NumberNormalizer, DateValidator, ValidatorFn, ValidatorFnWrapper, MappingFn, Validator, CheckValidator, OptionalValidator, AssertTrue, IfValidator, GroupOrName, WhenGroupValidator, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, ObjectModel, ObjectValidator, ObjectNormalizer, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AllOfValidator, AnyOfValidator, OneOfValidator, CompositionValidator, EnumValidator, HasValueValidator, JsonValidator } from './validators'; | ||
declare const V: { | ||
@@ -48,2 +48,3 @@ fn: (fn: ValidatorFn, type?: string | undefined) => ValidatorFnWrapper; | ||
allOf: (...validators: Validator[]) => AllOfValidator; | ||
anyOf: (...validators: Validator[]) => AnyOfValidator; | ||
oneOf: (...validators: Validator[]) => OneOfValidator; | ||
@@ -55,3 +56,4 @@ compositionOf: (...validators: Validator[]) => CompositionValidator; | ||
hasValue: (expectedValue: any) => HasValueValidator; | ||
json: (...validators: Validator[]) => JsonValidator; | ||
}; | ||
export default V; |
@@ -54,2 +54,3 @@ "use strict"; | ||
allOf: (...validators) => new validators_1.AllOfValidator(validators), | ||
anyOf: (...validators) => new validators_1.AnyOfValidator(validators), | ||
oneOf: (...validators) => new validators_1.OneOfValidator(validators), | ||
@@ -61,3 +62,4 @@ compositionOf: (...validators) => new validators_1.CompositionValidator(validators), | ||
hasValue: (expectedValue) => new validators_1.HasValueValidator(expectedValue), | ||
json: (...validators) => new validators_1.JsonValidator(validators), | ||
}; | ||
exports.default = V; |
@@ -125,2 +125,3 @@ import { Path } from '@finnair/path'; | ||
DateTime = "DateTime", | ||
AnyOf = "AnyOf", | ||
OneOf = "OneOf", | ||
@@ -235,2 +236,7 @@ Pattern = "Pattern" | ||
} | ||
export declare class AnyOfValidator extends Validator { | ||
readonly validators: Validator[]; | ||
constructor(validators: Validator[]); | ||
validatePath(value: any, path: Path, ctx: ValidationContext): Promise<ValidationResult>; | ||
} | ||
export declare class IfValidator extends Validator { | ||
@@ -403,1 +409,6 @@ readonly conditionals: Conditional[]; | ||
} | ||
export declare class JsonValidator extends Validator { | ||
readonly validator: Validator; | ||
constructor(allOf: Validator[]); | ||
validatePath(value: any, path: Path, ctx: ValidationContext): Promise<ValidationResult>; | ||
} |
@@ -15,3 +15,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IgnoreValidator = exports.isPromise = exports.ValueMapper = exports.OptionalValidator = exports.PatternNormalizer = exports.PatternValidator = exports.DateValidator = exports.AllOfValidator = exports.maybeAllOfValidator = exports.HasValueValidator = exports.AssertTrueValidator = exports.EnumValidator = exports.MaxValidator = exports.MinValidator = exports.NumberNormalizer = exports.NumberValidator = exports.isNumber = exports.NumberFormat = exports.BooleanNormalizer = exports.BooleanValidator = exports.NotBlankValidator = exports.SizeValidator = exports.NotEmptyValidator = exports.IsNullOrUndefinedValidator = exports.NotNullOrUndefinedValidator = exports.StringNormalizer = exports.StringValidator = exports.isSimplePrimitive = exports.isString = exports.AnyValidator = exports.JsonMap = exports.MapNormalizer = exports.MapValidator = exports.WhenGroup = exports.WhenGroupValidator = exports.Conditional = exports.IfValidator = exports.OneOfValidator = exports.CompositionValidator = exports.CheckValidator = exports.NextValidator = exports.ArrayNormalizer = exports.ArrayValidator = exports.MapEntryValidator = exports.ObjectNormalizer = exports.ObjectValidator = exports.mergeProperties = exports.ValidatorFnWrapper = exports.defaultViolations = exports.ValidatorType = exports.isNullOrUndefined = exports.Groups = exports.Group = exports.SizeViolation = exports.MaxViolation = exports.MinViolation = exports.OneOfMismatch = exports.PatternViolation = exports.HasValueViolation = exports.ErrorViolation = exports.EnumMismatch = exports.TypeMismatch = exports.Violation = exports.ValidationError = exports.ValidationResult = exports.Validator = exports.ValidationContext = void 0; | ||
exports.JsonValidator = exports.IgnoreValidator = exports.isPromise = exports.ValueMapper = exports.OptionalValidator = exports.PatternNormalizer = exports.PatternValidator = exports.DateValidator = exports.AllOfValidator = exports.maybeAllOfValidator = exports.HasValueValidator = exports.AssertTrueValidator = exports.EnumValidator = exports.MaxValidator = exports.MinValidator = exports.NumberNormalizer = exports.NumberValidator = exports.isNumber = exports.NumberFormat = exports.BooleanNormalizer = exports.BooleanValidator = exports.NotBlankValidator = exports.SizeValidator = exports.NotEmptyValidator = exports.IsNullOrUndefinedValidator = exports.NotNullOrUndefinedValidator = exports.StringNormalizer = exports.StringValidator = exports.isSimplePrimitive = exports.isString = exports.AnyValidator = exports.JsonMap = exports.MapNormalizer = exports.MapValidator = exports.WhenGroup = exports.WhenGroupValidator = exports.Conditional = exports.IfValidator = exports.AnyOfValidator = exports.OneOfValidator = exports.CompositionValidator = exports.CheckValidator = exports.NextValidator = exports.ArrayNormalizer = exports.ArrayValidator = exports.MapEntryValidator = exports.ObjectNormalizer = exports.ObjectValidator = exports.mergeProperties = exports.ValidatorFnWrapper = exports.defaultViolations = exports.ValidatorType = exports.isNullOrUndefined = exports.Groups = exports.Group = exports.SizeViolation = exports.MaxViolation = exports.MinViolation = exports.OneOfMismatch = exports.PatternViolation = exports.HasValueViolation = exports.ErrorViolation = exports.EnumMismatch = exports.TypeMismatch = exports.Violation = exports.ValidationError = exports.ValidationResult = exports.Validator = exports.ValidationContext = void 0; | ||
const deep_equal_1 = __importDefault(require("deep-equal")); | ||
@@ -248,2 +248,3 @@ const path_1 = require("@finnair/path"); | ||
ValidatorType["DateTime"] = "DateTime"; | ||
ValidatorType["AnyOf"] = "AnyOf"; | ||
ValidatorType["OneOf"] = "OneOf"; | ||
@@ -520,3 +521,3 @@ ValidatorType["Pattern"] = "Pattern"; | ||
if (!Array.isArray(value)) { | ||
return ctx.failure(new TypeMismatch(path, 'array'), value); | ||
return ctx.failure(new TypeMismatch(path, 'array', value), value); | ||
} | ||
@@ -666,2 +667,23 @@ const convertedArray = []; | ||
exports.OneOfValidator = OneOfValidator; | ||
class AnyOfValidator extends Validator { | ||
constructor(validators) { | ||
super(); | ||
this.validators = validators; | ||
} | ||
validatePath(value, path, ctx) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const passes = []; | ||
const failures = []; | ||
const validateAll = (validators) => __awaiter(this, void 0, void 0, function* () { | ||
for (const validator of validators) { | ||
const result = yield validator.validatePath(value, path, ctx); | ||
result.isSuccess() ? passes.push(result.getValue()) : failures.push(...result.getViolations()); | ||
} | ||
}); | ||
yield validateAll(this.validators); | ||
return passes.length > 0 ? ctx.success(passes.pop()) : ctx.failure(failures, value); | ||
}); | ||
} | ||
} | ||
exports.AnyOfValidator = AnyOfValidator; | ||
class IfValidator extends Validator { | ||
@@ -1396,1 +1418,25 @@ constructor(conditionals, elseValidator) { | ||
}); | ||
class JsonValidator extends Validator { | ||
constructor(allOf) { | ||
super(); | ||
this.validator = maybeAllOfValidator(allOf); | ||
} | ||
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); | ||
} | ||
try { | ||
const parsedValue = JSON.parse(value); | ||
return this.validator.validatePath(parsedValue, path, ctx); | ||
} | ||
catch (e) { | ||
return ctx.failure(new TypeMismatch(path, 'JSON', value), value); | ||
} | ||
}); | ||
} | ||
} | ||
exports.JsonValidator = JsonValidator; |
{ | ||
"name": "@finnair/v-validation", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"private": false, | ||
@@ -27,3 +27,3 @@ "description": "V-validation core package", | ||
"dependencies": { | ||
"@finnair/path": "^0.8.0", | ||
"@finnair/path": "^0.9.0", | ||
"@types/deep-equal": "1.0.1", | ||
@@ -34,3 +34,3 @@ "@types/uuid-validate": "0.0.1", | ||
}, | ||
"gitHead": "fabb67c9516dc2215ea65e4cce1b94c86434fd18" | ||
"gitHead": "a60423b411b0cab587265e388a5b531f0f905588" | ||
} |
@@ -214,2 +214,3 @@ ![CI](https://github.com/finnair/v-validation/workflows/CI/badge.svg?branch=master) | ||
- if conversion happens, all the validators must return the same value (deepEquals) | ||
- `anyOf` - at least one of the validators must match | ||
- `oneOf` - exactly one validator must match while others should return false | ||
@@ -564,2 +565,3 @@ - `compositionOf` - validators are run one after another against the (current) converted value (a shortcut for [`Validator.next`](#next)) | ||
| allOf | ...validators: Validator[] | Requires that all given validators match. Validators are run in parallel and in case they convert the input, all must provide same output. | | ||
| anyOf | ...validators: Validator[] | Requires minimum one of given validators matches. Validators are run in parallel and in case of failure, all violations will be returned. | | ||
| oneOf | ...validators: Validator[] | Requires that exactly one of the given validators match. | | ||
@@ -574,2 +576,3 @@ | compositionOf | ...validators: Validator[] | Runs given the validators one after another, chaining the result. | | ||
| whenGroup...otherwise | group: GroupOrName, ...allOf: Validator[] | Defines validation rules (`allOf`) to be executed for given `ValidatorOptions.group`. | | ||
| json | ...validators: Validator[] | Parse JSON input and validate it against given validators. | | ||
@@ -576,0 +579,0 @@ ## Violations |
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
133025
2210
621
+ Added@finnair/path@0.9.0(transitive)
- Removed@finnair/path@0.8.0(transitive)
Updated@finnair/path@^0.9.0