Comparing version 1.0.0-pre-48 to 1.0.0-pre-49
@@ -9,3 +9,2 @@ import { DryvValidatableArray, DryvValidationResult } from './'; | ||
private readonly _items; | ||
proxy: TModel[]; | ||
constructor(model: TModel[], session: DryvValidationSession, parent: DryvComplexValidator | undefined, options: DryvOptions, field?: keyof any); | ||
@@ -22,2 +21,3 @@ protected onParentChanged(): void; | ||
private updateItemIndexes; | ||
toJSON(): any; | ||
} |
@@ -18,3 +18,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
this.transparentProxy = dryvValidatableArray(this); | ||
this.proxy = this.updateArray(model); | ||
this.updateArray(model); | ||
} | ||
@@ -29,4 +29,3 @@ onParentChanged() { | ||
const { proxy, register, unregister } = observableArrayProxy(model); | ||
this.proxy = proxy; | ||
this.model = model; | ||
this.model = proxy; | ||
this._items.length = 0; | ||
@@ -42,3 +41,3 @@ for (let i = 0; i < proxy.length; i++) { | ||
this._unregisterArray = () => unregister(eventId); | ||
return this.proxy; | ||
return this.model; | ||
} | ||
@@ -96,3 +95,3 @@ onArrayEvent(event) { | ||
get value() { | ||
return this.proxy; | ||
return this.model; | ||
} | ||
@@ -123,3 +122,6 @@ set value(value) { | ||
} | ||
toJSON() { | ||
return Object.assign(Object.assign({}, super.toJSON()), { _items: undefined }); | ||
} | ||
} | ||
//# sourceMappingURL=DryvArrayValidator.js.map |
@@ -8,2 +8,5 @@ import type { DryvValidationSession } from './'; | ||
private _transparentProxy?; | ||
readonly refs: { | ||
[field: string]: object | undefined; | ||
}; | ||
protected constructor(model: TModel, session: DryvValidationSession<TModel>, parent: DryvComplexValidator | undefined, options: DryvOptions, field?: keyof TModel); | ||
@@ -15,2 +18,3 @@ get transparentProxy(): TTransparentProxy; | ||
refreshDirty(): void; | ||
toJSON(): any; | ||
} |
@@ -7,2 +7,3 @@ import { DryvValidator } from './DryvValidator'; | ||
this._isReverting = false; | ||
this.refs = {}; | ||
} | ||
@@ -38,3 +39,6 @@ get transparentProxy() { | ||
} | ||
toJSON() { | ||
return Object.assign(Object.assign({}, super.toJSON()), { _reference: undefined, _ignoreChildChanges: undefined, _isReverting: undefined, _transparentProxy: undefined }); | ||
} | ||
} | ||
//# sourceMappingURL=DryvComplexValidator.js.map |
@@ -6,3 +6,7 @@ import type { DryvValidationResult, DryvValidationSession } from './'; | ||
private _initialValue; | ||
private _referenceField?; | ||
private _reference; | ||
constructor(model: TModel, session: DryvValidationSession, parent: DryvComplexValidator, options: DryvOptions, field: keyof TModel); | ||
get reference(): any; | ||
private set reference(value); | ||
get value(): any; | ||
@@ -15,4 +19,6 @@ set value(value: any); | ||
validate(): Promise<DryvValidationResult>; | ||
protected onParentChanged(): void; | ||
setValidationResult(response: DryvServerValidationResponse | DryvServerErrors): boolean; | ||
toString(): any; | ||
toJSON(): any; | ||
} |
@@ -16,2 +16,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
get reference() { | ||
return this._reference; | ||
} | ||
set reference(value) { | ||
this._reference = value; | ||
} | ||
get value() { | ||
@@ -21,2 +27,24 @@ return this.model[this.field]; | ||
set value(value) { | ||
const parent = this.parent; | ||
const oldValue = this.value; | ||
if (oldValue === value) { | ||
return; | ||
} | ||
if (parent && this.field && this._referenceField) { | ||
if (typeof value === 'object') { | ||
const reference = value; | ||
const id = reference[this._referenceField]; | ||
if (id !== undefined) { | ||
value = id; | ||
if (parent) { | ||
this.reference = reference; | ||
parent.refs[String(this.field)] = reference; | ||
} | ||
} | ||
} | ||
else { | ||
this.reference = undefined; | ||
parent.refs[String(this.field)] = undefined; | ||
} | ||
} | ||
this.model[this.field] = value; | ||
@@ -50,2 +78,6 @@ } | ||
} | ||
onParentChanged() { | ||
var _a; | ||
this._referenceField = (_a = this.rules.validators.find((v) => !!v.referenceField)) === null || _a === void 0 ? void 0 : _a.referenceField; | ||
} | ||
setValidationResult(response) { | ||
@@ -70,3 +102,6 @@ var _a, _b, _c, _d; | ||
} | ||
toJSON() { | ||
return Object.assign(Object.assign({}, super.toJSON()), { _referenceField: undefined, _reference: undefined, reference: this.reference }); | ||
} | ||
} | ||
//# sourceMappingURL=DryvFieldValidator.js.map |
@@ -10,4 +10,3 @@ import { DryvValidationResult, DryvValidationSession } from './'; | ||
}; | ||
proxy: TModel; | ||
constructor(model: TModel, session: DryvValidationSession<TModel, any>, parent: DryvComplexValidator | undefined, options: DryvOptions, field?: keyof TModel); | ||
constructor(model: TModel, session: DryvValidationSession<TModel>, parent: DryvComplexValidator | undefined, options: DryvOptions, field?: keyof TModel); | ||
private updateModel; | ||
@@ -14,0 +13,0 @@ get value(): TModel; |
@@ -18,3 +18,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
this.transparentProxy = dryvValidatableObject(this); | ||
this.proxy = this.updateModel(model); | ||
this.updateModel(model); | ||
} | ||
@@ -26,5 +26,4 @@ updateModel(model) { | ||
Object.values(this.fields).forEach((field) => field === null || field === void 0 ? void 0 : field.destroy()); | ||
const { proxy, register, unregister } = observableProxy(model); | ||
this.proxy = proxy; | ||
this.model = model; | ||
const { proxy, register, unregister } = observableProxy(model, this); | ||
this.model = proxy; | ||
for (const field in model) { | ||
@@ -49,6 +48,7 @@ this.fields[field] = createValidator(this, proxy[field], proxy, field, this.session, this.options); | ||
this._unregister = () => unregister(eventId); | ||
return this.proxy; | ||
this.updateHierarchy(true); | ||
return proxy; | ||
} | ||
get value() { | ||
return this.proxy; | ||
return this.model; | ||
} | ||
@@ -55,0 +55,0 @@ set value(value) { |
@@ -1,6 +0,6 @@ | ||
import { DryvFieldValidationResult, DryvOptions, DryvValidationResult, DryvValidationRule, DryvValidationRuleSet, DryvValidator } from './.'; | ||
import { DryvFieldValidationResult, DryvOptions, DryvRefsModel, DryvValidationResult, DryvValidationRule, DryvValidationRuleSet, DryvValidator } from './.'; | ||
import { DryvComplexValidator } from './DryvComplexValidator'; | ||
export declare class DryvValidationSession<TModel extends object = any, TParameters = any> { | ||
private options; | ||
private ruleSet; | ||
readonly ruleSet: DryvValidationRuleSet<TModel, TParameters>; | ||
private _depth; | ||
@@ -21,3 +21,3 @@ private _isTriggered; | ||
validateObject(objectValidator: DryvComplexValidator): Promise<DryvValidationResult>; | ||
validateField(field: DryvValidator<TModel>, model?: TModel): Promise<DryvValidationResult>; | ||
validateField(validator: DryvValidator<TModel>, model: DryvRefsModel<TModel>): Promise<DryvValidationResult>; | ||
private canValidateFields; | ||
@@ -29,3 +29,2 @@ private startValidationChain; | ||
private runValidators; | ||
private getModel; | ||
private success; | ||
@@ -32,0 +31,0 @@ private createObjectResults; |
@@ -32,5 +32,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
validateObject(objectValidator) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (yield this.runDisablers(objectValidator.rootModel, (_a = objectValidator.field) !== null && _a !== void 0 ? _a : '')) { | ||
if (yield this.runDisablers(objectValidator.rootModel, objectValidator)) { | ||
objectValidator.clear(); | ||
@@ -66,15 +65,12 @@ return { | ||
} | ||
validateField(field, model) { | ||
validateField(validator, model) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.canValidateFields() || ((_a = this._processedFields) === null || _a === void 0 ? void 0 : _a[field.uniquePath])) { | ||
return this.success(field.path); | ||
if (!this.canValidateFields() || ((_a = this._processedFields) === null || _a === void 0 ? void 0 : _a[validator.uniquePath])) { | ||
return this.success(validator.path); | ||
} | ||
if (!model) { | ||
model = this.getModel(field); | ||
} | ||
const newValidationChain = this.startValidationChain(); | ||
const fieldResult = yield this.validateFieldInternal(model, field); | ||
const result = this.createFieldValidationResult(fieldResult, field); | ||
this.results.fields[field.path] = result.success ? undefined : fieldResult !== null && fieldResult !== void 0 ? fieldResult : undefined; | ||
const fieldResult = yield this.validateFieldInternal(model, validator); | ||
const result = this.createFieldValidationResult(fieldResult, validator); | ||
this.results.fields[validator.path] = result.success ? undefined : fieldResult !== null && fieldResult !== void 0 ? fieldResult : undefined; | ||
if (fieldResult === null || fieldResult === void 0 ? void 0 : fieldResult.group) { | ||
@@ -116,31 +112,29 @@ this.results.groups[fieldResult === null || fieldResult === void 0 ? void 0 : fieldResult.group] = result.success ? undefined : fieldResult; | ||
} | ||
validateFieldInternal(model, validatable) { | ||
var _a, _b; | ||
validateFieldInternal(model, validator) { | ||
var _a, _b, _c; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const field = validatable.field; | ||
if (!field || !this.ruleSet) { | ||
if (!((_b = (_a = validator.rules) === null || _a === void 0 ? void 0 : _a.validators) === null || _b === void 0 ? void 0 : _b.length)) { | ||
return Promise.resolve(null); | ||
} | ||
if (this._processedFields) { | ||
this._processedFields[validatable.uniquePath] = true; | ||
this._processedFields[validator.uniquePath] = true; | ||
} | ||
const rules = (_b = (_a = this.ruleSet) === null || _a === void 0 ? void 0 : _a.validators) === null || _b === void 0 ? void 0 : _b[validatable.path]; | ||
if (!rules || rules.length <= 0) { | ||
if (!((_c = validator.rules.validators) === null || _c === void 0 ? void 0 : _c.length)) { | ||
return Promise.resolve(null); | ||
} | ||
if (yield this.runDisablers(model, field)) { | ||
if (yield this.runDisablers(model, validator)) { | ||
return Promise.resolve(null); | ||
} | ||
return yield this.runValidators(rules, model, validatable); | ||
return yield this.runValidators(model, validator); | ||
}); | ||
} | ||
runDisablers(model, field) { | ||
var _a, _b; | ||
runDisablers(model, validator) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const disablers = (_b = (_a = this.ruleSet) === null || _a === void 0 ? void 0 : _a.disablers) === null || _b === void 0 ? void 0 : _b[field]; | ||
if (disablers && disablers.length > 0) { | ||
for (const rule of disablers) { | ||
if (yield rule.validate(model, this)) { | ||
return true; | ||
} | ||
const disablers = validator.rules.disablers; | ||
if (!(disablers === null || disablers === void 0 ? void 0 : disablers.length)) { | ||
return false; | ||
} | ||
for (const rule of disablers) { | ||
if (yield rule.validate(model, this)) { | ||
return true; | ||
} | ||
@@ -151,3 +145,3 @@ } | ||
} | ||
runValidators(rules, model, validatable) { | ||
runValidators(model, validator) { | ||
var _a; | ||
@@ -157,8 +151,8 @@ return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
for (const rule of rules) { | ||
for (const rule of validator.rules.validators) { | ||
(_a = rule.related) === null || _a === void 0 ? void 0 : _a.forEach((relatedField) => { | ||
if (!relatedField || relatedField === validatable.path) { | ||
if (!relatedField || relatedField === validator.path) { | ||
return; | ||
} | ||
const field = getValidatorByPath(validatable.rootValidator, relatedField); | ||
const field = getValidatorByPath(validator.rootValidator, relatedField); | ||
if (!field) { | ||
@@ -174,3 +168,3 @@ return; | ||
result = { | ||
path: validatable.path, | ||
path: validator.path, | ||
type: 'error', | ||
@@ -192,6 +186,6 @@ text: r, | ||
catch (error) { | ||
console.error(`DRYV: Error validating field '${String(validatable.field)}'`, error); | ||
console.error(`DRYV: Error validating field '${String(validator.field)}'`, error); | ||
if (this.options.exceptionHandling === 'failValidation') { | ||
result = { | ||
path: validatable.path, | ||
path: validator.path, | ||
type: 'error', | ||
@@ -206,8 +200,2 @@ text: 'Validation failed.', | ||
} | ||
getModel(parent) { | ||
while (parent.parent) { | ||
parent = parent.parent; | ||
} | ||
return parent.model; | ||
} | ||
success(path) { | ||
@@ -214,0 +202,0 @@ return { |
@@ -1,2 +0,2 @@ | ||
import { DryvOptions, DryvServerErrors, DryvServerValidationResponse, DryvValidationResult, DryvValidationResultType, DryvValidationSession } from './'; | ||
import { DryvOptions, DryvServerErrors, DryvServerValidationResponse, DryvValidationResult, DryvValidationResultType, DryvValidationRule, DryvValidationSession } from './'; | ||
export declare abstract class DryvValidator<TModel extends object = any, TValue = any, TParent extends DryvValidator = any> { | ||
@@ -9,2 +9,7 @@ model: TModel; | ||
private _index?; | ||
private _rules; | ||
get rules(): { | ||
validators: DryvValidationRule<TModel>[]; | ||
disablers: DryvValidationRule<TModel>[]; | ||
}; | ||
get index(): number | undefined; | ||
@@ -48,3 +53,3 @@ set index(value: number | undefined); | ||
protected onParentChanged(): void; | ||
private updateHierarchy; | ||
protected updateHierarchy(cascade?: boolean): void; | ||
clear(): void; | ||
@@ -51,0 +56,0 @@ setValidationResult(response: DryvServerValidationResponse | DryvServerErrors): boolean; |
export class DryvValidator { | ||
get rules() { | ||
return this._rules; | ||
} | ||
get index() { | ||
@@ -53,3 +56,6 @@ return this._index; | ||
this.field = field; | ||
this._rootModel = model; | ||
this._rules = { | ||
validators: [], | ||
disablers: [] | ||
}; | ||
this._rootValidator = this; | ||
@@ -131,3 +137,3 @@ this._reactive = options.reactiveWrapper({ | ||
updateHierarchy(cascade = false) { | ||
var _a; | ||
var _a, _b, _c, _d; | ||
const parent = this.parent; | ||
@@ -142,6 +148,10 @@ if (parent) { | ||
} | ||
this.path = [parent === null || parent === void 0 ? void 0 : parent.path, this.field].filter((x) => !!x).join('.'); | ||
const path = [parent === null || parent === void 0 ? void 0 : parent.path, this.field].filter((x) => !!x).join('.'); | ||
this.path = path; | ||
this.uniquePath = [parent === null || parent === void 0 ? void 0 : parent.uniquePath, this.index, this.field] | ||
.filter((x) => typeof x === 'number' || !!x) | ||
.join('.'); | ||
const rules = this.rules; | ||
rules.validators = (_b = this.session.ruleSet.validators[path]) !== null && _b !== void 0 ? _b : []; | ||
rules.disablers = (_d = (_c = this.session.ruleSet.disablers) === null || _c === void 0 ? void 0 : _c[path]) !== null && _d !== void 0 ? _d : []; | ||
if (cascade) { | ||
@@ -167,5 +177,5 @@ this.childValidators().forEach((v) => v.updateHierarchy(true)); | ||
toJSON() { | ||
return Object.assign(Object.assign({}, this), { value: this.value, path: this.path, text: this.text, hasError: this.hasError, hasWarning: this.hasWarning, isSuccess: this.isSuccess, uniquePath: this.uniquePath, _parent: undefined, _path: undefined, _rootModel: undefined, _rootValidator: undefined, _reactive: undefined, _initialValue: undefined, _ignoreChildChanges: undefined, _isReverting: undefined, _items: undefined, _uniquePath: undefined, rootValidator: undefined, rootModel: undefined, parent: undefined, model: undefined, proxy: undefined, session: undefined, options: undefined, transparentProxy: undefined }); | ||
return Object.assign(Object.assign({}, this), { value: this.value, path: this.path, text: this.text, hasError: this.hasError, hasWarning: this.hasWarning, isSuccess: this.isSuccess, uniquePath: this.uniquePath, _parent: undefined, _path: undefined, _rootModel: undefined, _rootValidator: undefined, _reactive: undefined, _initialValue: undefined, _ignoreChildChanges: undefined, _isReverting: undefined, _uniquePath: undefined, _rules: undefined, rootValidator: undefined, rootModel: undefined, parent: undefined, model: undefined, session: undefined, options: undefined }); | ||
} | ||
} | ||
//# sourceMappingURL=DryvValidator.js.map |
@@ -11,5 +11,5 @@ import { DryvComplexValidator } from '../DryvComplexValidator'; | ||
if (!/^\d+$/.test(String(prop))) { | ||
const maybeFunction = this.validator.proxy[String(prop)]; | ||
const maybeFunction = this.validator.model[String(prop)]; | ||
if (typeof maybeFunction === 'function') { | ||
return maybeFunction.bind(this.validator.proxy); | ||
return maybeFunction.bind(this.validator.model); | ||
} | ||
@@ -16,0 +16,0 @@ } |
@@ -8,3 +8,3 @@ import { DryvObjectValidator, DryvValidator } from '../.'; | ||
ownKeys(target) { | ||
return target.proxy ? Reflect.ownKeys(target.proxy) : []; | ||
return target.model ? Reflect.ownKeys(target.model) : []; | ||
} | ||
@@ -11,0 +11,0 @@ get(target, prop) { |
@@ -1,9 +0,9 @@ | ||
import { FieldEvent } from '../.'; | ||
import { DryvObjectValidator, type DryvRefsModel, FieldEvent } from '../.'; | ||
export interface FieldEventHandler<TModel extends object> { | ||
(event: FieldEvent<TModel>): void; | ||
} | ||
export declare function observableProxy<TModel extends object>(model: TModel): { | ||
proxy: TModel; | ||
export declare function observableProxy<TModel extends object>(model: TModel, validator: DryvObjectValidator<TModel>): { | ||
proxy: DryvRefsModel<TModel>; | ||
register: (eventHandler: FieldEventHandler<TModel>) => number; | ||
unregister: (id: number) => void; | ||
}; |
@@ -1,3 +0,3 @@ | ||
export function observableProxy(model) { | ||
const proxyHandler = new ObservableProxyHandler(); | ||
export function observableProxy(model, validator) { | ||
const proxyHandler = new ObservableProxyHandler(validator); | ||
const proxy = new Proxy(model, proxyHandler); | ||
@@ -11,6 +11,14 @@ return { | ||
class ObservableProxyHandler { | ||
constructor() { | ||
constructor(_validator) { | ||
this._validator = _validator; | ||
this._eventHandlers = new Map(); | ||
this._nextId = 0; | ||
} | ||
get(target, prop, receiver) { | ||
var _a; | ||
if (prop === '$dryvrefs') { | ||
return (_a = this._validator) === null || _a === void 0 ? void 0 : _a.refs; | ||
} | ||
return Reflect.get(target, prop, receiver); | ||
} | ||
set(target, prop, value, receiver) { | ||
@@ -17,0 +25,0 @@ const oldValue = Reflect.get(target, prop, receiver); |
@@ -10,4 +10,5 @@ import { DryvValidationSession } from './.'; | ||
related?: string[]; | ||
referenceField?: string; | ||
group?: string; | ||
validate: <TInput = TModel>($m: TInput, session: DryvValidationSession<TModel>) => DryvValidateFunctionResult; | ||
validate: <TInput extends object = TModel>($m: DryvRefsModel<TInput>, session: DryvValidationSession<TModel>) => DryvValidateFunctionResult; | ||
} | ||
@@ -96,1 +97,4 @@ export type DrvvRuleInvocations<TModel extends object> = { | ||
}; | ||
export type DryvRefsModel<TModel extends object = any> = TModel & { | ||
$dryvrefs: any; | ||
}; |
{ | ||
"name": "dryvjs", | ||
"version": "1.0.0-pre-48", | ||
"version": "1.0.0-pre-49", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
411102
5981