I created a model with validation like below:
sequelize
.define('Email',
{
email: { type: DataTypes.STRING, allowNull: false, unique:false, defaultValue: 'placeholder',
validate: {
isEmail: true,
}
}
});
If I try and create an email with an empty 'email' field, it generates this error:
{
"error": {
"__raw": [
null
],
"email": [
"Validation isEmail failed"
]
}
What is the __raw field and how can I remove it?
Try this validation
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
emailValidation: function() {
var pattern = /^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*#[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/;
if(!pattern.test(this.email)) {
throw new Error('INVALID_EMAIL');
}
}
}
}
Hope this is helpful
Related
I am getting the below error whenever I tried to call any call from serverless framework lambda
[offline] _____ HANDLER RESOLVED _____
offline: Failure: product.hasMany called with something that's not a subclass of Sequelize.Model
Error: product.hasMany called with something that's not a subclass of Sequelize.Model
at Function.hasMany (C:\Users\Kiran\Documents\Projects\Rentals-Backend\node_modules\sequelize\lib\associations\mixin.js:18:13)
at Function.Product.associate (C:\Users\Kiran\Documents\Projects\Rentals-Backend\entity\product.js:21:17)
IMPORTANT
Below code is the answer for the above error. You might missed any steps. So you can refer and fix. Thanks Anatoly who helped me to solve the problem.
Product model:
const { STRING, BOOLEAN, INTEGER } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
const Product = sequelize.define("product", {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
name: { type: STRING },
description: { type: STRING, allowNull: true },
purchase_price: { type: STRING },
tax: { type: STRING },
sale_price: { type: STRING },
categoryId: { type: STRING },
status: { type: BOOLEAN, defaultValue: 0 },
created_on: { type: INTEGER, allowNull: true },
updated_on: { type: INTEGER, allowNull: true },
}, {
timestamps: false,
freezeTableName: true,
})
Product.associate = function (models) {
Product.hasMany(models.product_image, { as: "images" });
Product.belongsTo(models.product_category, { as: "category", foreignKey: 'categoryId' });
};
return Product;
}
Image model:
const { STRING, BOOLEAN, INTEGER } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
const ProductImage = sequelize.define("product_image", {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
productId: { type: INTEGER },
fileName: { type: STRING },
url: { type: STRING },
position: { type: INTEGER },
isDefault: { type: BOOLEAN, defaultValue: 0 },
shopId: { type: STRING },
status: { type: BOOLEAN, defaultValue: 0 },
created_on: { type: INTEGER, allowNull: true },
updated_on: { type: INTEGER, allowNull: true },
}, {
timestamps: false,
freezeTableName: true,
})
return ProductImage;
}
Category model:
const { STRING, BOOLEAN, INTEGER } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
const ProductCategory = sequelize.define("product_category", {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
name: { type: STRING },
description: { type: STRING, allowNull: true },
status: { type: BOOLEAN, defaultValue: 0 },
created_on: { type: INTEGER, allowNull: true },
updated_on: { type: INTEGER, allowNull: true },
}, {
timestamps: false,
freezeTableName: true,
});
return ProductCategory;
}
This is the config file where we initialize sequelize
Config file
const Sequelize = require('sequelize')
const fs = require('fs')
const path = require('path')
const db = {}
const models = path.join(__dirname, '..', 'entity')
var basename = path.basename(module.filename)
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD,
{
dialect: 'mysql',
host: process.env.DB_HOST,
port: process.env.DB_PORT,
logging: false
}
)
fs
.readdirSync(models)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
})
.forEach(function (file) {
var model = require(path.join(models, file))(
sequelize,
Sequelize.DataTypes
);
db[model.name] = model;
})
Object.keys(db).forEach(function (modelName) {
if (db[modelName].associate) {
db[modelName].associate(db)
}
})
db.Sequelize = Sequelize
db.sequelize = sequelize
module.exports = db
Here we are calling product details.
Calling function
const db = require('../config/sequelize-config');
exports.getProductById = (query, username, shopId) => {
return new Promise((resolve, reject) => {
db.product.findOne({
where: {
id: query.id
},
attributes: ['id', 'name', 'description', ['purchase_price', 'purchasePrice'], 'tax', ['sale_price', 'salePrice']],
include: [{
model: db.product_image,
as: 'images',
where: {
status: 1
},
required: false,
attributes: ['id', 'fileName', 'position', 'url']
},
{
model: db.product_category,
as: 'category',
required: false,
attributes: ['id', 'name']
}]
}).then(product => {
if (product) {
resolve({ [KEY_STATUS]: 1, [KEY_MESSAGE]: "Product details fetched successfully", [KEY_DATA]: product });
} else {
reject({ [KEY_STATUS]: 0, [KEY_MESSAGE]: "Product details fetch failed" });
}
}).catch(error => {
reject({ [KEY_STATUS]: 0, [KEY_MESSAGE]: "Product details fetch failed", [KEY_ERROR]: error.message });
});
})
}
To avoid cross-reference errors and similar ones I recommend converting model definitions to functions and registering models and associations in the same one module, see this answer and the question
I'm having an issue with mongoosastic in combination with KeystoneJS. I want to search for all the posts of a specific user with a match on the title. I have the following model:
var keystone = require('keystone'),
Types = keystone.Field.Types,
mongoosastic = require('mongoosastic');
User = keystone.list('User');
var Post = new keystone.List('Post');
Post.add({
title: {
type: String,
required: true,
es_indexed: true
},
state: {
type: Types.Select,
options: 'draft, published, archived',
default: 'draft',
es_indexed: true
},
author: {
type: Types.Relationship,
ref: 'User',
es_schema: User,
es_include_in_parent: true,
es_indexed:true
},
publishedDate: {
type: Types.Date,
index: true,
dependsOn: {
state: 'published'
}
},
content: {
extended: {
type: Types.Html,
wysiwyg: true,
height: 400,
es_indexed: true
}
},
});
Post.schema.plugin(mongoosastic, {
hosts: [
process.env.SEARCHBOX_SSL_URL
]
});
Post.defaultColumns = 'title, state|20%, author|20%, publishedDate|20%';
Post.register();
Post.model.createMapping(function(err, mapping) {
if (err) {
console.log('error creating mapping', err);
} else {
console.log('mapping created for Post');
}
});
User.schema.plugin(mongoosastic, {
populate: [
{path: 'author'}
]
});
I tried the approach of "Indexing Mongoose references" (https://github.com/mongoosastic/mongoosastic#indexing-mongoose-references) but I can't get it to work. Elasticsearch is not returning any hits.. My search query:
{
"query": {
"filtered": {
"query": { "match": { "title": "req.query.query" }},
"filter": { "term": { "author": "req.query.userId" }} // or author._id
}
}
}
Can someone provide me the correct Post model configuration & search query? Thanks!
I am having an issue with validation on certain fields. I want to only validate on a couple of fields, and the other fields should not validate. In my Email field, I am firing a function to check proper formatting, but the other fields are simply set to validate. Any help would be greatly appreciated.
model: {
id: "UserID",
fields: {
UserID: { editable: false },
CompanyID: { editable: false },
FirstName: { type: "string", validation: { required: { message: "Name is required"} } },
LastName: { type: "string", validation: { required: { message: "Name is required" } } },
Email: {
type: "string",
validation: {
required: { message: "Email is required." },
validateEmailFormat: function(input) {
if (input.attr("data-bind") == "value:Email") {
input.attr("data-validateEmailFormat-msg", "Email format invalid.");
return checkEmail(input.val());
}
return true;
}
}
},
PhoneNumber: { type: "string" },
Extension: { type: "string" }
}
}
With this code, all fields are being validated when trying to save/update. I don't want Extension or PhoneNumber to validate.
In your update/Save action, You can remove your fields from the ModelState which don't want to valid, like :
ModelState.Remove("PhoneNumber");
ModelState.Remove("Extension");
http://jsfiddle.net/bhoff/ZCyPx/50/
$("#grid").kendoGrid({
dataSource:{
data:entries,
schema:{
parse:function (response) {
$.each(response, function (idx, elem) {
if (elem.time && typeof elem.time === "string") {
elem.time = kendo.parseDate(elem.time, "HH:mm:ss");
}
if (elem.datetime && typeof elem.datetime === "string") {
elem.datetime = kendo.parseDate(elem.datetime, "HH:mm:ss");
}
});
return response;
}
}
},
columns:[
{ command: [ "edit" ] },
{ field:"type", title:"Cash Transation Type" },
{ field:"begintime", title:"Begin Time(CT)", format:"{0:hh:mm tt}", editor: timeEditor },
{ field:"endtime", title:"End Time(CT)", format:"{0:hh:mm tt}", editor: timeEditor },
],
editable:"inline",
navigatable:true
});
Based on my example how do I stop the user from editing my "Cash Transation Type" column?
Does it have something to do with this -> editable:"inline" ?
look here
you need to set in the datasource
<script>
var dataSource = new kendo.data.DataSource({
schema: {
model: {
id: "ProductID",
fields: {
ProductID: {
//this field will not be editable (default value is true)
editable: false,
// a defaultValue will not be assigned (default value is false)
nullable: true
},
ProductName: {
//set validation rules
validation: { required: true }
},
UnitPrice: {
//data type of the field {Number|String|Boolean|Date} default is String
type: "number",
// used when new model is created
defaultValue: 42,
validation: { required: true, min: 1 }
}
}
}
}
});
</script>
You would normally set this on your DataSource on the schema.model.fields.
var data = new kendo.data.DataSource({
schema: {
model: {
fields: {
type: { editable: "false" }
Add editable in the field you do not want to enable Edit,
columns:[
{ command: [ "edit" ] },
{ field:"type", title:"Cash Transation Type", editable: false },
{ field:"begintime", title:"Begin Time(CT)", format:"{0:hh:mm tt}", editor: timeEditor },
{ field:"endtime", title:"End Time(CT)", format:"{0:hh:mm tt}", editor: timeEditor },
],
editable:"inline",
navigatable:true
});
When I add remote to the jquery validate my form submits even if there are errors, when I remove remote it works properly and you can't submit the form and unless you have filled in all fields? Any ideas? code is below
<script>
$(document).ready(function () {
$("#SignUp").validate({
onsubmit: true,
onkeyup: function(element) { $(element).valid(); },
rules: {
email: {
required: true,
email: true
},
password: {
required: true,
password: true
},
confirm_password: {
equalTo: "#password",
required: true
},
company: {
nls:true,
required: true
},
telephone: {
required: true,
phoneUK: true
},
email2: {
email: true
},
website: {
url: true
},
address1: {
nls:true
},
address2: {
nls:true
},
town: {
nls:true
},
postcode: {
postcodeUK:true
},
country: {
selectcountry:true
},
terms:{
required:true
},
answer:{
remote: "Captcha/check-captcha.php",
type: "POST",
data: {
captcha: function() {
return $("#answer").val();
}
}
}
},
messages:{
email:
{
required: "Please Enter an Email Address"
},
password:
{
required: "Please Enter a Password"
},
confirm_password:
{
required: "Please Confirm Your Password"
},
company:
{
required: "Please Enter Your Company/Climbing Gym Name"
},
telephone:
{
required: "Please Enter a Telephone Number"
},
terms:
{
required: "Please Agree Our Terms and Conditions"
},
answer:{
remote: "You Have Entered The Captcha Correctly When This Message Disappears"
}
}
});
$.validator.addMethod("password", function (value, element) {
return this.optional(element) || /^[A-Za-z0-9!##$%^&*()_]{6,16}$/i.test(value);
}, "Passwords are 6-16 characters");
$.validator.addMethod("nls", function(value, element)
{
return this.optional(element) || /^[a-zA-Z0-9\s.\-_']+$/i.test(value);
}, "Please Only Enter Alpha Numeric Characters and Spaces");
jQuery.validator.addMethod('selectcountry', function (value) {
return (value != 'Nothing');
}, "Please Select a Country");
$.validator.addMethod("url", function(value, element)
{
return this.optional(element) || /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i.test(value);
}, "Please Enter A Valid Website URL");
});
</script>
PHP is below:
<?php
session_start();
if(strtolower($_REQUEST['answer']) == $_SESSION['captcha']){
echo 'true';die;
}else{
echo 'false';die;
}
?>
The validation still works onkeyup etc with and without remote but just not on submit?
The remote rule syntax is slightly off in your case
answer: {
required: true,
remote: {
url: "Captcha/check-captcha.php",
type: "POST",
data: {
captcha: function () {
return $("#answer").val();
}
}
}
}