From 3041a3058db10d04dde8613bedb475088dca9d6e Mon Sep 17 00:00:00 2001 From: Andrey Stukalin Date: Mon, 19 Mar 2018 17:05:40 +0100 Subject: [PATCH] ember-cp-validations migration continues #2 --- app/admin/textreplace/controller.js | 18 +++++------- app/models/lab.js | 34 +++++++++-------------- app/models/patient.js | 32 +++++++++------------ app/models/social-expense.js | 22 +++++++-------- app/models/text-expansion.js | 20 ++++++------- app/models/user.js | 23 +++++++-------- app/utils/email-validation.js | 3 -- package.json | 1 - tests/unit/utils/email-validation-test.js | 14 ---------- yarn.lock | 9 +----- 10 files changed, 62 insertions(+), 114 deletions(-) delete mode 100644 app/utils/email-validation.js delete mode 100644 tests/unit/utils/email-validation-test.js diff --git a/app/admin/textreplace/controller.js b/app/admin/textreplace/controller.js index 2dcb704b88..df14dd93c8 100644 --- a/app/admin/textreplace/controller.js +++ b/app/admin/textreplace/controller.js @@ -1,7 +1,12 @@ import Ember from 'ember'; -import EmberValidations from 'ember-validations'; +import { validator, buildValidations } from 'ember-cp-validations'; -export default Ember.Controller.extend(EmberValidations, { +const Validations = buildValidations({ + 'newExpansion.from': validator('presence', true), + 'newExpansion.to': validator('presence', true) +}); + +export default Ember.Controller.extend(Validations, { hideCancelButton: true, updateCapability: 'update_config', @@ -14,14 +19,5 @@ export default Ember.Controller.extend(EmberValidations, { cancelExpansion() { this.createExpansion(); } - }, - - validations: { - 'newExpansion.from': { - presence: true - }, - 'newExpansion.to': { - presence: true - } } }); diff --git a/app/models/lab.js b/app/models/lab.js index 8bfa94457d..fd10f6cfae 100644 --- a/app/models/lab.js +++ b/app/models/lab.js @@ -3,12 +3,21 @@ import CanEditRequested from 'hospitalrun/mixins/can-edit-requested'; import DateFormat from 'hospitalrun/mixins/date-format'; import DS from 'ember-data'; import Ember from 'ember'; -import PatientValidation from 'hospitalrun/utils/patient-validation'; -import ResultValidation from 'hospitalrun/mixins/result-validation'; +import { validator, buildValidations } from 'ember-cp-validations'; const { computed, get } = Ember; -export default AbstractModel.extend(CanEditRequested, DateFormat, ResultValidation, { +const Validations = buildValidations({ + labTypeName: validator('presence', { + presence: true, + disabled: Ember.computed.not('model.isNew'), + message: 'Please select a lab type' + }), + patient: validator('presence', true), + patientTypeAhead: validator('patient-typeahead') +}); + +export default AbstractModel.extend(CanEditRequested, DateFormat, Validations, { // Attributes customForms: DS.attr('custom-forms'), labDate: DS.attr('date'), @@ -30,22 +39,5 @@ export default AbstractModel.extend(CanEditRequested, DateFormat, ResultValidati requestedDateAsTime: computed('requestedDate', function() { return this.dateToTime(get(this, 'requestedDate')); - }), - - validations: { - labTypeName: { - presence: { - 'if'(object) { - if (object.get('isNew')) { - return true; - } - }, - message: 'Please select a lab type' - } - }, - patientTypeAhead: PatientValidation.patientTypeAhead, - patient: { - presence: true - } - } + }) }); diff --git a/app/models/patient.js b/app/models/patient.js index dde81c443f..a437e2cb2d 100644 --- a/app/models/patient.js +++ b/app/models/patient.js @@ -1,13 +1,23 @@ import AbstractModel from 'hospitalrun/models/abstract'; import DOBDays from 'hospitalrun/mixins/dob-days'; -import EmailValidation from 'hospitalrun/utils/email-validation'; import Ember from 'ember'; import DS from 'ember-data'; import PatientName from 'hospitalrun/mixins/patient-name'; +import { validator, buildValidations } from 'ember-cp-validations'; const { computed, get } = Ember; -export default AbstractModel.extend(DOBDays, PatientName, { +const Validations = buildValidations({ + firstName: validator('presence', true), + lastName: validator('presence', true), + email: validator('format', { + type: 'email', + message: 'Please, enter a valid email address', + allowBlank: true + }) +}); + +export default AbstractModel.extend(DOBDays, PatientName, Validations, { // Attributes admitted: DS.attr('boolean', { defaultValue: false }), additionalContacts: DS.attr(), @@ -94,21 +104,5 @@ export default AbstractModel.extend(DOBDays, PatientName, { shortDisplayName: computed('firstName', 'lastName', function() { return this.getPatientDisplayName(this, true); - }), - - validations: { - email: { - format: { - with: EmailValidation.emailRegex, - allowBlank: true, - message: 'please enter a valid email address' - } - }, - firstName: { - presence: true - }, - lastName: { - presence: true - } - } + }) }); diff --git a/app/models/social-expense.js b/app/models/social-expense.js index e535d9e18a..1648c3d339 100644 --- a/app/models/social-expense.js +++ b/app/models/social-expense.js @@ -2,21 +2,19 @@ * Model for social worker family info */ import DS from 'ember-data'; -import EmberValidations from 'ember-validations'; import { Model } from 'ember-pouch'; +import { validator, buildValidations } from 'ember-cp-validations'; -export default Model.extend(EmberValidations, { +const Validations = buildValidations({ + category: validator('presence', true), + cost: validator('number', { + allowString: true + }) +}); + +export default Model.extend(Validations, { // Attributes category: DS.attr('string'), sources: DS.attr('string'), - cost: DS.attr(), - - validations: { - category: { - presence: true - }, - cost: { - numericality: true - } - } + cost: DS.attr() }); diff --git a/app/models/text-expansion.js b/app/models/text-expansion.js index 7120175047..40909da0cb 100644 --- a/app/models/text-expansion.js +++ b/app/models/text-expansion.js @@ -1,17 +1,13 @@ import DS from 'ember-data'; -import EmberValidations from 'ember-validations'; import { Model } from 'ember-pouch'; +import { validator, buildValidations } from 'ember-cp-validations'; -export default Model.extend(EmberValidations, { - from: DS.attr('string'), - to: DS.attr('string'), +const Validations = buildValidations({ + from: validator('presence', true), + to: validator('presence', true) +}); - validations: { - from: { - presence: true - }, - to: { - presence: true - } - } +export default Model.extend(Validations, { + from: DS.attr('string'), + to: DS.attr('string') }); diff --git a/app/models/user.js b/app/models/user.js index db1c71affe..84274261ad 100644 --- a/app/models/user.js +++ b/app/models/user.js @@ -1,11 +1,17 @@ import DS from 'ember-data'; -import EmailValidation from 'hospitalrun/utils/email-validation'; import Ember from 'ember'; -import EmberValidations from 'ember-validations'; +import { validator, buildValidations } from 'ember-cp-validations'; + +const Validations = buildValidations({ + email: validator('format', { + type: 'email', + message: 'Please, enter a valid email address' + }) +}); const { computed } = Ember; -let User = DS.Model.extend(EmberValidations, { +let User = DS.Model.extend(Validations, { // Attributes derived_key: DS.attr('string'), deleted: DS.attr('boolean'), @@ -26,16 +32,7 @@ let User = DS.Model.extend(EmberValidations, { if (!Ember.isEmpty(roles)) { return roles[0]; } - }), - - validations: { - email: { - format: { - with: EmailValidation.emailRegex, - message: 'Please, enter a valid email address' - } - } - } + }) }); export default User; diff --git a/app/utils/email-validation.js b/app/utils/email-validation.js deleted file mode 100644 index f90ced8708..0000000000 --- a/app/utils/email-validation.js +++ /dev/null @@ -1,3 +0,0 @@ -export default { - emailRegex: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ -}; diff --git a/package.json b/package.json index e87fb2ab0e..1c6ac38847 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,6 @@ "ember-sinon-qunit": "^1.4.0", "ember-source": "2.11.0", "ember-truth-helpers": "2.0.0", - "ember-validations": "2.0.0-alpha.5", "eslint-plugin-ember-suave": "^1.0.0", "express": "^4.8.5", "glob": "^7.1.0", diff --git a/tests/unit/utils/email-validation-test.js b/tests/unit/utils/email-validation-test.js deleted file mode 100644 index aa6af55e0f..0000000000 --- a/tests/unit/utils/email-validation-test.js +++ /dev/null @@ -1,14 +0,0 @@ -import EmailValidation from 'hospitalrun/utils/email-validation'; -import { moduleFor, test } from 'ember-qunit'; - -moduleFor('util:email-validation', 'Unit | Utils | email-validation'); - -test('emailRegex', function(assert) { - assert.notStrictEqual('test@example.com'.match(EmailValidation.emailRegex), null, 'Should be valid with standard email'); - assert.notStrictEqual('test.person@example.com'.match(EmailValidation.emailRegex), null, 'Should be valid with dot'); - assert.notStrictEqual('test+person@example.com'.match(EmailValidation.emailRegex), null, 'Should be valid with plus'); - assert.notStrictEqual('test@example.randomnewtld'.match(EmailValidation.emailRegex), null, 'Should be valid with tld'); - - assert.strictEqual('test@@example.com'.match(EmailValidation.emailRegex), null, 'Should be invalid with two @'); - assert.strictEqual(' test.person@example.com'.match(EmailValidation.emailRegex), null, 'Should be invalid with name'); -}); diff --git a/yarn.lock b/yarn.lock index badebc2e02..29d8539757 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5198,7 +5198,7 @@ ember-fullcalendar@1.8.0: fullcalendar "^3.6.0" fullcalendar-scheduler "^1.8.0" -ember-getowner-polyfill@^1.0.0, ember-getowner-polyfill@^1.1.0, ember-getowner-polyfill@^1.2.2: +ember-getowner-polyfill@^1.1.0, ember-getowner-polyfill@^1.2.2: version "1.2.2" resolved "https://registry.yarnpkg.com/ember-getowner-polyfill/-/ember-getowner-polyfill-1.2.2.tgz#cdab739e89cc8f25af0f78735422df1a61193e92" dependencies: @@ -5454,13 +5454,6 @@ ember-try@^0.2.15: rsvp "^3.0.17" semver "^5.1.0" -ember-validations@2.0.0-alpha.5: - version "2.0.0-alpha.5" - resolved "https://registry.yarnpkg.com/ember-validations/-/ember-validations-2.0.0-alpha.5.tgz#95af5bb5fcf43a5d18a6ddebe64594280bf988c0" - dependencies: - ember-cli-babel "^5.1.6" - ember-getowner-polyfill "^1.0.0" - ember-validators@1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/ember-validators/-/ember-validators-1.0.4.tgz#7d89c2511945c52bd20c5384e715138f902133bb"