0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Update validator to 3.4.0

Closes #1379

- Convert to new api usage for both server-side and client-side
- Provide way require a negative response for boolean methods in
  default-settings.json
- Add field validation functional tests
  - Settings (General)
    - Title length validation
    - Description length validation
    - postsPerPage, numeric, min, max
  - Settings (User)
    - Bio Length validation
    - Location length validation
    - Url validation
  - Login
    - Email validation
  - Editor
    - Title required validation
This commit is contained in:
Jonathan Johnson 2014-02-27 23:51:52 -07:00
parent a17067d8e6
commit 97dce96fa1
4 changed files with 418 additions and 1034 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,4 @@
/*globals window, $, _, Backbone, Validator */
/*globals window, $, _, Backbone, validator */
(function () {
'use strict';
@ -17,7 +17,6 @@
Views : {},
Collections : {},
Models : {},
Validate : new Validator(),
paths: ghostPaths(),
@ -62,21 +61,16 @@
});
};
Ghost.Validate.error = function (object) {
this._errors.push(object);
return this;
};
Ghost.Validate.handleErrors = function () {
validator.handleErrors = function (errors) {
Ghost.notifications.clearEverything();
_.each(Ghost.Validate._errors, function (errorObj) {
_.each(errors, function (errorObj) {
Ghost.notifications.addItem({
type: 'error',
message: errorObj.message || errorObj,
status: 'passive'
});
if (errorObj.hasOwnProperty('el')) {
errorObj.el.addClass('input-error');
}

View file

@ -1,4 +1,4 @@
/*global window, Ghost, $ */
/*global window, Ghost, $, validator */
(function () {
"use strict";
@ -25,14 +25,19 @@
event.preventDefault();
var email = this.$el.find('.email').val(),
password = this.$el.find('.password').val(),
redirect = Ghost.Views.Utils.getUrlVariables().r;
redirect = Ghost.Views.Utils.getUrlVariables().r,
validationErrors = [];
Ghost.Validate._errors = [];
Ghost.Validate.check(email).isEmail();
Ghost.Validate.check(password, "Please enter a password").len(0);
if (!validator.isEmail(email)) {
validationErrors.push("Invalid Email");
}
if (Ghost.Validate._errors.length > 0) {
Ghost.Validate.handleErrors();
if (!validator.isLength(password, 0)) {
validationErrors.push("Please enter a password");
}
if (validationErrors.length) {
validator.handleErrors(validationErrors);
} else {
$.ajax({
url: Ghost.paths.subdir + '/ghost/signin/',
@ -88,18 +93,27 @@
event.preventDefault();
var name = this.$('.name').val(),
email = this.$('.email').val(),
password = this.$('.password').val();
password = this.$('.password').val(),
validationErrors = [];
// This is needed due to how error handling is done. If this is not here, there will not be a time
// when there is no error.
Ghost.Validate._errors = [];
Ghost.Validate.check(name, "Please enter a name").len(1);
Ghost.Validate.check(email, "Please enter a correct email address").isEmail();
Ghost.Validate.check(password, "Your password is not long enough. It must be at least 8 characters long.").len(8);
Ghost.Validate.check(this.submitted, "Ghost is signing you up. Please wait...").equals("no");
if (!validator.isLength(name, 1)) {
validationErrors.push("Please enter a name.");
}
if (Ghost.Validate._errors.length > 0) {
Ghost.Validate.handleErrors();
if (!validator.isEmail(email)) {
validationErrors.push("Please enter a correct email address.");
}
if (!validator.isLength(password, 0)) {
validationErrors.push("Please enter a password");
}
if (!validator.equals(this.submitted, "no")) {
validationErrors.push("Ghost is signing you up. Please wait...");
}
if (validationErrors.length) {
validator.handleErrors(validationErrors);
} else {
this.submitted = "yes";
$.ajax({
@ -152,13 +166,15 @@
submitHandler: function (event) {
event.preventDefault();
var email = this.$el.find('.email').val();
var email = this.$el.find('.email').val(),
validationErrors = [];
Ghost.Validate._errors = [];
Ghost.Validate.check(email).isEmail();
if (!validator.isEmail(email)) {
validationErrors.push("Please enter a correct email address.");
}
if (Ghost.Validate._errors.length > 0) {
Ghost.Validate.handleErrors();
if (validationErrors.length) {
validator.handleErrors(validationErrors);
} else {
$.ajax({
url: Ghost.paths.subdir + '/ghost/forgotten/',

View file

@ -1,4 +1,4 @@
/*global document, Ghost, $, _, Countable */
/*global document, Ghost, $, _, Countable, validator */
(function () {
"use strict";
@ -160,28 +160,32 @@
description = this.$('#blog-description').val(),
email = this.$('#email-address').val(),
postsPerPage = this.$('#postsPerPage').val(),
permalinks = this.$('#permalinks').is(':checked') ? '/:year/:month/:day/:slug/' : '/:slug/';
permalinks = this.$('#permalinks').is(':checked') ? '/:year/:month/:day/:slug/' : '/:slug/',
validationErrors = [];
Ghost.Validate._errors = [];
Ghost.Validate
.check(title, {message: "Title is too long", el: $('#blog-title')})
.len(0, 150);
Ghost.Validate
.check(description, {message: "Description is too long", el: $('#blog-description')})
.len(0, 200);
Ghost.Validate
.check(email, {message: "Please supply a valid email address", el: $('#email-address')})
.isEmail().len(0, 254);
Ghost.Validate
.check(postsPerPage, {message: "Please use a number less than 1000", el: $('postsPerPage')})
.isInt().max(1000);
Ghost.Validate
.check(postsPerPage, {message: "Please use a number greater than 0", el: $('postsPerPage')})
.isInt().min(0);
if (!validator.isLength(title, 0, 150)) {
validationErrors.push({message: "Title is too long", el: $('#blog-title')});
}
if (!validator.isLength(description, 0, 200)) {
validationErrors.push({message: "Description is too long", el: $('#blog-description')});
}
if (!validator.isEmail(email) || !validator.isLength(email, 0, 254)) {
validationErrors.push({message: "Please supply a valid email address", el: $('#email-address')});
}
if (!validator.isInt(postsPerPage) || postsPerPage > 1000) {
validationErrors.push({message: "Please use a number less than 1000", el: $('postsPerPage')});
}
if (!validator.isInt(postsPerPage) || postsPerPage < 0) {
validationErrors.push({message: "Please use a number greater than 0", el: $('postsPerPage')});
}
if (Ghost.Validate._errors.length > 0) {
Ghost.Validate.handleErrors();
if (validationErrors.length) {
validator.handleErrors(validationErrors);
} else {
this.model.save({
title: title,
@ -343,30 +347,33 @@
userEmail = this.$('#user-email').val(),
userLocation = this.$('#user-location').val(),
userWebsite = this.$('#user-website').val(),
userBio = this.$('#user-bio').val();
userBio = this.$('#user-bio').val(),
validationErrors = [];
Ghost.Validate._errors = [];
Ghost.Validate
.check(userName, {message: "Name is too long", el: $('#user-name')})
.len(0, 150);
Ghost.Validate
.check(userBio, {message: "Bio is too long", el: $('#user-bio')})
.len(0, 200);
Ghost.Validate
.check(userEmail, {message: "Please supply a valid email address", el: $('#user-email')})
.isEmail();
Ghost.Validate
.check(userLocation, {message: "Location is too long", el: $('#user-location')})
.len(0, 150);
if (userWebsite.length > 0) {
Ghost.Validate
.check(userWebsite, {message: "Please use a valid url", el: $('#user-website')})
.isUrl()
.len(0, 2000);
if (!validator.isLength(userName, 0, 150)) {
validationErrors.push({message: "Name is too long", el: $('#user-name')});
}
if (Ghost.Validate._errors.length > 0) {
Ghost.Validate.handleErrors();
if (!validator.isLength(userBio, 0, 200)) {
validationErrors.push({message: "Bio is too long", el: $('#user-bio')});
}
if (!validator.isEmail(userEmail)) {
validationErrors.push({message: "Please supply a valid email address", el: $('#user-email')});
}
if (!validator.isLength(userLocation, 0, 150)) {
validationErrors.push({message: "Location is too long", el: $('#user-location')});
}
if (userWebsite.length) {
if (!validator.isURL(userWebsite) || !validator.isLength(userWebsite, 0, 2000)) {
validationErrors.push({message: "Please use a valid url", el: $('#user-website')});
}
}
if (validationErrors.length) {
validator.handleErrors(validationErrors);
} else {
this.model.save({
@ -389,16 +396,20 @@
var self = this,
oldPassword = this.$('#user-password-old').val(),
newPassword = this.$('#user-password-new').val(),
ne2Password = this.$('#user-new-password-verification').val();
ne2Password = this.$('#user-new-password-verification').val(),
validationErrors = [];
Ghost.Validate._errors = [];
Ghost.Validate.check(newPassword, {message: 'Your new passwords do not match'}).equals(ne2Password);
Ghost.Validate.check(newPassword, {message: 'Your password is not long enough. It must be at least 8 characters long.'}).len(8);
if (!validator.equals(newPassword, ne2Password)) {
validationErrors.push("Your new passwords do not match");
}
if (Ghost.Validate._errors.length > 0) {
Ghost.Validate.handleErrors();
if (!validator.isLength(newPassword, 8)) {
validationErrors.push("Your password is not long enough. It must be at least 8 characters long.");
}
if (validationErrors.length) {
validator.handleErrors(validationErrors);
} else {
$.ajax({
url: Ghost.paths.subdir + '/ghost/changepw/',
type: 'POST',