Skip to content

WIP: New error api #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions addon/-private/internal-result-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const {
isNone,
computed,
canInvoke,
makeArray
makeArray,
getOwner
} = Ember;

const {
Expand All @@ -26,7 +27,12 @@ export default Ember.Object.extend({
model: null,
isValid: true,
isValidating: false,
message: null,

/**
* The result returned by the validator
* @private
*/
result: null,
attribute: '',

attrValue: null,
Expand Down Expand Up @@ -70,16 +76,20 @@ export default Ember.Object.extend({
return !isNone(attrValue);
}),

message: computed.readOnly('error.message'),

messages: computed('message', function() {
return makeArray(get(this, 'message'));
}),

error: computed('isInvalid', 'type', 'message', 'attribute', function() {
let owner = getOwner(this);
if (get(this, 'isInvalid')) {
return ValidationError.create({
return ValidationError.create(owner.ownerInjection(), {
type: get(this, '_type'),
message: get(this, 'message'),
attribute: get(this, 'attribute')
result: get(this, 'result'),
attribute: get(this, 'attribute'),
options: get(this, 'options')
});
}

Expand Down
16 changes: 13 additions & 3 deletions addon/-private/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const {
isArray,
computed,
setProperties,
getProperties
getProperties,
getOwner
} = Ember;

const {
Expand Down Expand Up @@ -43,6 +44,14 @@ const Result = Ember.Object.extend({
*/
attribute: '',

/**
* @property options
* @async
* @private
* @type {Promise}
*/
options: null,

/**
* @property _promise
* @async
Expand Down Expand Up @@ -159,9 +168,10 @@ const Result = Ember.Object.extend({
* @type {Result}
*/
_validations: computed('model', 'attribute', '_promise', '_validator', function() {
let owner = getOwner(this);
return InternalResultObject.extend({
attrValue: computed.readOnly(`model.${get(this, 'attribute')}`)
}).create(getProperties(this, ['model', 'attribute', '_promise', '_validator']));
}).create(owner.ownerInjection(), getProperties(this, ['model', 'attribute', '_promise', '_validator']));
}),

init() {
Expand Down Expand Up @@ -211,7 +221,7 @@ const Result = Ember.Object.extend({
} else if (!get(this, '_isReadOnly')) {
if (typeof result === 'string') {
setProperties(validations, {
message: result,
result,
isValid: false
});
} else if (typeof result === 'boolean') {
Expand Down
13 changes: 12 additions & 1 deletion addon/validations/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Ember from 'ember';

const {
computed
} = Ember;

/**
* @module Validations
* @class Error
Expand All @@ -13,12 +17,19 @@ export default Ember.Object.extend({
*/
type: null,

/**
* The error returned by the validators `validate` method
* @property message
* @type {Any}
*/
result: null,

/**
* The error message
* @property message
* @type {String}
*/
message: null,
message: computed.readOnly('result'),

/**
* The attribute that the error belongs to
Expand Down
12 changes: 7 additions & 5 deletions addon/validations/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ function generateValidationResultsFor(attribute, model, validators, validate, op
value = validate(validator, options);
}

result = validationReturnValueHandler(attribute, value, model, validator);
result = validationReturnValueHandler(attribute, value, model, validator, options);

/*
If the current result is invalid, the rest of the validations do not need to be
Expand Down Expand Up @@ -632,20 +632,22 @@ function extractOptionsDependentKeys(options) {
* @param {Object} model
* @return {ValidationResult}
*/
function validationReturnValueHandler(attribute, value, model, validator) {
function validationReturnValueHandler(attribute, value, model, validator, options) {
let result;
let commonProps = {
model,
attribute,
_validator: validator
_validator: validator,
options
};
let owner = getOwner(model);

if (isPromise(value)) {
result = ValidationResult.create(commonProps, {
result = ValidationResult.create(owner.ownerInjection(), commonProps, {
_promise: Promise.resolve(value)
});
} else {
result = ValidationResult.create(commonProps);
result = ValidationResult.create(owner.ownerInjection(), commonProps);
result.update(value);
}

Expand Down