refactor: 💡 update adapter when transforming filter error (#2702)
* refactor: 💡 update adapter when transforming filter error ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580 * style: 💄 add comment about adapter changes for translated field ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580 * style: 💄 move comment to adapter ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580 * style: 💄 update old comment about model fragments ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580 * refactor: 💡 use extractErrors in serializers ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580
This commit is contained in:
1 parent
8087430dd2
commit
f35382a4e0
6 files changed
+69
-6
No files matched your search
@@ -291,9 +291,7 @@ export default class ApplicationAdapter extends RESTAdapter.extend(
|
||||
const errors = fieldErrors.map((error) => ({
|
||||
detail: error.description,
|
||||
// Going forward, nested attributes will be hoisted. Thus "attributes."
|
||||
// must be stripped from the error name key. Until all models are
|
||||
// transitioned away from model fragments, however, both this and the
|
||||
// prior error key mappings are necessary.
|
||||
// must be stripped from the error name key.
|
||||
source: {
|
||||
pointer: `/data/attributes/${error.name.replace('attributes.', '')}`,
|
||||
},
|
||||
|
||||
@@ -95,4 +95,18 @@ export default class HostSetSerializer extends ApplicationSerializer {
|
||||
|
||||
return super.normalize(typeClass, normalizedHash, ...rest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts errors from the payload and transforms specific keys if needed.
|
||||
* @override
|
||||
* @return {object}
|
||||
*/
|
||||
extractErrors() {
|
||||
const errors = super.extractErrors(...arguments);
|
||||
if (errors?.filter) {
|
||||
errors.filter_string = errors.filter;
|
||||
delete errors.filter;
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
@@ -53,4 +53,18 @@ export default class ManagedGroupSerializer extends ApplicationSerializer {
|
||||
|
||||
return super.normalize(typeClass, normalizedHash, ...rest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts errors from the payload and transforms specific keys if needed.
|
||||
* @override
|
||||
* @return {object}
|
||||
*/
|
||||
extractErrors() {
|
||||
const errors = super.extractErrors(...arguments);
|
||||
if (errors?.filter) {
|
||||
errors.filter_string = errors.filter;
|
||||
delete errors.filter;
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
@@ -208,4 +208,22 @@ module('Unit | Serializer | host set', function (hooks) {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('it handles errors correctly', function (assert) {
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('host-set');
|
||||
const schema = store.modelFor('host-set');
|
||||
|
||||
const errors = {
|
||||
base: ['Error in provided request.'],
|
||||
filter: ['This field is required.'],
|
||||
};
|
||||
|
||||
const result = serializer.extractErrors(store, schema, errors, null);
|
||||
|
||||
assert.deepEqual(result, {
|
||||
base: ['Error in provided request.'],
|
||||
filter_string: ['This field is required.'],
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -183,7 +183,8 @@ module('Unit | Serializer | Managed group', function (hooks) {
|
||||
test('it normalizes correctly', function (assert) {
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('managed-group');
|
||||
const hostSetModelClass = store.createRecord('managed-group').constructor;
|
||||
const managedGroupModelClass =
|
||||
store.createRecord('managed-group').constructor;
|
||||
const payload = {
|
||||
id: '1',
|
||||
description: 'Description for the test',
|
||||
@@ -196,7 +197,7 @@ module('Unit | Serializer | Managed group', function (hooks) {
|
||||
};
|
||||
const normalized = serializer.normalizeSingleResponse(
|
||||
store,
|
||||
hostSetModelClass,
|
||||
managedGroupModelClass,
|
||||
payload,
|
||||
);
|
||||
|
||||
@@ -219,4 +220,22 @@ module('Unit | Serializer | Managed group', function (hooks) {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('it handles errors correctly', function (assert) {
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('managed-group');
|
||||
const schema = store.modelFor('managed-group');
|
||||
|
||||
const errors = {
|
||||
base: ['Error in provided request.'],
|
||||
filter: ['This field is required.'],
|
||||
};
|
||||
|
||||
const result = serializer.extractErrors(store, schema, errors, null);
|
||||
|
||||
assert.deepEqual(result, {
|
||||
base: ['Error in provided request.'],
|
||||
filter_string: ['This field is required.'],
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -66,7 +66,7 @@
|
||||
<F.HelperText>{{t 'form.filter.help'}}</F.HelperText>
|
||||
{{#if @model.errors.filter_string}}
|
||||
<F.Error as |E|>
|
||||
{{#each @model.errors.name as |error|}}
|
||||
{{#each @model.errors.filter_string as |error|}}
|
||||
<E.Message>{{error.message}}</E.Message>
|
||||
{{/each}}
|
||||
</F.Error>
|
||||
|
||||
Reference in new issue
Block a user