Comparing version 1.0.0-pre-2 to 1.0.0-pre-3
@@ -1,96 +0,100 @@ | ||
import { dryvProxy, isDryvProxy } from '.'; | ||
import { isDryvValidatable } from '@/core'; | ||
import { dryvValidatableObject } from './dryvValidatableObject'; | ||
import { dryvValidatableValue } from './dryvValidatableValue'; | ||
import { dryvProxy, isDryvProxy } from '.' | ||
import { isDryvValidatable } from '@/core' | ||
import { dryvValidatableObject } from './dryvValidatableObject' | ||
import { dryvValidatableValue } from './dryvValidatableValue' | ||
export function dryvProxyHandler(field, session, options) { | ||
const _excludedFields = {}; | ||
let _dryv = null; | ||
return { | ||
get(target, fieldSymbol, receiver) { | ||
const fieldName = String(fieldSymbol); | ||
if (fieldName === '$dryv') { | ||
return getDryv(receiver); | ||
} | ||
if (isExcludedField(fieldName)) { | ||
return Reflect.get(target, fieldName, receiver); | ||
} | ||
const originalValue = Reflect.get(target, fieldName, receiver); | ||
const field = fieldName; | ||
let resultValue; | ||
if (originalValue && typeof originalValue === 'object') { | ||
resultValue = ensureObjectProxy(originalValue, field, receiver, session); | ||
if (resultValue !== originalValue) { | ||
Reflect.set(target, fieldName, resultValue); | ||
} | ||
} | ||
else if (typeof originalValue !== 'function') { | ||
ensureValueProxy(field, receiver); | ||
resultValue = originalValue; | ||
} | ||
return resultValue; | ||
}, | ||
set(target, fieldSymbol, value, receiver) { | ||
const fieldName = String(fieldSymbol); | ||
if (fieldName === '$dryv') { | ||
throw new Error('The $dryv property is read-only.'); | ||
} | ||
const field = fieldName; | ||
if (typeof value === 'function') { | ||
return Reflect.set(target, field, value); | ||
} | ||
if (isExcludedField(fieldName)) { | ||
return Reflect.set(target, field, value); | ||
} | ||
const originalValue = Reflect.get(target, field, receiver); | ||
if (!value && isDryvProxy(originalValue)) { | ||
originalValue.$dryv.parent = undefined; | ||
} | ||
let targetValue; | ||
let proxy = undefined; | ||
if (typeof value === 'object') { | ||
targetValue = ensureObjectProxy(value, field, receiver, session); | ||
} | ||
else { | ||
proxy = ensureValueProxy(field, receiver); | ||
targetValue = value; | ||
} | ||
const result = Reflect.set(target, field, targetValue); | ||
proxy === null || proxy === void 0 ? void 0 : proxy.validate().catch(console.error); | ||
return result; | ||
const _excludedFields = {} | ||
let _dryv = null | ||
return { | ||
get(target, fieldSymbol, receiver) { | ||
const fieldName = String(fieldSymbol) | ||
if (fieldName === '$dryv') { | ||
return getDryv(receiver) | ||
} | ||
if (isExcludedField(fieldName)) { | ||
return Reflect.get(target, fieldName, receiver) | ||
} | ||
const originalValue = Reflect.get(target, fieldName, receiver) | ||
const field = fieldName | ||
let resultValue | ||
if (originalValue && typeof originalValue === 'object') { | ||
resultValue = ensureObjectProxy(originalValue, field, receiver, session) | ||
if (resultValue !== originalValue) { | ||
Reflect.set(target, fieldName, resultValue) | ||
} | ||
}; | ||
function ensureObjectProxy(value, field, receiver, session) { | ||
const proxy = !isDryvProxy(value) | ||
? dryvProxy(value, field, session, options) | ||
: value; | ||
const dryv = getDryv(receiver); | ||
dryv.value[field] = proxy.$dryv.value; | ||
proxy.$dryv.parent = dryv; | ||
return proxy; | ||
} else if (typeof originalValue !== 'function') { | ||
ensureValueProxy(field, receiver) | ||
resultValue = originalValue | ||
} | ||
return resultValue | ||
}, | ||
set(target, fieldSymbol, value, receiver) { | ||
const fieldName = String(fieldSymbol) | ||
if (fieldName === '$dryv') { | ||
throw new Error('The $dryv property is read-only.') | ||
} | ||
const field = fieldName | ||
if (value && typeof value === 'function') { | ||
return Reflect.set(target, field, value) | ||
} | ||
if (isExcludedField(fieldName)) { | ||
return Reflect.set(target, field, value) | ||
} | ||
const originalValue = Reflect.get(target, field, receiver) | ||
if (!value && isDryvProxy(originalValue)) { | ||
originalValue.$dryv.parent = undefined | ||
} | ||
let targetValue | ||
let proxy = undefined | ||
if (typeof value === 'object') { | ||
targetValue = ensureObjectProxy(value, field, receiver, session) | ||
} else { | ||
proxy = ensureValueProxy(field, receiver) | ||
targetValue = value | ||
} | ||
const result = Reflect.set(target, field, targetValue) | ||
proxy === null || proxy === void 0 ? void 0 : proxy.validate().catch(console.error) | ||
return result | ||
} | ||
function ensureValueProxy(field, receiver) { | ||
const dryv = getDryv(receiver); | ||
const dryvObject = dryv.value; | ||
if (isDryvValidatable(dryvObject[field])) { | ||
return dryvObject[field]; | ||
} | ||
const validatable = dryvValidatableValue(field, dryv, options, () => receiver[field], (value) => (receiver[field] = value)); | ||
const proxy = options.objectWrapper(validatable); | ||
dryvObject[field] = proxy; | ||
return proxy; | ||
} | ||
function ensureObjectProxy(value, field, receiver, session) { | ||
const proxy = !isDryvProxy(value) ? dryvProxy(value, field, session, options) : value | ||
const dryv = getDryv(receiver) | ||
dryv.value[field] = proxy.$dryv.value | ||
proxy.$dryv.parent = dryv | ||
return proxy | ||
} | ||
function ensureValueProxy(field, receiver) { | ||
const dryv = getDryv(receiver) | ||
const dryvObject = dryv.value | ||
if (isDryvValidatable(dryvObject[field])) { | ||
return dryvObject[field] | ||
} | ||
function getDryv(model) { | ||
if (!_dryv) { | ||
_dryv = dryvValidatableObject(field, session, model, options); | ||
} | ||
return _dryv; | ||
const validatable = dryvValidatableValue( | ||
field, | ||
dryv, | ||
options, | ||
() => receiver[field], | ||
(value) => (receiver[field] = value) | ||
) | ||
const proxy = options.objectWrapper(validatable) | ||
dryvObject[field] = proxy | ||
return proxy | ||
} | ||
function getDryv(model) { | ||
if (!_dryv) { | ||
_dryv = dryvValidatableObject(field, session, model, options) | ||
} | ||
function isExcludedField(field) { | ||
var _a; | ||
if (_excludedFields[field] === undefined) { | ||
_excludedFields[field] = !!((_a = options.excludedFields) === null || _a === void 0 ? void 0 : _a.find((regexp) => regexp === null || regexp === void 0 ? void 0 : regexp.test(field))); | ||
} | ||
return _excludedFields[field]; | ||
return _dryv | ||
} | ||
function isExcludedField(field) { | ||
var _a | ||
if (_excludedFields[field] === undefined) { | ||
_excludedFields[field] = !!((_a = options.excludedFields) === null || _a === void 0 | ||
? void 0 | ||
: _a.find((regexp) => (regexp === null || regexp === void 0 ? void 0 : regexp.test(field)))) | ||
} | ||
return _excludedFields[field] | ||
} | ||
} | ||
//# sourceMappingURL=dryvProxyHandler.js.map | ||
//# sourceMappingURL=dryvProxyHandler.js.map |
{ | ||
"name": "dryvjs", | ||
"version": "1.0.0-pre-2", | ||
"version": "1.0.0-pre-3", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
820
59048