Comparing version 1.0.0-pre-37 to 1.0.0-pre-38
import { DryvOptions, DryvProxy, DryvValidationRuleSet } from './typings'; | ||
export declare function annotate<TModel extends object>(model: DryvProxy<TModel>, ruleSet: DryvValidationRuleSet<TModel>, options: DryvOptions): void; | ||
export declare function annotate<TModel extends object, TParameters = object>(model: DryvProxy<TModel>, ruleSet: DryvValidationRuleSet<TModel, TParameters>, options: DryvOptions): void; |
@@ -20,3 +20,3 @@ import { dryvProxyHandler } from './dryvProxyHandler'; | ||
.filter((prop) => { var _a; return !((_a = options.excludedFields) === null || _a === void 0 ? void 0 : _a.find((regexp) => regexp.test(prop))); }) | ||
.forEach((prop) => (proxy[prop] = proxy[prop])); | ||
.forEach((prop) => (proxy[prop] = model[prop])); | ||
return proxy; | ||
@@ -23,0 +23,0 @@ } |
@@ -60,3 +60,3 @@ import { dryvProxy, isDryvProxy } from '.'; | ||
} | ||
let targetValue; | ||
let targetValue = value; | ||
let proxy = undefined; | ||
@@ -63,0 +63,0 @@ if (value && typeof value === 'object' && !Array.isArray(value)) { |
@@ -1,2 +0,2 @@ | ||
import type { DryvObject, DryvOptions, DryvValidatableInternal, DryvValidationSession } from './typings'; | ||
export declare function dryvValidatableObject<TModel extends object = any, TValue extends object = any>(field: keyof TModel | undefined, parentOrSession: DryvValidatableInternal | DryvValidationSession<TModel> | undefined, model: TModel, options: DryvOptions): DryvValidatableInternal<TModel, DryvObject<TValue>>; | ||
import { DryvObjectValidator, DryvValidatableObject } from '../core'; | ||
export declare function dryvValidatableObject<TModel extends object>(validator: DryvObjectValidator<TModel>): DryvValidatableObject<TModel>; |
@@ -1,127 +0,39 @@ | ||
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()); | ||
}); | ||
}; | ||
import { isDryvValidatable } from './isDryvValidatable'; | ||
import { dryvValidatableValue } from './dryvValidatableValue'; | ||
import { isDryvProxy } from './isDryvProxy'; | ||
const excludedFromUpdate = { | ||
$model: true, | ||
toJSON: true, | ||
parent: true, | ||
_isDryvValidatable: true, | ||
session: true | ||
}; | ||
export function dryvValidatableObject(field, parentOrSession, model, options) { | ||
const parentIsValidatable = isDryvValidatable(parentOrSession); | ||
const handler = new DryvValidatableObjectHandler(model, parentIsValidatable ? parentOrSession : undefined, parentIsValidatable ? undefined : parentOrSession, options); | ||
const _value = new Proxy({ | ||
$model: model, | ||
toJSON() { | ||
return Object.assign(Object.assign({}, this), { $model: undefined }); | ||
} | ||
}, handler); | ||
const validatable = options.objectWrapper({ | ||
_isDryvValidatable: true, | ||
field, | ||
text: null, | ||
group: null, | ||
type: null, | ||
required: null, | ||
get value() { | ||
return _value; | ||
}, | ||
get parent() { | ||
return handler.parent; | ||
}, | ||
set parent(value) { | ||
handler.parent = value; | ||
}, | ||
get session() { | ||
return handler.session; | ||
}, | ||
get hasError() { | ||
return this.type === 'error'; | ||
}, | ||
get hasWarning() { | ||
return this.type === 'warning'; | ||
}, | ||
get isSuccess() { | ||
return !this.hasError && !this.hasWarning; | ||
}, | ||
get path() { | ||
var _a; | ||
return (((_a = handler.parent) === null || _a === void 0 ? void 0 : _a.path) ? handler.parent.path + '.' : '') + (field ? String(field) : ''); | ||
}, | ||
validate() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const session = handler.session; | ||
if (!session) { | ||
throw new Error('No validation session found'); | ||
} | ||
return session.validateObject(this); | ||
}); | ||
}, | ||
clear() { | ||
validatable.type = null; | ||
validatable.text = null; | ||
validatable.group = null; | ||
Object.values(_value) | ||
.filter((v) => isDryvValidatable(v)) | ||
.forEach((v) => v.clear()); | ||
}, | ||
set(response) { | ||
return Object.values(_value) | ||
.filter((v) => isDryvValidatable(v)) | ||
.reduce((acc, v) => v.set(response) && acc, true); | ||
}, | ||
updateValue(value) { | ||
const model = this.value; | ||
const keys = Array.from(new Set([ | ||
...Object.keys(value), | ||
...Object.keys(model).filter((k) => !excludedFromUpdate[k]) | ||
])); | ||
keys.forEach((key) => { | ||
const entry = value[key]; | ||
const newValue = isDryvValidatable(entry) ? entry.value : entry; | ||
const oldValue = model[key]; | ||
if (isDryvValidatable(oldValue)) { | ||
oldValue.updateValue(newValue); | ||
} | ||
else if (isDryvValidatable(oldValue === null || oldValue === void 0 ? void 0 : oldValue.$model.$validatable)) { | ||
oldValue.$model.$validatable.updateValue(newValue); | ||
} | ||
else { | ||
model[key] = newValue; | ||
} | ||
}); | ||
}, | ||
toJSON() { | ||
return Object.assign(Object.assign({}, this), { parent: undefined, _isDryvValidatable: undefined, session: undefined }); | ||
} | ||
}); | ||
return validatable; | ||
import { DryvFieldValidator, DryvObjectValidator } from '../core'; | ||
export function dryvValidatableObject(validator) { | ||
return new Proxy(validator, new DryvTransparentProxyHandler()); | ||
} | ||
class DryvValidatableObjectHandler { | ||
get session() { | ||
var _a, _b; | ||
return (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.session) !== null && _b !== void 0 ? _b : this._session; | ||
class DryvTransparentProxyHandler { | ||
ownKeys(target) { | ||
return target.proxy ? Reflect.ownKeys(target.proxy) : []; | ||
} | ||
constructor(model, parent, session, options) { | ||
this.model = model; | ||
this.parent = parent; | ||
this.options = options; | ||
this._session = session; | ||
get(target, prop, receiver) { | ||
const innerValue = Reflect.get(target.fields, prop, receiver); | ||
return innerValue instanceof DryvObjectValidator ? innerValue.transparentProxy : innerValue; | ||
} | ||
set(target, field, value, receiver) { | ||
return target.hasOwnProperty(field) || isDryvValidatable(value) || isDryvProxy(value) | ||
? Reflect.set(target, field, value) | ||
: Reflect.set(target, field, dryvValidatableValue(field, receiver, this.session, this.options, () => this.model[field], (value) => (this.model[field] = value))); | ||
set(target, prop, value, receiver) { | ||
const validator = target.fields[prop]; | ||
if (!(validator instanceof DryvFieldValidator)) { | ||
return Reflect.set(target.fields, prop, value, receiver); | ||
} | ||
validator.value = value; | ||
return true; | ||
} | ||
has(target, key) { | ||
return !!target.fields[key]; | ||
} | ||
getOwnPropertyDescriptor(target, key) { | ||
if (!target.fields) | ||
return undefined; | ||
const value = target.fields[key]; | ||
const decriptor = Reflect.getOwnPropertyDescriptor(target.fields, key); | ||
return value | ||
? { | ||
value: value instanceof DryvObjectValidator ? value.transparentProxy : value, | ||
writable: value instanceof DryvObjectValidator ? false : decriptor === null || decriptor === void 0 ? void 0 : decriptor.writable, | ||
enumerable: decriptor === null || decriptor === void 0 ? void 0 : decriptor.enumerable, | ||
configurable: decriptor === null || decriptor === void 0 ? void 0 : decriptor.configurable | ||
} | ||
: undefined; | ||
} | ||
} | ||
//# sourceMappingURL=dryvValidatableObject.js.map |
@@ -1,2 +0,37 @@ | ||
import type { DryvOptions, DryvValidationRuleSet, DryvValidationSession } from './typings'; | ||
export declare function dryvValidationSession<TModel extends object>(options: DryvOptions, ruleSet: DryvValidationRuleSet<TModel>): DryvValidationSession<TModel>; | ||
import { DryvFieldValidationResult, DryvOptions, DryvValidationResult, DryvValidationRule, DryvValidationRuleSet, DryvValidationSession } from '../core/typings'; | ||
import { DryvObjectValidator } from '../core/DryvObjectValidator'; | ||
import { DryvFieldValidator } from '../core/DryvFieldValidator'; | ||
export declare class DryvValidationSessionImplementation<TModel extends object, TParameters = any> implements DryvValidationSession<TModel, TParameters> { | ||
private options; | ||
private ruleSet; | ||
private _depth; | ||
private _excludedFields; | ||
private _isTriggered; | ||
private _processedFields; | ||
readonly results: { | ||
fields: Record<string, DryvFieldValidationResult | undefined>; | ||
groups: Record<string, DryvFieldValidationResult | undefined>; | ||
}; | ||
readonly dryv: { | ||
callServer(url: string, method: string, data: any): Promise<any>; | ||
handleResult(session: DryvValidationSession<TModel>, $m: TModel, field: keyof TModel | string, rule: DryvValidationRule<TModel> | undefined | null, result: any): Promise<any>; | ||
valueOfDate(date: string, locale: string, format: string): number; | ||
}; | ||
constructor(options: DryvOptions, ruleSet: DryvValidationRuleSet<TModel, TParameters>); | ||
get isValidating(): boolean; | ||
validateObject(objectValidator: DryvObjectValidator<TModel>): Promise<DryvValidationResult>; | ||
validateField(field: DryvFieldValidator<TModel>, model?: TModel): Promise<DryvValidationResult>; | ||
private canValidateFields; | ||
private startValidationChain; | ||
private endValidationChain; | ||
private traverseFields; | ||
private isExcludedField; | ||
private validateFieldInternal; | ||
private runDisablers; | ||
private runValidators; | ||
private getModel; | ||
private success; | ||
private createObjectResults; | ||
private createFieldValidationResult; | ||
private hashCode; | ||
} |
@@ -10,88 +10,84 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { isDryvProxy, isDryvValidatable } from '.'; | ||
import { getMemberByPath } from './getMemberByPath'; | ||
export function dryvValidationSession(options, ruleSet) { | ||
if (!options.callServer) { | ||
throw new Error('The callServer option is required.'); | ||
} | ||
if (!options.handleResult) { | ||
throw new Error('The handleResult option is required.'); | ||
} | ||
if (!options.valueOfDate) { | ||
throw new Error('The valueOfDate option is required.'); | ||
} | ||
const _excludedFields = {}; | ||
let _isTriggered = false; | ||
let _depth = 0; | ||
let _processedFields = undefined; | ||
function isValidating() { | ||
return _depth > 0; | ||
} | ||
const session = { | ||
dryv: { | ||
import { DryvValidator } from '../core/DryvValidator'; | ||
import { getValidatorByPath } from '../core/getMemberByPath'; | ||
export class DryvValidationSessionImplementation { | ||
constructor(options, ruleSet) { | ||
this.options = options; | ||
this.ruleSet = ruleSet; | ||
this._depth = 0; | ||
this._excludedFields = {}; | ||
this._isTriggered = false; | ||
this._processedFields = undefined; | ||
this.dryv = { | ||
callServer: options.callServer, | ||
handleResult: options.handleResult, | ||
valueOfDate: options.valueOfDate | ||
}, | ||
results: options.objectWrapper({ | ||
}; | ||
this.results = options.objectWrapper({ | ||
fields: {}, | ||
groups: {} | ||
}), | ||
validateObject(objOrProxy) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const obj = isDryvProxy(objOrProxy) | ||
? objOrProxy.$validatable | ||
: objOrProxy; | ||
if (!obj) { | ||
throw new Error('The value null of undefined is not validatable.'); | ||
} | ||
_depth++; | ||
_isTriggered = true; | ||
try { | ||
const newValidationChain = startValidationChain(); | ||
const fieldResults = yield Promise.all(Array.from(traverseFields(obj.value)).map(([field, value]) => value.validate().then((result) => { var _a; return (Object.assign(Object.assign({}, result), { path: (_a = value.path) !== null && _a !== void 0 ? _a : undefined })); }))); | ||
const result = createObjectResults(fieldResults.filter((r) => !!r)); | ||
obj.type = result.hasErrors ? 'error' : result.hasWarnings ? 'warning' : 'success'; | ||
if (newValidationChain) { | ||
endValidationChain(); | ||
} | ||
return result; | ||
} | ||
finally { | ||
_depth--; | ||
} | ||
}); | ||
}, | ||
validateField(field, model) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!canValidateFields() || (_processedFields === null || _processedFields === void 0 ? void 0 : _processedFields[field.field])) { | ||
return success(field.path); | ||
} | ||
if (!model) { | ||
model = getModel(field); | ||
} | ||
const newValidationChain = startValidationChain(); | ||
const fieldResult = yield validateFieldInternal(session, ruleSet, model, field, options); | ||
const result = createFieldValidationResult(fieldResult, field); | ||
session.results.fields[field.path] = result.success ? undefined : fieldResult !== null && fieldResult !== void 0 ? fieldResult : undefined; | ||
if (fieldResult === null || fieldResult === void 0 ? void 0 : fieldResult.group) { | ||
session.results.groups[fieldResult === null || fieldResult === void 0 ? void 0 : fieldResult.group] = result.success ? undefined : fieldResult; | ||
} | ||
}); | ||
} | ||
get isValidating() { | ||
return this._depth > 0; | ||
} | ||
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 : '')) { | ||
objectValidator.clear(); | ||
return { | ||
success: true, | ||
path: objectValidator.path, | ||
hasErrors: false, | ||
hasWarnings: false, | ||
warningHash: null, | ||
results: [] | ||
}; | ||
} | ||
this._depth++; | ||
this._isTriggered = true; | ||
try { | ||
const newValidationChain = this.startValidationChain(); | ||
const fieldResults = yield Promise.all(Array.from(this.traverseFields(objectValidator.value)).map(([, value]) => value.validate().then((result) => { var _a; return (Object.assign(Object.assign({}, result), { path: (_a = value.path) !== null && _a !== void 0 ? _a : undefined })); }))); | ||
const result = this.createObjectResults(fieldResults.filter((r) => !!r)); | ||
objectValidator.type = result.hasErrors ? 'error' : result.hasWarnings ? 'warning' : 'success'; | ||
if (newValidationChain) { | ||
endValidationChain(); | ||
this.endValidationChain(); | ||
} | ||
return result; | ||
}); | ||
} | ||
}; | ||
return session; | ||
function canValidateFields() { | ||
switch (options.validationTrigger) { | ||
} | ||
finally { | ||
this._depth--; | ||
} | ||
}); | ||
} | ||
validateField(field, 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.field])) { | ||
return this.success(field.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; | ||
if (fieldResult === null || fieldResult === void 0 ? void 0 : fieldResult.group) { | ||
this.results.groups[fieldResult === null || fieldResult === void 0 ? void 0 : fieldResult.group] = result.success ? undefined : fieldResult; | ||
} | ||
if (newValidationChain) { | ||
this.endValidationChain(); | ||
} | ||
return result; | ||
}); | ||
} | ||
canValidateFields() { | ||
switch (this.options.validationTrigger) { | ||
case 'auto': | ||
if (session.$initializing) { | ||
return false; | ||
} | ||
break; | ||
case 'manual': | ||
if (!isValidating()) { | ||
if (!this.isValidating) { | ||
return false; | ||
@@ -101,3 +97,3 @@ } | ||
case 'autoAfterManual': | ||
if (!_isTriggered && !isValidating()) { | ||
if (!this._isTriggered && !this.isValidating) { | ||
return false; | ||
@@ -109,66 +105,81 @@ } | ||
} | ||
function startValidationChain() { | ||
const newValidationChain = !_processedFields; | ||
startValidationChain() { | ||
const newValidationChain = !this._processedFields; | ||
if (newValidationChain) { | ||
_processedFields = {}; | ||
this._processedFields = {}; | ||
} | ||
return newValidationChain; | ||
} | ||
function endValidationChain() { | ||
_processedFields = undefined; | ||
endValidationChain() { | ||
this._processedFields = undefined; | ||
} | ||
function* traverseFields(obj) { | ||
*traverseFields(obj, parentPath) { | ||
var _a, _b; | ||
if (!parentPath) { | ||
parentPath = ''; | ||
} | ||
for (const key in obj) { | ||
if (!(!isExcludedField(key) && obj.hasOwnProperty(key))) { | ||
if (!(!this.isExcludedField(key) && obj.hasOwnProperty(key))) { | ||
console.log('*** excluded field ' + key); | ||
continue; | ||
} | ||
const path = parentPath ? parentPath + '.' + key : key; | ||
const value = obj[key]; | ||
if (typeof value !== 'object') { | ||
console.log('*** what field ' + path); | ||
continue; | ||
} | ||
if (isDryvValidatable(value)) { | ||
yield [key, value]; | ||
const model = (_a = obj.$model) !== null && _a !== void 0 ? _a : obj; | ||
const disablers = (_b = this.ruleSet.disablers) === null || _b === void 0 ? void 0 : _b[key]; | ||
if (disablers && disablers.find((disabler) => disabler.validate(model, this))) { | ||
console.log('*** skipping field ' + path); | ||
continue; | ||
} | ||
if (value instanceof DryvValidator) { | ||
console.log('*** using field ' + path); | ||
yield [path, value]; | ||
} | ||
else { | ||
yield* traverseFields(value); | ||
console.log('*** drilling into field ' + path); | ||
yield* this.traverseFields(value, path); | ||
} | ||
} | ||
} | ||
function isExcludedField(fieldName, path) { | ||
if (!options.excludedFields) { | ||
isExcludedField(fieldName, path) { | ||
if (!this.options.excludedFields) { | ||
return false; | ||
} | ||
const key = path ? path + '.' + fieldName : fieldName; | ||
if (_excludedFields[key] === undefined) { | ||
_excludedFields[key] = !!options.excludedFields.find((regexp) => regexp.test(key)); | ||
if (this._excludedFields[key] === undefined) { | ||
this._excludedFields[key] = !!this.options.excludedFields.find((regexp) => regexp.test(key)); | ||
} | ||
return _excludedFields[key]; | ||
return this._excludedFields[key]; | ||
} | ||
function validateFieldInternal(session, ruleSet, model, validatable, options) { | ||
var _a; | ||
validateFieldInternal(model, validatable) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const field = validatable.field; | ||
if (!field || !ruleSet) { | ||
if (!field || !this.ruleSet) { | ||
return Promise.resolve(null); | ||
} | ||
if (_processedFields) { | ||
_processedFields[field] = true; | ||
if (this._processedFields) { | ||
this._processedFields[field] = true; | ||
} | ||
const rules = (_a = ruleSet === null || ruleSet === void 0 ? void 0 : ruleSet.validators) === null || _a === void 0 ? void 0 : _a[validatable.path]; | ||
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) { | ||
return Promise.resolve(null); | ||
} | ||
if (yield runDisablers(session, ruleSet, model, field)) { | ||
if (yield this.runDisablers(model, field)) { | ||
return Promise.resolve(null); | ||
} | ||
return yield runValidators(session, rules, model, validatable, options); | ||
return yield this.runValidators(rules, model, validatable); | ||
}); | ||
} | ||
function runDisablers(session, ruleSet, model, field) { | ||
var _a; | ||
runDisablers(model, field) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const disablers = (_a = ruleSet === null || ruleSet === void 0 ? void 0 : ruleSet.disablers) === null || _a === void 0 ? void 0 : _a[field]; | ||
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, session)) { | ||
if (yield rule.validate(model, this)) { | ||
return true; | ||
@@ -181,3 +192,3 @@ } | ||
} | ||
function runValidators(session, rules, model, validatable, options) { | ||
runValidators(rules, model, validatable) { | ||
var _a; | ||
@@ -192,10 +203,10 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
const field = getMemberByPath(model.$validatable.value, relatedField); | ||
const field = getValidatorByPath(validatable.rootValidator, relatedField); | ||
if (!field) { | ||
return; | ||
} | ||
session.validateField(field, model); | ||
this.validateField(field, model); | ||
}); | ||
const r = yield rule.validate(model, session); | ||
if (!r) { | ||
const r = yield rule.validate(model, this); | ||
if (!r || r === true) { | ||
} | ||
@@ -222,3 +233,3 @@ else if (typeof r === 'string') { | ||
console.error(`DRYV: Error validating field '${String(validatable.field)}'`, error); | ||
if (options.exceptionHandling === 'failValidation') { | ||
if (this.options.exceptionHandling === 'failValidation') { | ||
result = { | ||
@@ -235,74 +246,73 @@ path: validatable.path, | ||
} | ||
} | ||
function getModel(parent) { | ||
var _a; | ||
while (parent.parent) { | ||
parent = parent.parent; | ||
getModel(parent) { | ||
while (parent.parent) { | ||
parent = parent.parent; | ||
} | ||
return parent.model; | ||
} | ||
return (_a = parent.$model) !== null && _a !== void 0 ? _a : parent.value.$model; | ||
} | ||
function success(path) { | ||
return { | ||
results: [], | ||
success: true, | ||
hasErrors: false, | ||
hasWarnings: false, | ||
warningHash: null, | ||
path | ||
}; | ||
} | ||
function createObjectResults(results) { | ||
const fieldResults = results | ||
.filter((r) => r) | ||
.flatMap((r) => r.results.map((r2) => (Object.assign(Object.assign({}, r2), { path: r.path })))); | ||
const hasWarnings = fieldResults.some((r) => r.type === 'warning'); | ||
const hasErrors = fieldResults.some((r) => r.type === 'error'); | ||
return { | ||
results: fieldResults, | ||
hasErrors: hasErrors, | ||
hasWarnings: hasWarnings, | ||
warningHash: hashCode(fieldResults | ||
.filter((r) => r.type === 'warning') | ||
.map((r) => r.text) | ||
.join()), | ||
success: !hasErrors && !hasWarnings | ||
}; | ||
} | ||
function createFieldValidationResult(result, field) { | ||
var _a; | ||
if (result) { | ||
field.type = result.type; | ||
field.text = result.text; | ||
field.group = result.group; | ||
const type = (_a = result.type) === null || _a === void 0 ? void 0 : _a.toLowerCase(); | ||
return type === 'success' | ||
? success(field.path) | ||
: { | ||
results: [result], | ||
hasErrors: type === 'error', | ||
hasWarnings: type === 'warning', | ||
warningHash: type === 'warning' ? result.text : null, | ||
success: type === 'success' || !type, | ||
path: String(field.path) | ||
}; | ||
success(path) { | ||
return { | ||
results: [], | ||
success: true, | ||
hasErrors: false, | ||
hasWarnings: false, | ||
warningHash: null, | ||
path | ||
}; | ||
} | ||
else { | ||
field.type = 'success'; | ||
field.text = null; | ||
field.group = null; | ||
return success(field.path); | ||
createObjectResults(results) { | ||
const fieldResults = results | ||
.filter((r) => r) | ||
.flatMap((r) => r.results.map((r2) => (Object.assign(Object.assign({}, r2), { path: r.path })))); | ||
const hasWarnings = fieldResults.some((r) => r.type === 'warning'); | ||
const hasErrors = fieldResults.some((r) => r.type === 'error'); | ||
return { | ||
results: fieldResults, | ||
hasErrors: hasErrors, | ||
hasWarnings: hasWarnings, | ||
warningHash: this.hashCode(fieldResults | ||
.filter((r) => r.type === 'warning') | ||
.map((r) => r.text) | ||
.join()), | ||
success: !hasErrors && !hasWarnings | ||
}; | ||
} | ||
} | ||
function hashCode(text) { | ||
if (!text || text.length === 0) { | ||
return ''; | ||
createFieldValidationResult(result, field) { | ||
var _a, _b, _c, _d; | ||
if (result) { | ||
field.type = (_a = result.type) !== null && _a !== void 0 ? _a : 'success'; | ||
field.text = (_b = result.text) !== null && _b !== void 0 ? _b : null; | ||
field.group = (_c = result.group) !== null && _c !== void 0 ? _c : null; | ||
const type = (_d = result.type) === null || _d === void 0 ? void 0 : _d.toLowerCase(); | ||
return type === 'success' | ||
? this.success(field.path) | ||
: { | ||
results: [result], | ||
hasErrors: type === 'error', | ||
hasWarnings: type === 'warning', | ||
warningHash: type === 'warning' ? result.text : null, | ||
success: type === 'success' || !type, | ||
path: String(field.path) | ||
}; | ||
} | ||
else { | ||
field.type = 'success'; | ||
field.text = null; | ||
field.group = null; | ||
return this.success(field.path); | ||
} | ||
} | ||
let hash = 0; | ||
for (let i = 0; i < text.length; i++) { | ||
const chr = text.charCodeAt(i); | ||
hash = (hash << 5) - hash + chr; | ||
hash |= 0; | ||
hashCode(text) { | ||
if (!text || text.length === 0) { | ||
return ''; | ||
} | ||
let hash = 0; | ||
for (let i = 0; i < text.length; i++) { | ||
const chr = text.charCodeAt(i); | ||
hash = (hash << 5) - hash + chr; | ||
hash |= 0; | ||
} | ||
return Math.abs(hash).toString(16); | ||
} | ||
return Math.abs(hash).toString(16); | ||
} | ||
//# sourceMappingURL=dryvValidationSession.js.map |
@@ -1,9 +0,8 @@ | ||
export * from './dryvProxy'; | ||
export * from './dryvValidationSession'; | ||
export * from './isDryvValidatable'; | ||
export * from './isDryvProxy'; | ||
export * from './DryvValidationSessionImplementation'; | ||
export * from './dryvOptions'; | ||
export * from './dryvRuleSet'; | ||
export * from './annotate'; | ||
export * from './defaultDryvOptions'; | ||
export * from './typings'; | ||
export * from './DryvFieldValidator'; | ||
export * from './DryvObjectValidator'; | ||
export * from './DryvValidator'; |
@@ -1,10 +0,9 @@ | ||
export * from './dryvProxy'; | ||
export * from './dryvValidationSession'; | ||
export * from './isDryvValidatable'; | ||
export * from './isDryvProxy'; | ||
export * from './DryvValidationSessionImplementation'; | ||
export * from './dryvOptions'; | ||
export * from './dryvRuleSet'; | ||
export * from './annotate'; | ||
export * from './defaultDryvOptions'; | ||
export * from './typings'; | ||
export * from './DryvFieldValidator'; | ||
export * from './DryvObjectValidator'; | ||
export * from './DryvValidator'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,116 +0,126 @@ | ||
export type DryvValidateFunctionResult = DryvFieldValidationResult | string | null | undefined | Promise<DryvFieldValidationResult | string | null | undefined>; | ||
import { DryvValidator } from '../core/DryvValidator' | ||
export type DryvValidateFunctionResult = | ||
| DryvFieldValidationResult | ||
| string | ||
| boolean | ||
| null | ||
| undefined | ||
| Promise<DryvFieldValidationResult | string | null | undefined> | ||
export interface DryvValidationRule<TModel extends object> { | ||
async?: boolean; | ||
annotations?: { | ||
required?: boolean; | ||
[path: string]: unknown; | ||
}; | ||
related?: string[]; | ||
group?: string; | ||
validate: ($m: TModel, session: DryvValidationSession<TModel>) => DryvValidateFunctionResult; | ||
async?: boolean | ||
annotations?: { | ||
required?: boolean | ||
[path: string]: unknown | ||
} | ||
related?: string[] | ||
group?: string | ||
validate: ($m: TModel, session: DryvValidationSession<TModel>) => DryvValidateFunctionResult | ||
} | ||
export type DrvvRuleInvocations<TModel extends object> = { | ||
[Property in keyof TModel]?: DryvValidationRule<TModel>[]; | ||
[Property in keyof TModel]?: DryvValidationRule<TModel>[] | ||
} & { | ||
[path: string]: DryvValidationRule<TModel>[]; | ||
}; | ||
[path: string]: DryvValidationRule<TModel>[] | ||
} | ||
export interface DryvValidationRuleSet<TModel extends object, TParameters = object> { | ||
validators: DrvvRuleInvocations<TModel>; | ||
disablers?: DrvvRuleInvocations<TModel>; | ||
parameters?: TParameters; | ||
validators: DrvvRuleInvocations<TModel> | ||
disablers?: DrvvRuleInvocations<TModel> | ||
parameters?: TParameters | ||
} | ||
export interface DryvValidationRuleSetResolver { | ||
name: string; | ||
resolve<TModel extends object, TParameters = object>(ruleSetName: string): DryvValidationRuleSet<TModel, TParameters>; | ||
name: string | ||
resolve<TModel extends object, TParameters = object>( | ||
ruleSetName: string | ||
): DryvValidationRuleSet<TModel, TParameters> | ||
} | ||
export interface DryvValidationResult { | ||
results: DryvFieldValidationResult[]; | ||
success: boolean; | ||
hasErrors: boolean; | ||
hasWarnings: boolean; | ||
warningHash: string | undefined | null; | ||
path?: string; | ||
results: DryvFieldValidationResult[] | ||
success: boolean | ||
hasErrors: boolean | ||
hasWarnings: boolean | ||
warningHash: string | undefined | null | ||
path?: string | ||
} | ||
export interface DryvFieldValidationResult { | ||
path?: string; | ||
type?: DryvValidationResultType; | ||
text?: string | null; | ||
group?: string | null; | ||
path?: string | ||
type?: DryvValidationResultType | ||
text?: string | null | ||
group?: string | null | ||
} | ||
export type DryvValidationResultType = 'error' | 'warning' | 'success' | string; | ||
export type DryvProxy<TModel extends object> = TModel & { | ||
$validatable: DryvValidatable<TModel, DryvObject<TModel>>; | ||
}; | ||
export type DryvValidationResultType = 'error' | 'warning' | 'success' | string | ||
export interface DryvGroupValidationResult { | ||
name: string; | ||
results: { | ||
type: DryvValidationResultType; | ||
texts: string[]; | ||
}[]; | ||
name: string | ||
results: { | ||
type: DryvValidationResultType | ||
texts: string[] | ||
}[] | ||
} | ||
export interface DryvValidatable<TModel extends object = any, TValue = any> { | ||
_isDryvValidatable: true; | ||
required?: boolean | null; | ||
text?: string | null; | ||
path?: string | null; | ||
group?: string | null; | ||
groupShown?: boolean | null; | ||
type?: DryvValidationResultType | null; | ||
value?: TValue | undefined; | ||
parent?: DryvValidatable | undefined; | ||
field?: keyof TModel | undefined; | ||
get hasError(): boolean; | ||
get hasWarning(): boolean; | ||
get isSuccess(): boolean; | ||
validate(): Promise<DryvValidationResult>; | ||
clear(): void; | ||
set(response: DryvServerValidationResponse | DryvServerErrors): boolean; | ||
updateValue(value: any): void; | ||
export interface DryvValidationSessionInternal<TModel extends object, TParameters = object> | ||
extends DryvValidationSession<TModel, TParameters> { | ||
$initializing?: boolean | ||
} | ||
export interface DryvValidatableInternal<TModel extends object = any, TValue = any> extends DryvValidatable<TModel, TValue> { | ||
get session(): DryvValidationSession<TModel> | undefined; | ||
export interface DryvValidationSession<TModel extends object, TParameters = any> { | ||
results: { | ||
fields: Record<string, DryvFieldValidationResult | undefined> | ||
groups: Record<string, DryvFieldValidationResult | undefined> | ||
} | ||
validateObject(objectValidator: DryvValidator<TModel>): Promise<DryvValidationResult> | ||
validateField(field: DryvValidator<TModel>, model?: TModel): Promise<DryvValidationResult> | ||
dryv: { | ||
callServer(url: string, method: string, data: any): Promise<any> | ||
handleResult( | ||
session: DryvValidationSession<TModel>, | ||
$m: TModel, | ||
field: keyof TModel | string, | ||
rule: DryvValidationRule<TModel> | undefined | null, | ||
result: any | ||
): Promise<any> | ||
valueOfDate(date: string, locale: string, format: string): number | ||
} | ||
} | ||
export interface DryvValidationGroup<TModel extends object> { | ||
name: string; | ||
fields: DryvValidatable<TModel>; | ||
text?: string | null; | ||
type?: DryvValidationResultType | null; | ||
export interface DryvOptions { | ||
exceptionHandling?: 'failValidation' | 'succeedValidation' | ||
excludedFields?: RegExp[] | ||
reactiveWrapper<TObject>(object: TObject): TObject | ||
callServer?(url: string, method: string, data: any): Promise<DryvServerValidationResponse> | ||
handleResult?<TModel extends object>( | ||
session: DryvValidationSession<TModel>, | ||
$m: TModel, | ||
field: keyof TModel, | ||
rule: DryvValidationRule<TModel>, | ||
result: any | ||
): Promise<any> | ||
valueOfDate?(date: string, locale: string, format: string): number | ||
validationTrigger?: 'immediate' | 'auto' | 'manual' | 'autoAfterManual' | ||
} | ||
export type DryvObject<TModel extends object> = { | ||
[Property in keyof TModel]: TModel[Property] extends boolean ? DryvValidatable<TModel, TModel[Property]> : TModel[Property] extends string ? DryvValidatable<TModel, TModel[Property]> : TModel[Property] extends Date ? DryvValidatable<TModel, TModel[Property]> : TModel[Property] extends Array<infer ArrayType> ? DryvValidatable<TModel, TModel[Property]> : TModel[Property] extends object ? DryvObject<TModel[Property]> : DryvValidatable<TModel, TModel[Property]>; | ||
} & { | ||
$model: DryvProxy<TModel> | undefined; | ||
toJSON(): any; | ||
}; | ||
export interface DryvValidationSessionInternal<TModel extends object> extends DryvValidationSession<TModel> { | ||
$initializing?: boolean; | ||
export type DryvServerValidationResponse = | ||
| any | ||
| { | ||
success: boolean | ||
messages: DryvServerErrors | ||
} | ||
export interface DryvServerErrors { | ||
[field: string]: DryvFieldValidationResult | ||
} | ||
export interface DryvValidationSession<TModel extends object> { | ||
results: { | ||
fields: Record<string, DryvFieldValidationResult | undefined>; | ||
groups: Record<string, DryvFieldValidationResult | undefined>; | ||
}; | ||
validateObject(objOrProxy: DryvValidatable<TModel> | DryvProxy<TModel>): Promise<DryvValidationResult>; | ||
validateField<TValue>(field: DryvValidatable<TModel, TValue>, model?: DryvProxy<TModel>): Promise<DryvValidationResult>; | ||
dryv: { | ||
callServer(url: string, method: string, data: any): Promise<any>; | ||
handleResult(session: DryvValidationSession<TModel>, $m: TModel, field: keyof TModel, rule: DryvValidationRule<TModel> | undefined | null, result: any): Promise<any>; | ||
valueOfDate(date: string, locale: string, format: string): number; | ||
}; | ||
export interface FieldEvent<TModel> { | ||
oldValue: any | ||
newValue: any | ||
field: keyof TModel | ||
} | ||
export interface DryvOptions { | ||
exceptionHandling?: 'failValidation' | 'succeedValidation'; | ||
excludedFields?: RegExp[]; | ||
objectWrapper?<TObject>(object: TObject): TObject; | ||
callServer?(url: string, method: string, data: any): Promise<DryvServerValidationResponse>; | ||
handleResult?<TModel extends object>(session: DryvValidationSession<TModel>, $m: TModel, field: keyof TModel, rule: DryvValidationRule<TModel>, result: any): Promise<any>; | ||
valueOfDate?(date: string, locale: string, format: string): number; | ||
validationTrigger?: 'immediate' | 'auto' | 'manual' | 'autoAfterManual'; | ||
export type DryvValidatableField<TValue = object> = { | ||
path: string | ||
type: DryvValidationResultType | null | ||
text: string | null | ||
group: string | null | ||
groupShown: boolean | ||
success: boolean | ||
hasErrors: boolean | ||
hasWarnings: boolean | ||
warningHash: string | undefined | null | ||
value: TValue | ||
validate(): Promise<DryvValidationResult> | ||
} | ||
export type DryvServerValidationResponse = any | { | ||
success: boolean; | ||
messages: DryvServerErrors; | ||
}; | ||
export interface DryvServerErrors { | ||
[field: string]: DryvFieldValidationResult; | ||
export type DryvValidatableObject<TModel extends object> = { | ||
[Property in keyof TModel]: TModel[Property] extends object | ||
? DryvValidatableObject<TModel[Property]> | ||
: DryvValidatableField<TModel[Property]> | ||
} |
@@ -1,2 +0,10 @@ | ||
export * from './core'; | ||
export * from './transaction'; | ||
export * from './DryvValidationSession'; | ||
export * from './dryvRuleSet'; | ||
export * from './typings'; | ||
export * from './DryvFieldValidator'; | ||
export * from './DryvObjectValidator'; | ||
export * from './DryvValidator'; | ||
export * from './defaultDryvOptions'; | ||
export * from './dryvOptions'; | ||
export * from './DryvValidationSession'; | ||
export { createValidator } from './CreateValidator'; |
@@ -1,3 +0,11 @@ | ||
export * from './core'; | ||
export * from './transaction'; | ||
export * from './DryvValidationSession'; | ||
export * from './dryvRuleSet'; | ||
export * from './typings'; | ||
export * from './DryvFieldValidator'; | ||
export * from './DryvObjectValidator'; | ||
export * from './DryvValidator'; | ||
export * from './defaultDryvOptions'; | ||
export * from './dryvOptions'; | ||
export * from './DryvValidationSession'; | ||
export { createValidator } from './CreateValidator'; | ||
//# sourceMappingURL=index.js.map |
export interface DryvTransactionOptions { | ||
objectWrapper?<TObject>(object: TObject): TObject; | ||
changeHook?(model: any, field: string, newValue: any, oldValue: any): void; | ||
reactiveWrapper?<TObject>(object: TObject): TObject | ||
changeHook?(model: any, field: string, newValue: any, oldValue: any): void | ||
} | ||
export interface DryTransaction<TModel extends object = any> { | ||
rollback: () => void; | ||
commit: () => void; | ||
model: TModel; | ||
dirty: () => boolean; | ||
rollback: () => void | ||
commit: () => void | ||
model: TModel | ||
dirty: () => boolean | ||
} | ||
export declare function dryvTransaction<TModel extends object>(model: TModel, options?: DryvTransactionOptions): DryTransaction<TModel>; | ||
export declare function isDryvTransaction(obj: any): boolean; | ||
export declare function dryvTransaction<TModel extends object>( | ||
model: TModel, | ||
options?: DryvTransactionOptions | ||
): DryTransaction<TModel> | ||
export declare function isDryvTransaction(obj: any): boolean |
@@ -1,80 +0,99 @@ | ||
export type DryvValidationState = 'error' | 'warning' | 'success' | string | undefined; | ||
export type DryvValidationState = 'error' | 'warning' | 'success' | string | undefined | ||
export interface DryvValidationResult { | ||
results: DryvFieldValidationResult[]; | ||
success: boolean; | ||
error: boolean; | ||
warning: boolean; | ||
warningHash: string | undefined | null; | ||
path?: string; | ||
results: DryvFieldValidationResult[] | ||
success: boolean | ||
error: boolean | ||
warning: boolean | ||
warningHash: string | undefined | null | ||
path?: string | ||
} | ||
export interface DryvFieldValidationResult { | ||
path?: string; | ||
state?: DryvValidationState; | ||
message?: string; | ||
group?: string; | ||
path?: string | ||
state?: DryvValidationState | ||
message?: string | ||
group?: string | ||
} | ||
export interface DryvValidatable<TModel = any, TValue = any> { | ||
get required(): boolean; | ||
get message(): string | undefined; | ||
get path(): string; | ||
get group(): string | undefined; | ||
get groupShown(): boolean; | ||
get state(): DryvValidationState; | ||
get fieldKey(): keyof TModel; | ||
get value(): TValue | undefined; | ||
set value(value: TValue | undefined); | ||
get parent(): DryvValidatable | undefined; | ||
set parent(value: DryvValidatable | undefined); | ||
get error(): boolean; | ||
get warning(): boolean; | ||
get success(): boolean; | ||
validate(): Promise<DryvValidationResult>; | ||
reset(): void; | ||
setValidationResults(response: DryvServerValidationResponse | DryvServerErrors): boolean; | ||
get required(): boolean | ||
get message(): string | undefined | ||
get path(): string | ||
get group(): string | undefined | ||
get groupShown(): boolean | ||
get state(): DryvValidationState | ||
get fieldKey(): keyof TModel | ||
get value(): TValue | undefined | ||
set value(value: TValue | undefined) | ||
get parent(): DryvValidatable | undefined | ||
set parent(value: DryvValidatable | undefined) | ||
get error(): boolean | ||
get warning(): boolean | ||
get success(): boolean | ||
validate(): Promise<DryvValidationResult> | ||
reset(): void | ||
setValidationResults(response: DryvServerValidationResponse | DryvServerErrors): boolean | ||
} | ||
export interface DryvServerValidationResponse { | ||
success: boolean; | ||
messages: DryvServerErrors; | ||
success: boolean | ||
messages: DryvServerErrors | ||
} | ||
export interface DryvServerErrors { | ||
[field: string]: DryvFieldValidationResult; | ||
[field: string]: DryvFieldValidationResult | ||
} | ||
export interface DryvValidationSession { | ||
validateField<TModel, TValue>(field: DryvValidatable<TModel, TValue>): Promise<DryvFieldValidationResult>; | ||
validateField<TModel, TValue>( | ||
field: DryvValidatable<TModel, TValue> | ||
): Promise<DryvFieldValidationResult> | ||
} | ||
export interface DryvOptions { | ||
exceptionHandling?: 'failValidation' | 'succeedValidation'; | ||
excludedFields?: RegExp[]; | ||
objectWrapper?<TObject>(object: TObject): TObject; | ||
callServer?(url: string, method: string, data: any): Promise<DryvServerValidationResponse>; | ||
handleResult?<TModel extends object>(session: DryvValidationSession, $m: TModel, field: keyof TModel, rule: DryvValidationRule<TModel>, result: any): Promise<any>; | ||
valueOfDate?(date: string, locale: string, format: string): number; | ||
validationTrigger?: 'immediate' | 'auto' | 'manual' | 'autoAfterManual'; | ||
exceptionHandling?: 'failValidation' | 'succeedValidation' | ||
excludedFields?: RegExp[] | ||
reactiveWrapper?<TObject>(object: TObject): TObject | ||
callServer?(url: string, method: string, data: any): Promise<DryvServerValidationResponse> | ||
handleResult?<TModel extends object>( | ||
session: DryvValidationSession, | ||
$m: TModel, | ||
field: keyof TModel, | ||
rule: DryvValidationRule<TModel>, | ||
result: any | ||
): Promise<any> | ||
valueOfDate?(date: string, locale: string, format: string): number | ||
validationTrigger?: 'immediate' | 'auto' | 'manual' | 'autoAfterManual' | ||
} | ||
export type DryvValidateFunctionResult = DryvFieldValidationResult | string | null | undefined | Promise<DryvFieldValidationResult | string | null | undefined>; | ||
export type DryvValidateFunctionResult = | ||
| DryvFieldValidationResult | ||
| string | ||
| null | ||
| undefined | ||
| Promise<DryvFieldValidationResult | string | null | undefined> | ||
interface DryvValidationContext<TModel extends object> { | ||
callServer(url: string, method: string, data: any): Promise<any>; | ||
handleResult(session: DryvValidationSession, $m: TModel, field: keyof TModel, rule: DryvValidationRule<TModel> | undefined | null, result: any): Promise<any>; | ||
valueOfDate(date: string, locale: string, format: string): number; | ||
callServer(url: string, method: string, data: any): Promise<any> | ||
handleResult( | ||
session: DryvValidationSession, | ||
$m: TModel, | ||
field: keyof TModel, | ||
rule: DryvValidationRule<TModel> | undefined | null, | ||
result: any | ||
): Promise<any> | ||
valueOfDate(date: string, locale: string, format: string): number | ||
} | ||
export interface DryvValidationRule<TModel extends object> { | ||
async?: boolean; | ||
annotations?: { | ||
required?: boolean; | ||
[path: string]: unknown; | ||
}; | ||
related?: string[]; | ||
group?: string; | ||
validate: ($m: TModel, ctx: DryvValidationContext<TModel>) => DryvValidateFunctionResult; | ||
async?: boolean | ||
annotations?: { | ||
required?: boolean | ||
[path: string]: unknown | ||
} | ||
related?: string[] | ||
group?: string | ||
validate: ($m: TModel, ctx: DryvValidationContext<TModel>) => DryvValidateFunctionResult | ||
} | ||
export type DrvvRuleInvocations<TModel extends object> = { | ||
[Property in keyof TModel]?: DryvValidationRule<TModel>[]; | ||
[Property in keyof TModel]?: DryvValidationRule<TModel>[] | ||
} & { | ||
[path: string]: DryvValidationRule<TModel>[]; | ||
}; | ||
[path: string]: DryvValidationRule<TModel>[] | ||
} | ||
export interface DryvValidationRuleSet<TModel extends object = any, TParameters = object> { | ||
validators: DrvvRuleInvocations<TModel>; | ||
disablers?: DrvvRuleInvocations<TModel>; | ||
parameters?: TParameters; | ||
validators: DrvvRuleInvocations<TModel> | ||
disablers?: DrvvRuleInvocations<TModel> | ||
parameters?: TParameters | ||
} | ||
export {}; | ||
export {} |
{ | ||
"name": "dryvjs", | ||
"version": "1.0.0-pre-37", | ||
"version": "1.0.0-pre-38", | ||
"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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
403772
254
5849
4