hi i am using MEAN STACK i want to validate JSON keys that comes from front end against mongoose schema. I am validating values but how shall i validate keys that com from client side for example.
var CategorySchema = new Schema({
name: {
type: String,
lowercase: true,
default: '',
trim: true,
unique: [true, 'Category name already exists'],
required: [true, 'Category Name cannot be blank'],
minlength: [4, 'Minimum 4 characters required'],
maxlength: [12, 'Category name cannot be That long']
},
parentCategory: {
type: String,
lowercase: true,
default: '',
trim: true
},
description: {
type: String,
lowercase: true,
default: '',
trim: true,
required: [true, 'description cannot be blank'],
minlength: [10, 'Very short description']
},
imageUrl: {
type: String,
default: '',
trim: true
}
});
what if i am providing this format
{
"IMAGEURL": "c:\abc.png", instead of imageUrl
"DESCRIPTION": "here is some description", INSTEAD OF description
"PARENTCATEGORY": "Men Wear", instead of parentcategory
"Name": "Shirts" instead of name
}
i am writing rest api that will be authenticated is it necessary to check these things. kindly help
For me json schema is so complicated. I suggest to use Json Pattern Validator
npm install jpv
it is very simple to use without any additional keys, and has many patterns to describe json.
import jpv from 'jpv';
var json = {
status : 'OK',
data : {
url : 'http://example.com'
}
}
var pattern = {
status : /^OK$/,
data : {
url : "[url]"
}
}
console.log( jpv.validate(json, pattern ) )
Related
This is the message schema I have been using so far...
{
sender: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: [true, "Please Provide Sender"],
},
content: {
type: String,
max: [2000, "Message can't be longer than 2000 characters"],
trim: true,
required: [true, "Please provide Message"],
},
chat: {
type: mongoose.Schema.Types.ObjectId,
ref: "Chat",
required: [true, "Please Provide Chat Details"],
},
readBy: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
],
}
Now I'd also like to add polls to the chat so
What is the correct way to do that?
One way that I can come up with is to add a type option. And based on that type the message schema changes, but I don't know how to do that...
{
sender:{...},
chat:{...},
readBy:{...},
typeOfMessage:{
........?[prabably a enum of normal string messages and polls object]
........?[also what type (talking about type field) it should have like object or what...]
},
....
content:{
.......?[and based on typeOfMessage my content will be string or object]
}
}
If you find it difficult to understand what I am asking for, tell me.
Thanks
I think that i have to create another schema for polls then add that in my typeOfMessage enum and mongoose validater for validating fields of content according to typeOfMessage value.
It's weird for Mongoose to say that because I'm pretty sure I have the thing.
This is my Mongoose model:
var Job = mongoose.model('Job', new Schema({
_creator: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
companyName: {
type: String,
required: true,
trim: true
},
position: {
type: String,
required: true,
trim: true
},
isCurrent: {
type: Boolean
// required: true
},
startedDate: {
type: Date,
required: true
},
endDate: {
type: Date
},
descriptions: [{
description: {
type: String,
required: true
},
skills: [{
skillId: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
skillName: {
type: String,
required: true
}
}]
}]
}));
And here's the dummy data I'm trying to send to my server, printed right before posting:
Yet it tells me that it requires the path description which is already included in the job object above:
Super confused. Any help appreciated!
This is my JSON file from which I want to access "tr_name" which is inside var InspectorDefs but I can't find the way.Any help please?
JSON FILE:
var InspectorDefs = {
'link': {
inputs: {
attrs: {
'.attributes': {
'tr_name': { type: 'text', group: 'attributes', label: 'Name', index: 1 },
'tr_description': { type: 'text', group: 'attributes', label: 'Description', index: 2 },
'tr_rules': { type: 'select', options: [], group: 'attributes', label: 'Rule', index: 3 },
'tr_value': { type: 'select', options: ['true', 'false'], group: 'attributes', label: 'Value', index: 4 },
'tr_rule_source': { type: 'select', options: ['BPM', 'Drools', 'iLog'], group: 'attributes', label: 'Rule source', index: 5 }
},
},
},
},
};
I want to pass tr_name path here but I am desperate:
cell.on('change:tr_name', function(cell, change, opt) {})
if you are just looking for how to access your properties in javascript then you would reference it like this
InspectorDefs.link.inputs.attrs[".attributes"].tr_description
JSON objects can be referenced as properties or dictionaries, i am assuming you are having issues with the ".attributes"?
JsFiddle
you can use [] as in array or map.
var tr_name = InspectorDefs['link']['inputs']['attrs']['.attributes']['tr_name'];
But in your case I guess you want to call some function when the attribute of some html tag changed. so you can use the actual html tag generated by this json file which can be found from the web page generated or from my guess it is: <Name> tag as read from the json file
I got this SimpleSchema for a collection in my meteor-app
Collection.attachSchema(new SimpleSchema({
title: { type: String },
slug: { type: String, unique: true },
language: { type: String, defaultValue: "en" },
'category.element': { type: String, optional: true }
}));
And I try to insert this JSON-data, but I get insert failed: Error: Category must be an object at getErrorObject
{
"_id" : "25uAB4TfeSfwAFRgv",
"title" : "Test 123",
"slug" : "test_123",
"language" : "en",
"category" : [
{
"element" : "Anything"
}
]
}
What is wrong with my JSON-data? Or what's wrong with my SimpleSchema. I can change both of them to match the best way.
You need to first declare the object, like,
Collection.attachSchema(new SimpleSchema({
...,
....,
category: {type: [Object], optional: true}
}));
After that you can extend/define object field(s) like,
Collection.attachSchema(new SimpleSchema({
....,
....,
category: {type: [Object]},
'category.$.element': {type: String}
}));
use '$' if its a Array Object ([Object]), If only object then then do not use '$'.
If you do not sure about Object Structure, use another parameter blackbox:true
like,
category: {type: [Object], blackbox: true}
The simplest solution is to define category as an array of objects in your schema:
Collection.attachSchema(new SimpleSchema({
title: { type: String },
slug: { type: String, unique: true },
language: { type: String, defaultValue: "en" },
category: { type: [Object], optional: true }
}));
This will get you unstuck.
If you want to be more specific about the contents of category then you can define a sub-schema for category. For example:
CategorySchema = new SimpleSchema({
element: { type: String }
});
Collection.attachSchema(new SimpleSchema({
title: { type: String },
slug: { type: String, unique: true },
language: { type: String, defaultValue: "en" },
category: { type: [CategorySchema], optional: true }
}));
I am trying to create a classic Articles/Categories association with mongoose.
Everything works fine, but since I am trying to expose the query results as JSON, I get a Converting circular structure to JSON error.
I know the issue is related in cross referencing models, but I don't know how to solve this.
Here are my model schemas.
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true
},
content: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
categories: [{
type: Schema.ObjectId,
ref: 'Category'
}]
});
ArticleSchema.statics.load = function(id, cb) {
this.findOne({
_id: id
}).populate('user', 'name username').populate('categories', 'title').exec(cb);
};
/**
* Category Schema
*/
var CategorySchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
});
/**
* Virtual Schema
*/
var articles = CategorySchema.virtual('articles');
articles.get(function () {
return Article.find({categories : { $eq: this }});
});
Seems to be like you have circular references, this mean that the json you create is calling it self in somere.. Im not sure if this line could be the problem:
articles.get(function () {
return Article.find({categories : { $eq: this }});
});
why dont you try with a hardcore value in $eq ??