@finnair/v-validation
Advanced tools
Comparing version 4.3.0 to 5.0.0
@@ -6,2 +6,24 @@ # Change Log | ||
# [5.0.0](https://github.com/finnair/v-validation/compare/v4.3.0...v5.0.0) (2023-10-13) | ||
### Features | ||
* use ECMAScript modules (ESM) instead of CommonJS ([#95](https://github.com/finnair/v-validation/issues/95)) ([92e9118](https://github.com/finnair/v-validation/commit/92e9118235957ec4bc2bcf2de73e195ea940378c)) | ||
# [5.0.0](https://github.com/finnair/v-validation/compare/v4.3.0...v5.0.0) (2023-10-13) | ||
### Features | ||
* use ECMAScript modules (ESM) instead of CommonJS ([#95](https://github.com/finnair/v-validation/issues/95)) ([92e9118](https://github.com/finnair/v-validation/commit/92e9118235957ec4bc2bcf2de73e195ea940378c)) | ||
# [4.3.0](https://github.com/finnair/v-validation/compare/v4.2.0...v4.3.0) (2023-10-03) | ||
@@ -8,0 +30,0 @@ |
@@ -1,2 +0,2 @@ | ||
export * from './V'; | ||
export * from './validators'; | ||
export * from './V.js'; | ||
export * from './validators.js'; |
@@ -1,18 +0,2 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./V"), exports); | ||
__exportStar(require("./validators"), exports); | ||
export * from './V.js'; | ||
export * from './validators.js'; |
@@ -1,2 +0,2 @@ | ||
import { Validator, ValidationContext, ValidationResult, ObjectValidator, PropertyModel, MapEntryModel, Violation } from './validators'; | ||
import { Validator, ValidationContext, ValidationResult, ObjectValidator, PropertyModel, MapEntryModel, Violation } from './validators.js'; | ||
import { Path } from '@finnair/path'; | ||
@@ -6,3 +6,3 @@ export interface DiscriminatorFn { | ||
} | ||
export declare type Discriminator = string | DiscriminatorFn; | ||
export type Discriminator = string | DiscriminatorFn; | ||
export interface SchemaModel { | ||
@@ -14,3 +14,3 @@ readonly discriminator: Discriminator; | ||
} | ||
export declare type ClassParentModel = string | ObjectValidator | (string | ObjectValidator)[]; | ||
export type ClassParentModel = string | ObjectValidator | (string | ObjectValidator)[]; | ||
export interface ClassModel { | ||
@@ -17,0 +17,0 @@ readonly properties?: PropertyModel; |
@@ -1,6 +0,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SchemaValidator = exports.DiscriminatorViolation = exports.ModelRef = void 0; | ||
const validators_1 = require("./validators"); | ||
class ModelRef extends validators_1.Validator { | ||
import { Validator, ObjectValidator, isNullOrUndefined, defaultViolations, isString, Violation, TypeMismatch, } from './validators.js'; | ||
export class ModelRef extends Validator { | ||
schema; | ||
name; | ||
constructor(schema, name) { | ||
@@ -16,4 +15,4 @@ super(); | ||
} | ||
exports.ModelRef = ModelRef; | ||
class DiscriminatorViolation extends validators_1.Violation { | ||
export class DiscriminatorViolation extends Violation { | ||
expectedOneOf; | ||
constructor(path, value, expectedOneOf) { | ||
@@ -24,8 +23,8 @@ super(path, 'Discriminator', value); | ||
} | ||
exports.DiscriminatorViolation = DiscriminatorViolation; | ||
class SchemaValidator extends validators_1.Validator { | ||
export class SchemaValidator extends Validator { | ||
discriminator; | ||
proxies = new Map(); | ||
validators = {}; | ||
constructor(fn) { | ||
super(); | ||
this.proxies = new Map(); | ||
this.validators = {}; | ||
const schema = fn(this); | ||
@@ -46,4 +45,4 @@ for (const name of this.proxies.keys()) { | ||
validateClass(value, path, ctx, expectedType) { | ||
if ((0, validators_1.isNullOrUndefined)(value)) { | ||
return ctx.failurePromise(validators_1.defaultViolations.notNull(path), value); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -53,3 +52,3 @@ // 1) Validate discriminator | ||
let typePath = path; | ||
if ((0, validators_1.isString)(this.discriminator)) { | ||
if (isString(this.discriminator)) { | ||
type = value[this.discriminator]; | ||
@@ -69,3 +68,3 @@ typePath = path.property(this.discriminator); | ||
if (!this.isSubtypeOf(validator, expectedParent)) { | ||
return ctx.failurePromise(new validators_1.TypeMismatch(path, expectedType, type), type); | ||
return ctx.failurePromise(new TypeMismatch(path, expectedType, type), type); | ||
} | ||
@@ -97,3 +96,3 @@ } | ||
// ...or it extends the expectedParent | ||
if (validator instanceof validators_1.ObjectValidator) { | ||
if (validator instanceof ObjectValidator) { | ||
return validator.parentValidators.some(parent => this.isSubtypeOf(parent, expectedParent)); | ||
@@ -117,3 +116,3 @@ } | ||
let validator; | ||
if (models[name] instanceof validators_1.Validator) { | ||
if (models[name] instanceof Validator) { | ||
validator = models[name]; | ||
@@ -124,3 +123,3 @@ } | ||
const localProperties = classModel.localProperties || {}; | ||
if ((0, validators_1.isString)(this.discriminator)) { | ||
if (isString(this.discriminator)) { | ||
const discriminatorProperty = this.discriminator; | ||
@@ -139,3 +138,3 @@ if (!localProperties[discriminatorProperty]) { | ||
}; | ||
validator = new validators_1.ObjectValidator(model); | ||
validator = new ObjectValidator(model); | ||
} | ||
@@ -152,3 +151,3 @@ this.validators[name] = validator; | ||
let parent; | ||
if ((0, validators_1.isString)(nameOrValidator)) { | ||
if (isString(nameOrValidator)) { | ||
parent = this.compileClass(nameOrValidator, models, seen); | ||
@@ -159,3 +158,3 @@ } | ||
} | ||
if (!(parent instanceof validators_1.ObjectValidator)) { | ||
if (!(parent instanceof ObjectValidator)) { | ||
throw new Error(`Illegal inheritance: objects may only inherit from other objects (ObjectValidators)`); | ||
@@ -167,2 +166,1 @@ } | ||
} | ||
exports.SchemaValidator = SchemaValidator; |
@@ -1,4 +0,4 @@ | ||
import { SchemaValidator, SchemaModel } from './schema'; | ||
import { SchemaValidator, SchemaModel } from './schema.js'; | ||
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, AnyOfValidator, OneOfValidator, CompositionValidator, EnumValidator, HasValueValidator, JsonValidator, RequiredValidator } 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, RequiredValidator } from './validators.js'; | ||
export declare const V: { | ||
@@ -5,0 +5,0 @@ fn: (fn: ValidatorFn, type?: string) => ValidatorFnWrapper; |
@@ -1,21 +0,15 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.V = void 0; | ||
const uuid_validate_1 = __importDefault(require("uuid-validate")); | ||
const schema_1 = require("./schema"); | ||
const validators_1 = require("./validators"); | ||
const ignoreValidator = new validators_1.IgnoreValidator(), anyValidator = new validators_1.AnyValidator(), stringValidator = new validators_1.StringValidator(), toStringValidator = new validators_1.StringNormalizer(), notNullValidator = new validators_1.NotNullOrUndefinedValidator(), nullOrUndefinedValidator = new validators_1.IsNullOrUndefinedValidator(), notEmptyValidator = new validators_1.NotEmptyValidator(), notBlankValidator = new validators_1.NotBlankValidator(), emptyToNullValidator = new validators_1.ValueMapper(val => ((0, validators_1.isNullOrUndefined)(val) || val === '' ? null : val)), emptyToUndefinedValidator = new validators_1.ValueMapper(val => ((0, validators_1.isNullOrUndefined)(val) || val === '' ? undefined : val)), undefinedToNullValidator = new validators_1.ValueMapper(val => (val === undefined ? null : val)), booleanValidator = new validators_1.BooleanValidator(), numberValidator = new validators_1.NumberValidator(validators_1.NumberFormat.number), toNumberValidator = new validators_1.NumberNormalizer(validators_1.NumberFormat.number), integerValidator = new validators_1.NumberValidator(validators_1.NumberFormat.integer), toIntegerValidator = new validators_1.NumberNormalizer(validators_1.NumberFormat.integer), dateValidator = new validators_1.DateValidator(validators_1.ValidatorType.Date); | ||
exports.V = { | ||
fn: (fn, type) => new validators_1.ValidatorFnWrapper(fn, type), | ||
map: (fn, error) => new validators_1.ValueMapper(fn, error), | ||
import { default as validateUuid } from 'uuid-validate'; | ||
import { SchemaValidator } from './schema.js'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, isNullOrUndefined, BooleanValidator, NumberValidator, NumberFormat, NumberNormalizer, DateValidator, ValidatorType, ValidatorFnWrapper, CheckValidator, maybeAllOfValidator, OptionalValidator, IfValidator, Conditional, WhenGroupValidator, WhenGroup, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, ObjectValidator, ObjectNormalizer, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AllOfValidator, AnyOfValidator, OneOfValidator, CompositionValidator, EnumValidator, HasValueValidator, JsonValidator, RequiredValidator, } from './validators.js'; | ||
const ignoreValidator = new IgnoreValidator(), anyValidator = new AnyValidator(), stringValidator = new StringValidator(), toStringValidator = new StringNormalizer(), notNullValidator = new NotNullOrUndefinedValidator(), nullOrUndefinedValidator = new IsNullOrUndefinedValidator(), notEmptyValidator = new NotEmptyValidator(), notBlankValidator = new NotBlankValidator(), emptyToNullValidator = new ValueMapper((value) => (isNullOrUndefined(value) || value === '' ? null : value)), emptyToUndefinedValidator = new ValueMapper((value) => (isNullOrUndefined(value) || value === '' ? undefined : value)), undefinedToNullValidator = new ValueMapper((value) => (value === undefined ? null : value)), booleanValidator = new BooleanValidator(), numberValidator = new NumberValidator(NumberFormat.number), toNumberValidator = new NumberNormalizer(NumberFormat.number), integerValidator = new NumberValidator(NumberFormat.integer), toIntegerValidator = new NumberNormalizer(NumberFormat.integer), dateValidator = new DateValidator(ValidatorType.Date); | ||
export const V = { | ||
fn: (fn, type) => new ValidatorFnWrapper(fn, type), | ||
map: (fn, error) => new ValueMapper(fn, error), | ||
ignore: () => ignoreValidator, | ||
any: () => anyValidator, | ||
check: (...allOf) => new validators_1.CheckValidator((0, validators_1.maybeAllOfValidator)(allOf)), | ||
optional: (type, ...allOf) => new validators_1.OptionalValidator(type, allOf), | ||
required: (type, ...allOf) => new validators_1.RequiredValidator(type, allOf), | ||
if: (fn, ...allOf) => new validators_1.IfValidator([new validators_1.Conditional(fn, allOf)]), | ||
whenGroup: (group, ...allOf) => new validators_1.WhenGroupValidator([new validators_1.WhenGroup(group, allOf)]), | ||
check: (...allOf) => new CheckValidator(maybeAllOfValidator(allOf)), | ||
optional: (type, ...allOf) => new OptionalValidator(type, allOf), | ||
required: (type, ...allOf) => new RequiredValidator(type, allOf), | ||
if: (fn, ...allOf) => new IfValidator([new Conditional(fn, allOf)]), | ||
whenGroup: (group, ...allOf) => new WhenGroupValidator([new WhenGroup(group, allOf)]), | ||
string: () => stringValidator, | ||
@@ -30,8 +24,8 @@ toString: () => toStringValidator, | ||
undefinedToNull: () => undefinedToNullValidator, | ||
emptyTo: (defaultValue) => new validators_1.ValueMapper(val => ((0, validators_1.isNullOrUndefined)(val) || val === '' ? defaultValue : val)), | ||
uuid: (version) => new validators_1.AssertTrueValidator(value => !(0, validators_1.isNullOrUndefined)(value) && (0, uuid_validate_1.default)(value, version), 'UUID'), | ||
pattern: (pattern, flags) => new validators_1.PatternValidator(pattern, flags), | ||
toPattern: (pattern, flags) => new validators_1.PatternNormalizer(pattern, flags), | ||
emptyTo: (defaultValue) => new ValueMapper((value) => (isNullOrUndefined(value) || value === '' ? defaultValue : value)), | ||
uuid: (version) => new AssertTrueValidator((value) => !isNullOrUndefined(value) && validateUuid(value, version), 'UUID'), | ||
pattern: (pattern, flags) => new PatternValidator(pattern, flags), | ||
toPattern: (pattern, flags) => new PatternNormalizer(pattern, flags), | ||
boolean: () => booleanValidator, | ||
toBoolean: (truePattern = /^true$/, falsePattern = /^false$/) => new validators_1.BooleanNormalizer(truePattern, falsePattern), | ||
toBoolean: (truePattern = /^true$/, falsePattern = /^false$/) => new BooleanNormalizer(truePattern, falsePattern), | ||
number: () => numberValidator, | ||
@@ -41,28 +35,28 @@ toNumber: () => toNumberValidator, | ||
toInteger: () => toIntegerValidator, | ||
min: (min, inclusive = true) => new validators_1.MinValidator(min, inclusive), | ||
max: (max, inclusive = true) => new validators_1.MaxValidator(max, inclusive), | ||
object: (model) => new validators_1.ObjectValidator(model), | ||
toObject: (property) => new validators_1.ObjectNormalizer(property), | ||
schema: (fn) => new schema_1.SchemaValidator(fn), | ||
min: (min, inclusive = true) => new MinValidator(min, inclusive), | ||
max: (max, inclusive = true) => new MaxValidator(max, inclusive), | ||
object: (model) => new ObjectValidator(model), | ||
toObject: (property) => new ObjectNormalizer(property), | ||
schema: (fn) => new SchemaValidator(fn), | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
mapType: (keys, values, jsonSafeMap = true) => new validators_1.MapValidator(keys, values, jsonSafeMap), | ||
mapType: (keys, values, jsonSafeMap = true) => new MapValidator(keys, values, jsonSafeMap), | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
toMapType: (keys, values) => new validators_1.MapNormalizer(keys, values), | ||
nullTo: (defaultValue) => new validators_1.ValueMapper(value => ((0, validators_1.isNullOrUndefined)(value) ? defaultValue : value)), | ||
nullToObject: () => new validators_1.ValueMapper(value => ((0, validators_1.isNullOrUndefined)(value) ? {} : value)), | ||
nullToArray: () => new validators_1.ValueMapper(value => ((0, validators_1.isNullOrUndefined)(value) ? [] : value)), | ||
array: (...items) => new validators_1.ArrayValidator((0, validators_1.maybeAllOfValidator)(items)), | ||
toArray: (...items) => new validators_1.ArrayNormalizer((0, validators_1.maybeAllOfValidator)(items)), | ||
size: (min, max) => new validators_1.SizeValidator(min, max), | ||
properties: (keys, values) => new validators_1.ObjectValidator({ additionalProperties: { keys, values } }), | ||
allOf: (...validators) => new validators_1.AllOfValidator(validators), | ||
anyOf: (...validators) => new validators_1.AnyOfValidator(validators), | ||
oneOf: (...validators) => new validators_1.OneOfValidator(validators), | ||
compositionOf: (...validators) => new validators_1.CompositionValidator(validators), | ||
toMapType: (keys, values) => new MapNormalizer(keys, values), | ||
nullTo: (defaultValue) => new ValueMapper(value => (isNullOrUndefined(value) ? defaultValue : value)), | ||
nullToObject: () => new ValueMapper((value) => (isNullOrUndefined(value) ? {} : value)), | ||
nullToArray: () => new ValueMapper((value) => (isNullOrUndefined(value) ? [] : value)), | ||
array: (...items) => new ArrayValidator(maybeAllOfValidator(items)), | ||
toArray: (...items) => new ArrayNormalizer(maybeAllOfValidator(items)), | ||
size: (min, max) => new SizeValidator(min, max), | ||
properties: (keys, values) => new ObjectValidator({ additionalProperties: { keys, values } }), | ||
allOf: (...validators) => new AllOfValidator(validators), | ||
anyOf: (...validators) => new AnyOfValidator(validators), | ||
oneOf: (...validators) => new OneOfValidator(validators), | ||
compositionOf: (...validators) => new CompositionValidator(validators), | ||
date: () => dateValidator, | ||
enum: (enumType, name) => new validators_1.EnumValidator(enumType, name), | ||
assertTrue: (fn, type = 'AssertTrue', path) => new validators_1.AssertTrueValidator(fn, type, path), | ||
hasValue: (expectedValue) => new validators_1.HasValueValidator(expectedValue), | ||
json: (...validators) => new validators_1.JsonValidator(validators), | ||
enum: (enumType, name) => new EnumValidator(enumType, name), | ||
assertTrue: (fn, type = 'AssertTrue', path) => new AssertTrueValidator(fn, type, path), | ||
hasValue: (expectedValue) => new HasValueValidator(expectedValue), | ||
json: (...validators) => new JsonValidator(validators), | ||
}; | ||
Object.freeze(exports.V); | ||
Object.freeze(V); |
@@ -8,3 +8,3 @@ import { Path } from '@finnair/path'; | ||
} | ||
export declare type Properties = { | ||
export type Properties = { | ||
[s: string]: Validator; | ||
@@ -110,3 +110,3 @@ }; | ||
} | ||
export declare type GroupOrName = Group | string; | ||
export type GroupOrName = Group | string; | ||
export declare class Group { | ||
@@ -161,6 +161,6 @@ readonly name: string; | ||
} | ||
export declare type PropertyModel = { | ||
export type PropertyModel = { | ||
[s: string]: string | number | Validator | Validator[]; | ||
}; | ||
export declare type ParentModel = ObjectModel | ObjectValidator | (ObjectModel | ObjectValidator)[]; | ||
export type ParentModel = ObjectModel | ObjectValidator | (ObjectModel | ObjectValidator)[]; | ||
export interface ObjectModel { | ||
@@ -167,0 +167,0 @@ readonly extends?: ParentModel; |
@@ -1,25 +0,10 @@ | ||
"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) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
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.SyncPromise = exports.ValidationContext = void 0; | ||
exports.JsonValidator = exports.IgnoreValidator = exports.isPromise = exports.ValueMapper = exports.RequiredValidator = 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 = void 0; | ||
const deep_equal_1 = __importDefault(require("deep-equal")); | ||
const path_1 = require("@finnair/path"); | ||
const ROOT = path_1.Path.ROOT; | ||
class ValidationContext { | ||
import deepEqual from 'deep-equal'; | ||
import { Path } from '@finnair/path'; | ||
const ROOT = Path.ROOT; | ||
export class ValidationContext { | ||
options; | ||
constructor(options) { | ||
this.options = options; | ||
this.objects = new Map(); | ||
} | ||
objects = new Map(); | ||
failure(violation, value) { | ||
@@ -54,3 +39,3 @@ const violations = [].concat(violation); | ||
} | ||
return this.failure(exports.defaultViolations.cycle(path), value); | ||
return this.failure(defaultViolations.cycle(path), value); | ||
} | ||
@@ -65,4 +50,4 @@ this.objects.set(value, convertedValue); | ||
} | ||
exports.ValidationContext = ValidationContext; | ||
class SyncPromise { | ||
export class SyncPromise { | ||
value; | ||
constructor(value) { | ||
@@ -93,4 +78,3 @@ this.value = value; | ||
} | ||
exports.SyncPromise = SyncPromise; | ||
class Validator { | ||
export class Validator { | ||
validateGroup(value, group) { | ||
@@ -112,4 +96,5 @@ return this.validate(value, { group }); | ||
} | ||
exports.Validator = Validator; | ||
class ValidationResult { | ||
export class ValidationResult { | ||
value; | ||
violations; | ||
constructor(violations, value) { | ||
@@ -136,4 +121,4 @@ this.violations = violations; | ||
} | ||
exports.ValidationResult = ValidationResult; | ||
class ValidationError extends Error { | ||
export class ValidationError extends Error { | ||
violations; | ||
constructor(violations) { | ||
@@ -145,4 +130,6 @@ super(`ValidationError: ${JSON.stringify(violations, undefined, 2)}`); | ||
} | ||
exports.ValidationError = ValidationError; | ||
class Violation { | ||
export class Violation { | ||
path; | ||
type; | ||
invalidValue; | ||
constructor(path, type, invalidValue) { | ||
@@ -154,4 +141,5 @@ this.path = path; | ||
} | ||
exports.Violation = Violation; | ||
class TypeMismatch extends Violation { | ||
export class TypeMismatch extends Violation { | ||
expected; | ||
invalidValue; | ||
constructor(path, expected, invalidValue) { | ||
@@ -163,4 +151,6 @@ super(path, ValidatorType.TypeMismatch, invalidValue); | ||
} | ||
exports.TypeMismatch = TypeMismatch; | ||
class EnumMismatch extends Violation { | ||
export class EnumMismatch extends Violation { | ||
path; | ||
enumType; | ||
invalidValue; | ||
constructor(path, enumType, invalidValue) { | ||
@@ -173,4 +163,4 @@ super(path, ValidatorType.EnumMismatch, invalidValue); | ||
} | ||
exports.EnumMismatch = EnumMismatch; | ||
class ErrorViolation extends Violation { | ||
export class ErrorViolation extends Violation { | ||
error; | ||
constructor(path, error) { | ||
@@ -181,4 +171,4 @@ super(path, 'Error'); | ||
} | ||
exports.ErrorViolation = ErrorViolation; | ||
class HasValueViolation extends Violation { | ||
export class HasValueViolation extends Violation { | ||
expectedValue; | ||
constructor(path, expectedValue, invalidValue) { | ||
@@ -189,4 +179,5 @@ super(path, 'HasValue', invalidValue); | ||
} | ||
exports.HasValueViolation = HasValueViolation; | ||
class PatternViolation extends Violation { | ||
export class PatternViolation extends Violation { | ||
pattern; | ||
invalidValue; | ||
constructor(path, pattern, invalidValue) { | ||
@@ -198,4 +189,4 @@ super(path, ValidatorType.Pattern, invalidValue); | ||
} | ||
exports.PatternViolation = PatternViolation; | ||
class OneOfMismatch extends Violation { | ||
export class OneOfMismatch extends Violation { | ||
matches; | ||
constructor(path, matches) { | ||
@@ -206,4 +197,6 @@ super(path, ValidatorType.OneOf); | ||
} | ||
exports.OneOfMismatch = OneOfMismatch; | ||
class MinViolation extends Violation { | ||
export class MinViolation extends Violation { | ||
min; | ||
inclusive; | ||
invalidValue; | ||
constructor(path, min, inclusive, invalidValue) { | ||
@@ -216,4 +209,6 @@ super(path, 'Min'); | ||
} | ||
exports.MinViolation = MinViolation; | ||
class MaxViolation extends Violation { | ||
export class MaxViolation extends Violation { | ||
max; | ||
inclusive; | ||
invalidValue; | ||
constructor(path, max, inclusive, invalidValue) { | ||
@@ -226,4 +221,5 @@ super(path, 'Max'); | ||
} | ||
exports.MaxViolation = MaxViolation; | ||
class SizeViolation extends Violation { | ||
export class SizeViolation extends Violation { | ||
min; | ||
max; | ||
constructor(path, min, max) { | ||
@@ -235,4 +231,5 @@ super(path, 'Size'); | ||
} | ||
exports.SizeViolation = SizeViolation; | ||
class Group { | ||
export class Group { | ||
name; | ||
allIncluded; | ||
constructor(name, includes) { | ||
@@ -263,7 +260,4 @@ this.name = name; | ||
} | ||
exports.Group = Group; | ||
class Groups { | ||
constructor() { | ||
this.groups = {}; | ||
} | ||
export class Groups { | ||
groups = {}; | ||
define(name, ...includes) { | ||
@@ -294,8 +288,6 @@ if (this.groups[name]) { | ||
} | ||
exports.Groups = Groups; | ||
function isNullOrUndefined(value) { | ||
export function isNullOrUndefined(value) { | ||
return value === null || value === undefined; | ||
} | ||
exports.isNullOrUndefined = isNullOrUndefined; | ||
var ValidatorType; | ||
export var ValidatorType; | ||
(function (ValidatorType) { | ||
@@ -314,4 +306,4 @@ ValidatorType["TypeMismatch"] = "TypeMismatch"; | ||
ValidatorType["Pattern"] = "Pattern"; | ||
})(ValidatorType = exports.ValidatorType || (exports.ValidatorType = {})); | ||
exports.defaultViolations = { | ||
})(ValidatorType || (ValidatorType = {})); | ||
export const defaultViolations = { | ||
date: (invalidValue, path = ROOT, type = ValidatorType.Date) => new TypeMismatch(path, type, invalidValue), | ||
@@ -383,3 +375,5 @@ object: (path = ROOT) => new TypeMismatch(path, 'object'), | ||
} | ||
class ValidatorFnWrapper extends Validator { | ||
export class ValidatorFnWrapper extends Validator { | ||
type; | ||
fn; | ||
constructor(fn, type) { | ||
@@ -395,6 +389,5 @@ super(); | ||
} | ||
exports.ValidatorFnWrapper = ValidatorFnWrapper; | ||
const lenientUnknownPropertyValidator = new ValidatorFnWrapper((value, path, ctx) => ctx.failurePromise(exports.defaultViolations.unknownProperty(path), value)); | ||
const strictUnknownPropertyValidator = new ValidatorFnWrapper((value, path, ctx) => ctx.failurePromise(exports.defaultViolations.unknownPropertyDenied(path), value)); | ||
function mergeProperties(from, to) { | ||
const lenientUnknownPropertyValidator = new ValidatorFnWrapper((value, path, ctx) => ctx.failurePromise(defaultViolations.unknownProperty(path), value)); | ||
const strictUnknownPropertyValidator = new ValidatorFnWrapper((value, path, ctx) => ctx.failurePromise(defaultViolations.unknownPropertyDenied(path), value)); | ||
export function mergeProperties(from, to) { | ||
if (from) { | ||
@@ -412,4 +405,10 @@ for (const key in from) { | ||
} | ||
exports.mergeProperties = mergeProperties; | ||
class ObjectValidator extends Validator { | ||
export class ObjectValidator extends Validator { | ||
model; | ||
properties; | ||
localProperties; | ||
additionalProperties; | ||
parentValidators; | ||
nextValidator; | ||
localNextValidator; | ||
constructor(model) { | ||
@@ -455,6 +454,6 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
if (typeof value !== 'object') { | ||
return ctx.failurePromise(exports.defaultViolations.object(path), value); | ||
return ctx.failurePromise(defaultViolations.object(path), value); | ||
} | ||
@@ -501,3 +500,2 @@ const context = { | ||
} | ||
exports.ObjectValidator = ObjectValidator; | ||
function validateProperty(key, currentValue, validator, context) { | ||
@@ -526,35 +524,33 @@ if (!context.filter(key)) { | ||
} | ||
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); | ||
async function validateAdditionalProperty(key, originalValue, additionalProperties, context) { | ||
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 = await entryValidator.keyValidator.validatePath(key, keyPath, context.ctx); | ||
if (result.isSuccess()) { | ||
validKey = true; | ||
result = await validateProperty(key, currentValue, entryValidator.valueValidator, context); | ||
if (result.isSuccess()) { | ||
validKey = true; | ||
result = yield validateProperty(key, currentValue, entryValidator.valueValidator, context); | ||
if (result.isSuccess()) { | ||
currentValue = result.getValue(); | ||
} | ||
else { | ||
return result; | ||
} | ||
currentValue = result.getValue(); | ||
} | ||
} | ||
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); | ||
return result; | ||
} | ||
} | ||
}); | ||
} | ||
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); | ||
} | ||
} | ||
} | ||
@@ -566,3 +562,4 @@ /** | ||
*/ | ||
class ObjectNormalizer extends Validator { | ||
export class ObjectNormalizer extends Validator { | ||
property; | ||
constructor(property) { | ||
@@ -585,4 +582,5 @@ super(); | ||
} | ||
exports.ObjectNormalizer = ObjectNormalizer; | ||
class MapEntryValidator { | ||
export class MapEntryValidator { | ||
keyValidator; | ||
valueValidator; | ||
constructor(entryModel) { | ||
@@ -594,4 +592,4 @@ this.keyValidator = maybeAllOfValidator(entryModel.keys); | ||
} | ||
exports.MapEntryValidator = MapEntryValidator; | ||
class ArrayValidator extends Validator { | ||
export class ArrayValidator extends Validator { | ||
itemsValidator; | ||
constructor(itemsValidator) { | ||
@@ -604,3 +602,3 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -637,4 +635,3 @@ if (!Array.isArray(value)) { | ||
} | ||
exports.ArrayValidator = ArrayValidator; | ||
class ArrayNormalizer extends ArrayValidator { | ||
export class ArrayNormalizer extends ArrayValidator { | ||
constructor(itemsValidator) { | ||
@@ -653,4 +650,5 @@ super(itemsValidator); | ||
} | ||
exports.ArrayNormalizer = ArrayNormalizer; | ||
class NextValidator extends Validator { | ||
export class NextValidator extends Validator { | ||
firstValidator; | ||
nextValidator; | ||
constructor(firstValidator, nextValidator) { | ||
@@ -671,4 +669,4 @@ super(); | ||
} | ||
exports.NextValidator = NextValidator; | ||
class CheckValidator extends Validator { | ||
export class CheckValidator extends Validator { | ||
validator; | ||
constructor(validator) { | ||
@@ -688,4 +686,4 @@ super(); | ||
} | ||
exports.CheckValidator = CheckValidator; | ||
class CompositionValidator extends Validator { | ||
export class CompositionValidator extends Validator { | ||
validators; | ||
constructor(validators) { | ||
@@ -697,20 +695,18 @@ super(); | ||
} | ||
validatePath(value, path, ctx) { | ||
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; | ||
} | ||
async validatePath(value, path, ctx) { | ||
let currentValue = value; | ||
for (let i = 0; i < this.validators.length; i++) { | ||
const result = await this.validators[i].validatePath(currentValue, path, ctx); | ||
if (result.isSuccess()) { | ||
currentValue = result.getValue(); | ||
} | ||
return ctx.success(currentValue); | ||
}); | ||
else { | ||
return result; | ||
} | ||
} | ||
return ctx.success(currentValue); | ||
} | ||
} | ||
exports.CompositionValidator = CompositionValidator; | ||
class OneOfValidator extends Validator { | ||
export class OneOfValidator extends Validator { | ||
validators; | ||
constructor(validators) { | ||
@@ -722,26 +718,24 @@ super(); | ||
} | ||
validatePath(value, path, ctx) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let matches = 0; | ||
let newValue = null; | ||
const promises = []; | ||
for (let i = 0; i < this.validators.length; i++) { | ||
promises[i] = this.validators[i].validatePath(value, path, ctx).then(result => { | ||
if (result.isSuccess()) { | ||
matches++; | ||
newValue = result.getValue(); | ||
} | ||
return result; | ||
}); | ||
} | ||
yield Promise.all(promises); | ||
if (matches === 1) { | ||
return ctx.successPromise(newValue); | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.oneOf(matches, path), value); | ||
}); | ||
async validatePath(value, path, ctx) { | ||
let matches = 0; | ||
let newValue = null; | ||
const promises = []; | ||
for (let i = 0; i < this.validators.length; i++) { | ||
promises[i] = this.validators[i].validatePath(value, path, ctx).then(result => { | ||
if (result.isSuccess()) { | ||
matches++; | ||
newValue = result.getValue(); | ||
} | ||
return result; | ||
}); | ||
} | ||
await Promise.all(promises); | ||
if (matches === 1) { | ||
return ctx.successPromise(newValue); | ||
} | ||
return ctx.failurePromise(defaultViolations.oneOf(matches, path), value); | ||
} | ||
} | ||
exports.OneOfValidator = OneOfValidator; | ||
class AnyOfValidator extends Validator { | ||
export class AnyOfValidator extends Validator { | ||
validators; | ||
constructor(validators) { | ||
@@ -753,17 +747,16 @@ super(); | ||
} | ||
validatePath(value, path, ctx) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const passes = []; | ||
const failures = []; | ||
for (let i = 0; i < this.validators.length; i++) { | ||
const validator = this.validators[i]; | ||
const result = yield validator.validatePath(value, path, ctx); | ||
result.isSuccess() ? passes.push(result.getValue()) : failures.push(...result.getViolations()); | ||
} | ||
return passes.length > 0 ? ctx.success(passes.pop()) : ctx.failure(failures, value); | ||
}); | ||
async validatePath(value, path, ctx) { | ||
const passes = []; | ||
const failures = []; | ||
for (let i = 0; i < this.validators.length; i++) { | ||
const validator = this.validators[i]; | ||
const result = await validator.validatePath(value, path, ctx); | ||
result.isSuccess() ? passes.push(result.getValue()) : failures.push(...result.getViolations()); | ||
} | ||
return passes.length > 0 ? ctx.success(passes.pop()) : ctx.failure(failures, value); | ||
} | ||
} | ||
exports.AnyOfValidator = AnyOfValidator; | ||
class IfValidator extends Validator { | ||
export class IfValidator extends Validator { | ||
conditionals; | ||
elseValidator; | ||
constructor(conditionals, elseValidator) { | ||
@@ -801,4 +794,5 @@ super(); | ||
} | ||
exports.IfValidator = IfValidator; | ||
class Conditional { | ||
export class Conditional { | ||
fn; | ||
validator; | ||
constructor(fn, allOf) { | ||
@@ -811,4 +805,5 @@ this.fn = fn; | ||
} | ||
exports.Conditional = Conditional; | ||
class WhenGroupValidator extends Validator { | ||
export class WhenGroupValidator extends Validator { | ||
whenGroups; | ||
otherwiseValidator; | ||
constructor(whenGroups, otherwiseValidator) { | ||
@@ -848,4 +843,5 @@ super(); | ||
} | ||
exports.WhenGroupValidator = WhenGroupValidator; | ||
class WhenGroup { | ||
export class WhenGroup { | ||
group; | ||
validator; | ||
constructor(group, allOf) { | ||
@@ -857,4 +853,6 @@ this.group = isString(group) ? group : group.name; | ||
} | ||
exports.WhenGroup = WhenGroup; | ||
class MapValidator extends Validator { | ||
export class MapValidator extends Validator { | ||
keys; | ||
values; | ||
jsonSafeMap; | ||
constructor(keys, values, jsonSafeMap) { | ||
@@ -869,3 +867,3 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -910,4 +908,3 @@ if (!(value instanceof Map)) { | ||
} | ||
exports.MapValidator = MapValidator; | ||
class MapNormalizer extends MapValidator { | ||
export class MapNormalizer extends MapValidator { | ||
constructor(keys, values) { | ||
@@ -945,4 +942,3 @@ super(keys, values, true); | ||
} | ||
exports.MapNormalizer = MapNormalizer; | ||
class JsonMap extends Map { | ||
export class JsonMap extends Map { | ||
constructor(params) { | ||
@@ -955,4 +951,3 @@ super(params); | ||
} | ||
exports.JsonMap = JsonMap; | ||
class AnyValidator extends Validator { | ||
export class AnyValidator extends Validator { | ||
validatePath(value, path, ctx) { | ||
@@ -962,16 +957,13 @@ return ctx.successPromise(value); | ||
} | ||
exports.AnyValidator = AnyValidator; | ||
function isString(value) { | ||
export function isString(value) { | ||
return typeof value === 'string' || value instanceof String; | ||
} | ||
exports.isString = isString; | ||
function isSimplePrimitive(value) { | ||
export function isSimplePrimitive(value) { | ||
const type = typeof value; | ||
return type === 'boolean' || type === 'number' || type === 'bigint' || type === 'string' || type === 'symbol'; | ||
} | ||
exports.isSimplePrimitive = isSimplePrimitive; | ||
class StringValidator extends Validator { | ||
export class StringValidator extends Validator { | ||
validatePath(value, path, ctx) { | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -981,10 +973,9 @@ if (isString(value)) { | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.string(value, path), value); | ||
return ctx.failurePromise(defaultViolations.string(value, path), value); | ||
} | ||
} | ||
exports.StringValidator = StringValidator; | ||
class StringNormalizer extends Validator { | ||
export class StringNormalizer extends Validator { | ||
validatePath(value, path, ctx) { | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1000,10 +991,8 @@ if (isString(value)) { | ||
} | ||
exports.StringNormalizer = StringNormalizer; | ||
class NotNullOrUndefinedValidator extends Validator { | ||
export class NotNullOrUndefinedValidator extends Validator { | ||
validatePath(value, path, ctx) { | ||
return isNullOrUndefined(value) ? ctx.failurePromise(exports.defaultViolations.notNull(path), value) : ctx.successPromise(value); | ||
return isNullOrUndefined(value) ? ctx.failurePromise(defaultViolations.notNull(path), value) : ctx.successPromise(value); | ||
} | ||
} | ||
exports.NotNullOrUndefinedValidator = NotNullOrUndefinedValidator; | ||
class IsNullOrUndefinedValidator extends Validator { | ||
export class IsNullOrUndefinedValidator extends Validator { | ||
validatePath(value, path, ctx) { | ||
@@ -1013,12 +1002,12 @@ return isNullOrUndefined(value) ? ctx.successPromise(value) : ctx.failurePromise(new TypeMismatch(path, 'NullOrUndefined', value), value); | ||
} | ||
exports.IsNullOrUndefinedValidator = IsNullOrUndefinedValidator; | ||
class NotEmptyValidator extends Validator { | ||
export class NotEmptyValidator extends Validator { | ||
validatePath(value, path, ctx) { | ||
return !isNullOrUndefined(value) && isNumber(value.length) && value.length > 0 | ||
? ctx.successPromise(value) | ||
: ctx.failurePromise(exports.defaultViolations.notEmpty(path), value); | ||
: ctx.failurePromise(defaultViolations.notEmpty(path), value); | ||
} | ||
} | ||
exports.NotEmptyValidator = NotEmptyValidator; | ||
class SizeValidator extends Validator { | ||
export class SizeValidator extends Validator { | ||
min; | ||
max; | ||
constructor(min, max) { | ||
@@ -1035,3 +1024,3 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1042,3 +1031,3 @@ if (!isNumber(value.length)) { | ||
if (value.length < this.min || value.length > this.max) { | ||
return ctx.failurePromise(exports.defaultViolations.size(this.min, this.max, path), value); | ||
return ctx.failurePromise(defaultViolations.size(this.min, this.max, path), value); | ||
} | ||
@@ -1048,14 +1037,13 @@ return ctx.successPromise(value); | ||
} | ||
exports.SizeValidator = SizeValidator; | ||
class NotBlankValidator extends Validator { | ||
export class NotBlankValidator extends Validator { | ||
validatePath(value, path, ctx) { | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notBlank(path), value); | ||
return ctx.failurePromise(defaultViolations.notBlank(path), value); | ||
} | ||
if (!isString(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.string(value, path), value); | ||
return ctx.failurePromise(defaultViolations.string(value, path), value); | ||
} | ||
const trimmed = value.trim(); | ||
if (trimmed === '') { | ||
return ctx.failurePromise(exports.defaultViolations.notBlank(path), value); | ||
return ctx.failurePromise(defaultViolations.notBlank(path), value); | ||
} | ||
@@ -1065,7 +1053,6 @@ return ctx.successPromise(value); | ||
} | ||
exports.NotBlankValidator = NotBlankValidator; | ||
class BooleanValidator extends Validator { | ||
export class BooleanValidator extends Validator { | ||
validatePath(value, path, ctx) { | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1075,7 +1062,8 @@ if (typeof value === 'boolean') { | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.boolean(value, path), value); | ||
return ctx.failurePromise(defaultViolations.boolean(value, path), value); | ||
} | ||
} | ||
exports.BooleanValidator = BooleanValidator; | ||
class BooleanNormalizer extends Validator { | ||
export class BooleanNormalizer extends Validator { | ||
truePattern; | ||
falsePattern; | ||
constructor(truePattern, falsePattern) { | ||
@@ -1091,3 +1079,3 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1107,3 +1095,3 @@ if (typeof value === 'boolean') { | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.boolean(value, path), value); | ||
return ctx.failurePromise(defaultViolations.boolean(value, path), value); | ||
} | ||
@@ -1113,16 +1101,15 @@ else if (isNumber(value)) { | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.boolean(value, path), value); | ||
return ctx.failurePromise(defaultViolations.boolean(value, path), value); | ||
} | ||
} | ||
exports.BooleanNormalizer = BooleanNormalizer; | ||
var NumberFormat; | ||
export var NumberFormat; | ||
(function (NumberFormat) { | ||
NumberFormat["number"] = "number"; | ||
NumberFormat["integer"] = "integer"; | ||
})(NumberFormat = exports.NumberFormat || (exports.NumberFormat = {})); | ||
function isNumber(value) { | ||
})(NumberFormat || (NumberFormat = {})); | ||
export function isNumber(value) { | ||
return (typeof value === 'number' || value instanceof Number) && !Number.isNaN(value.valueOf()); | ||
} | ||
exports.isNumber = isNumber; | ||
class NumberValidator extends Validator { | ||
export class NumberValidator extends Validator { | ||
format; | ||
constructor(format) { | ||
@@ -1135,6 +1122,6 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
if (!isNumber(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.number(value, this.format, path), value); | ||
return ctx.failurePromise(defaultViolations.number(value, this.format, path), value); | ||
} | ||
@@ -1147,3 +1134,3 @@ return this.validateFormat(value, path, ctx); | ||
if (!Number.isInteger(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.number(value, this.format, path), value); | ||
return ctx.failurePromise(defaultViolations.number(value, this.format, path), value); | ||
} | ||
@@ -1155,4 +1142,3 @@ break; | ||
} | ||
exports.NumberValidator = NumberValidator; | ||
class NumberNormalizer extends NumberValidator { | ||
export class NumberNormalizer extends NumberValidator { | ||
constructor(format) { | ||
@@ -1163,3 +1149,3 @@ super(format); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1171,3 +1157,3 @@ if (isNumber(value)) { | ||
if (value.trim() === '') { | ||
return ctx.failurePromise(exports.defaultViolations.number(value, this.format, path), value); | ||
return ctx.failurePromise(defaultViolations.number(value, this.format, path), value); | ||
} | ||
@@ -1178,9 +1164,10 @@ const nbr = Number(value); | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.number(value, this.format, path), value); | ||
return ctx.failurePromise(defaultViolations.number(value, this.format, path), value); | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.number(value, this.format, path), value); | ||
return ctx.failurePromise(defaultViolations.number(value, this.format, path), value); | ||
} | ||
} | ||
exports.NumberNormalizer = NumberNormalizer; | ||
class MinValidator extends Validator { | ||
export class MinValidator extends Validator { | ||
min; | ||
inclusive; | ||
constructor(min, inclusive) { | ||
@@ -1194,14 +1181,14 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
if (!isNumber(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.number(value, NumberFormat.number, path), value); | ||
return ctx.failurePromise(defaultViolations.number(value, NumberFormat.number, path), value); | ||
} | ||
if (this.inclusive) { | ||
if (value < this.min) { | ||
return ctx.failurePromise(exports.defaultViolations.min(this.min, this.inclusive, value, path), value); | ||
return ctx.failurePromise(defaultViolations.min(this.min, this.inclusive, value, path), value); | ||
} | ||
} | ||
else if (value <= this.min) { | ||
return ctx.failurePromise(exports.defaultViolations.min(this.min, this.inclusive, value, path), value); | ||
return ctx.failurePromise(defaultViolations.min(this.min, this.inclusive, value, path), value); | ||
} | ||
@@ -1211,4 +1198,5 @@ return ctx.successPromise(value); | ||
} | ||
exports.MinValidator = MinValidator; | ||
class MaxValidator extends Validator { | ||
export class MaxValidator extends Validator { | ||
max; | ||
inclusive; | ||
constructor(max, inclusive) { | ||
@@ -1222,14 +1210,14 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
if (!isNumber(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.number(value, NumberFormat.number, path), value); | ||
return ctx.failurePromise(defaultViolations.number(value, NumberFormat.number, path), value); | ||
} | ||
if (this.inclusive) { | ||
if (value > this.max) { | ||
return ctx.failurePromise(exports.defaultViolations.max(this.max, this.inclusive, value, path), value); | ||
return ctx.failurePromise(defaultViolations.max(this.max, this.inclusive, value, path), value); | ||
} | ||
} | ||
else if (value >= this.max) { | ||
return ctx.failurePromise(exports.defaultViolations.max(this.max, this.inclusive, value, path), value); | ||
return ctx.failurePromise(defaultViolations.max(this.max, this.inclusive, value, path), value); | ||
} | ||
@@ -1239,4 +1227,5 @@ return ctx.successPromise(value); | ||
} | ||
exports.MaxValidator = MaxValidator; | ||
class EnumValidator extends Validator { | ||
export class EnumValidator extends Validator { | ||
enumType; | ||
name; | ||
constructor(enumType, name) { | ||
@@ -1250,3 +1239,3 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1257,7 +1246,9 @@ const isValid = Object.values(this.enumType).includes(value); | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.enum(this.name, value, path), value); | ||
return ctx.failurePromise(defaultViolations.enum(this.name, value, path), value); | ||
} | ||
} | ||
exports.EnumValidator = EnumValidator; | ||
class AssertTrueValidator extends Validator { | ||
export class AssertTrueValidator extends Validator { | ||
type; | ||
path; | ||
fn; | ||
constructor(fn, type, path) { | ||
@@ -1277,4 +1268,4 @@ super(); | ||
} | ||
exports.AssertTrueValidator = AssertTrueValidator; | ||
class HasValueValidator extends Validator { | ||
export class HasValueValidator extends Validator { | ||
expectedValue; | ||
constructor(expectedValue) { | ||
@@ -1286,3 +1277,3 @@ super(); | ||
validatePath(value, path, ctx) { | ||
if ((0, deep_equal_1.default)(value, this.expectedValue)) { | ||
if (deepEqual(value, this.expectedValue)) { | ||
return ctx.successPromise(value); | ||
@@ -1293,4 +1284,3 @@ } | ||
} | ||
exports.HasValueValidator = HasValueValidator; | ||
function maybeAllOfValidator(validatorOrArray) { | ||
export function maybeAllOfValidator(validatorOrArray) { | ||
if (Array.isArray(validatorOrArray)) { | ||
@@ -1307,4 +1297,4 @@ if (validatorOrArray.length === 0) { | ||
} | ||
exports.maybeAllOfValidator = maybeAllOfValidator; | ||
class AllOfValidator extends Validator { | ||
export class AllOfValidator extends Validator { | ||
validators; | ||
constructor(validators) { | ||
@@ -1329,3 +1319,3 @@ super(); | ||
if (resultValue !== value) { | ||
if (convertedValue !== undefined && !(0, deep_equal_1.default)(resultValue, convertedValue)) { | ||
if (convertedValue !== undefined && !deepEqual(resultValue, convertedValue)) { | ||
throw new Error('Conflicting conversions'); | ||
@@ -1346,4 +1336,4 @@ } | ||
} | ||
exports.AllOfValidator = AllOfValidator; | ||
class DateValidator extends Validator { | ||
export class DateValidator extends Validator { | ||
dateType; | ||
constructor(dateType) { | ||
@@ -1356,3 +1346,3 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1368,11 +1358,11 @@ let dateValue; | ||
if (isNaN(dateValue.getTime())) { | ||
return ctx.failurePromise(exports.defaultViolations.date(value, path), value); | ||
return ctx.failurePromise(defaultViolations.date(value, path), value); | ||
} | ||
return ctx.successPromise(dateValue); | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.date(value, path, this.dateType), value); | ||
return ctx.failurePromise(defaultViolations.date(value, path, this.dateType), value); | ||
} | ||
} | ||
exports.DateValidator = DateValidator; | ||
class PatternValidator extends Validator { | ||
export class PatternValidator extends Validator { | ||
regExp; | ||
constructor(pattern, flags) { | ||
@@ -1386,6 +1376,6 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
if (!isString(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.string(value, path), value); | ||
return ctx.failurePromise(defaultViolations.string(value, path), value); | ||
} | ||
@@ -1395,3 +1385,3 @@ if (this.regExp.test(value)) { | ||
} | ||
return ctx.failurePromise(exports.defaultViolations.pattern(this.regExp, value, path), value); | ||
return ctx.failurePromise(defaultViolations.pattern(this.regExp, value, path), value); | ||
} | ||
@@ -1404,4 +1394,3 @@ toJSON() { | ||
} | ||
exports.PatternValidator = PatternValidator; | ||
class PatternNormalizer extends PatternValidator { | ||
export class PatternNormalizer extends PatternValidator { | ||
constructor(pattern, flags) { | ||
@@ -1412,3 +1401,3 @@ super(pattern, flags); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1424,4 +1413,4 @@ if (isString(value)) { | ||
} | ||
exports.PatternNormalizer = PatternNormalizer; | ||
class OptionalValidator extends Validator { | ||
export class OptionalValidator extends Validator { | ||
validator; | ||
constructor(type, allOf) { | ||
@@ -1444,4 +1433,4 @@ super(); | ||
} | ||
exports.OptionalValidator = OptionalValidator; | ||
class RequiredValidator extends Validator { | ||
export class RequiredValidator extends Validator { | ||
validator; | ||
constructor(type, allOf) { | ||
@@ -1459,3 +1448,3 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
@@ -1465,4 +1454,5 @@ return this.validator.validatePath(value, path, ctx); | ||
} | ||
exports.RequiredValidator = RequiredValidator; | ||
class ValueMapper extends Validator { | ||
export class ValueMapper extends Validator { | ||
fn; | ||
error; | ||
constructor(fn, error) { | ||
@@ -1501,8 +1491,6 @@ super(); | ||
} | ||
exports.ValueMapper = ValueMapper; | ||
function isPromise(value) { | ||
export function isPromise(value) { | ||
return value && typeof value['then'] === 'function'; | ||
} | ||
exports.isPromise = isPromise; | ||
class IgnoreValidator extends Validator { | ||
export class IgnoreValidator extends Validator { | ||
validatePath(value, path, ctx) { | ||
@@ -1512,3 +1500,2 @@ return ctx.successPromise(undefined); | ||
} | ||
exports.IgnoreValidator = IgnoreValidator; | ||
const allowAllMapEntries = new MapEntryValidator({ | ||
@@ -1522,3 +1509,4 @@ keys: new AnyValidator(), | ||
}); | ||
class JsonValidator extends Validator { | ||
export class JsonValidator extends Validator { | ||
validator; | ||
constructor(allOf) { | ||
@@ -1531,6 +1519,6 @@ super(); | ||
if (isNullOrUndefined(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.notNull(path), value); | ||
return ctx.failurePromise(defaultViolations.notNull(path), value); | ||
} | ||
if (!isString(value)) { | ||
return ctx.failurePromise(exports.defaultViolations.string(value, path), value); | ||
return ctx.failurePromise(defaultViolations.string(value, path), value); | ||
} | ||
@@ -1546,2 +1534,1 @@ try { | ||
} | ||
exports.JsonValidator = JsonValidator; |
{ | ||
"name": "@finnair/v-validation", | ||
"version": "4.3.0", | ||
"version": "5.0.0", | ||
"private": false, | ||
"description": "V-validation core package", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
} | ||
}, | ||
"license": "MIT", | ||
@@ -27,3 +34,3 @@ "homepage": "https://github.com/finnair/v-validation/tree/master/packages/core#readme", | ||
"dependencies": { | ||
"@finnair/path": "^4.0.0", | ||
"@finnair/path": "^5.0.0", | ||
"@types/deep-equal": "1.0.1", | ||
@@ -34,3 +41,3 @@ "@types/uuid-validate": "0.0.1", | ||
}, | ||
"gitHead": "2ba982bbf4e3457e2b2a1433d319e2fde89a413c" | ||
"gitHead": "b20762715a79e2a336f9c8ceb5107a4d84104a14" | ||
} |
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
Yes
129953
12
2195
+ Added@finnair/path@5.4.0(transitive)
- Removed@finnair/path@4.0.0(transitive)
Updated@finnair/path@^5.0.0