Ghost/ghost/admin/app/validators/member.js
Kevin Ansfield 786e0ac9c5
Updated ValidationEngine to support bare native class models (#15567)
no issue

- if a plain native class instance with tracked properties is validated against the `ValidationEngine` and it's associated validators would cause errors by assuming that the instance has a `.get()` method
- updated all model access in `ValidationEngine` and the validators to use direct property access which works for both native class and `EmberObject` instances
2022-10-07 20:13:42 +01:00

44 lines
1.2 KiB
JavaScript

import BaseValidator from './base';
import validator from 'validator';
import {isBlank} from '@ember/utils';
export default BaseValidator.create({
properties: ['name', 'email', 'note'],
name(model) {
if (!validator.isLength(model.name || '', 0, 191)) {
model.errors.add('name', 'Name cannot be longer than 191 characters.');
this.invalidate();
}
},
email(model) {
let email = model.email;
if (isBlank(email)) {
model.errors.add('email', 'Please enter an email.');
this.invalidate();
} else if (!validator.isEmail(email)) {
model.errors.add('email', 'Invalid Email.');
this.invalidate();
}
if (!validator.isLength(model.email || '', 0, 191)) {
model.errors.add('email', 'Email cannot be longer than 191 characters.');
this.invalidate();
}
model.hasValidated.addObject('email');
},
note(model) {
let note = model.note;
if (!validator.isLength(note || '', 0, 500)) {
model.errors.add('note', 'Note is too long.');
this.invalidate();
}
model.hasValidated.addObject('note');
}
});