Why my document is failing my schema validation MongoDB? - mongodb

Validation schema
{
bsonType: 'object',
required: [
'text',
'replaceLinkComponents'
],
properties: {
text: {
bsonType: 'string',
description: 'A introduction text string with replacement pattern \'${}\' that are replaced by replaceLinkComponents'
},
replaceLinkComponents: {
bsonType: 'array',
items: {
bsonType: 'object',
required: [
'text',
'link'
],
properties: {
text: {
bsonType: 'string'
},
link: {
bsonType: 'string'
}
}
}
}
}
}
[
{
text: "${}, ${}, and ${} enthusiast with a knack for ${} graduating with a specialization in AI.",
replaceLinkComponents: [
{ text: "Fullstack Developer", link: "https://github.com/akshaydohroo" },
{ text: "App Developer", link: "https://github.com/akshaydohroo" },
{ text: "Machine Learning", link: "https://www.kaggle.com/akshaydohroo" },
{ text: "problem-solving", link: "https://leetcode.com/akshay23codes/" },
],
},
{
text: "Working with my hands to make magic happen on the internet. View my ${}, ${}, ${}, or send me an email at ${}.",
replaceLinkComponents: [
{ text: "Projects", link: "/projects" },
{
text: "Resume",
link: "/resume",
},
{
text: "Contact Me",
link: "/contact",
},
{
text: "akshay.dohroo3#gmail.com",
link: "mailto:akshay.dohroo3#gmail.com",
},
],
},
]
And the worst part is that there is no prompt why my document validation and even mongosh doesnt give me a detailed reason as well, to debug the issue
What is the issue here and is there a better way to do json schema validation?

Related

MongoDB validation part with any one of the field check

Trying to figure out how to validate an array of object and map(key, value) pair
{
"commonIdentification": {
"CR": "BR",
"SN": "NAVS87397394"
}
"digitalIdentiifcation": {
"UUID": "326f040b-cf14-4cf9-9e67-57f7ca3ce1b2"
}
}
Here , I want to add the validation like,
In commonIdentification with the presence of CR ,no need for digitalIdentiifcation's presence
In commonIdentification with the absence of CR, digitalIdentiifcation's 'UUID' field is required.
Can anyone please help on reformatting the validation part?I am struggling to validate based on the above points.
{
$jsonSchema: {
properties: {
commonIdentification: {
type: 'object',
required: [
'CR'
],
properties: {
CR: {
type: 'string',
description: 'CR is mandatory '
}
}
}
},
type: 'object',
required: [
'commonIdentification',
'digitalIdentiifcation'
]
}
}
{
$jsonSchema: {
type: 'object',
required: [
'commonIdentification'
],
anyOf: [
{
required: [
'digitalIdentiifcation'
]
},
{
required: [
'commonIdentification'
]
}
],
properties: {
commonIdentification: {
type: 'object',
anyOf: [
{
required: [
'CR'
]
},
{
required: [
'SN'
]
}
],
properties: {
SN: {
type: 'string',
description: 'SN or CR or digitalIdentiifcation is mandatory '
},
CR: {
type: 'string',
description: 'SN or CR or digitalIdentiifcation is mandatory '
}
},
description: 'SN or CR or digitalIdentiifcation is mandatory '
},
digitalIdentiifcation: {
description: 'digitalIdentiifcation'
}
}
}
}

MongoDB: While using triggers, Document fails validation for int type

I am using mongodb triggers for automated update of courses_count in student collection.
My student collection is as:
Validation rule for student collection is defined as:
{
$jsonSchema: {
required: [
'name',
'email',
'password',
'dob',
'address'
],
properties: {
name: {
bsonType: 'string'
},
email: {
bsonType: 'string'
},
password: {
bsonType: 'string'
},
dob: {
bsonType: 'date'
},
address: {
bsonType: 'string'
},
courses_count: {
bsonType: [
'int',
'double',
'decimal'
]
}
courses: {
bsonType: 'array',
items: {
required: [
'_id',
'is_completed',
'is_certified'
],
properties: {
_id: {
bsonType: 'objectId'
},
name: {
bsonType: 'string'
},
is_free: {
bsonType: 'bool'
},
is_completed: {
bsonType: 'bool'
},
is_certified: {
bsonType: 'bool'
},
}
}
}
}
}
}
You can clearly see, I've defined courses_count to be int,doubleordecimal
I have defined courses_count_trigger in mongodb atlas as:
exports = async function(changeEvent) {
const { fullDocument } = changeEvent;
const mongodb = context.services.get("vidly");
const docId = changeEvent.documentKey._id;
var courses_count= 0;
if(fullDocument.courses){
courses_count=fullDocument.courses.length;
}
return mongodb.db("coursera").collection("student").updateOne({_id:docId},{ $set:{courses_count: courses_count}});
}
It's fine when there are some courses in my student collection.
It updates correctly using fullDocument.courses.length.
But when there are no courses , it fails to update the document.
i.e. it fails the validation.
I have also checked:
typeof(fullDocument.courses.length) gives number.
typeof(0) is obviously number.
Why does it fails validation? Please help.

How to pass the value of input to the next view of a modal in slack bolt framework?

I'm building a Slack API using bolt (on glitch). I'm new to this so and not sure about how to do this particular idea.
Using a slash command I open a modal that lists three radio inputs and has an action button that will use client.views.update to present a multi-line input.
I would like the option chosen to be the initial value of the multi-line code.
// this is required: github.com/slackapi/bolt
app.command("/slashcommand", async ({ ack, payload, context }) => {
// Acknowledge the command request
ack();
try {
const result = app.client.views.open({
token: context.botToken,
// Pass a valid trigger_id within 3 seconds of receiving it
trigger_id: payload.trigger_id,
// View payload
view: {
type: "modal",
callback_id: 'modal_1',
title: {
type: "plain_text",
text: "Initiate Feedback",
emoji: true
}, /*
submit: {
type: "plain_text",
text: "Submit",
emoji: true
}, */
close: {
type: "plain_text",
text: "Cancel",
emoji: true
},
blocks: [
{
type: "context",
elements: [
{
type: "mrkdwn",
text:
"Modal first view"
}
]
},
{
type: "divider"
},
{
type: "section",
block_id: 'radio_block',
text: {
type: "mrkdwn",
text: "Select from one of the following options:"
},
accessory: {
type: "radio_buttons",
action_id: 'radio_input',
initial_option: {
text: {
type: "plain_text",
text: "One"
},
value: "one",
description: {
type: "plain_text",
text: "describe one"
}
},
options: [
{
text: {
type: "plain_text",
text: "One"
},
value: "one",
description: {
type: "plain_text",
text: "describe one"
}
},
{
text: {
type: "plain_text",
text: "Two"
},
value: "two",
description: {
type: "plain_text",
text: "describe two"
}
},
{
text: {
type: "plain_text",
text: "Three"
},
value: "three",
description: {
type: "plain_text",
text: "describe three"
}
}
]
}
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: "Next",
emoji: true
},
action_id: "next_1"
}
]
}
]
}
});
console.log(result);
} catch (error) {
console.error(error);
}
});
// Listen for a button invocation with action_id `next_1` (assume it's inside of a modal)
app.action("next_1", async ({ ack, body, context }) => {
// VALUE FROM RADIO INPUT
// const val = Radio input value;
// Acknowledge the button request
ack();
try {
const result = app.client.views.update({
token: context.botToken,
// Pass the view_id
view_id: body.view.id,
// View payload with updated blocks
view: {
type: "modal",
// View identifier
callback_id: 'feed_1',
title: {
type: "plain_text",
text: "Share Feedback: message"
},
blocks: [
{
type: "section",
text: {
type: "plain_text",
text: 'You choose '
}
},
{
type: "input",
element: {
type: "plain_text_input",
// HERE IS WHERE THE RADIO OPTION GOES
initial_value: `One `,
multiline: true
},
label: {
type: "plain_text",
text: "Message",
emoji: true
}
}
],
submit: {
type: "plain_text",
text: "Submit"
}
}
});
console.log(result);
} catch (error) {
console.error(error);
}
});
Ok figured this out finally!
1) make sure your radio buttons are not in any input blocks!
2) output your returned payload in your action using body.actions
once you see what your returning it will be easier to target that value.
'''
app.action("next_1", async ({ ack, body, context }) => {
// Result of option selected
const val = JSON.stringify(body['actions'][0]);
// see the values
console.log(val);
// Acknowledge the button request
ack();
});
'''
Further details: https://api.slack.com/reference/interaction-payloads/actions

Not able to get reference to form in sencha

I am trying reset a form, on click of button.
The button's functionality is defined in file seperate controller file.
But on clicking button reset i get error
"Uncaught TypeError: Object form1orig has no method 'reset' "
Controller
Ext.define('myapp.controller.form1', {
extend: 'Ext.app.Controller',
requires: [
'myapp.view.form1'
],
config: {
refs: {
form1orig: '#pressc',
form1Submit: 'button[action=sub]',
form1Reset: 'button[action=res]'
},
control: {
'form1Reset' : {
tap: 'onForm1Reset'
},
'form1Submit' : {
tap: 'onForm1Submit'
}
}
},
onForm1Reset: function(button){
'form1orig'.reset();
console.log('Tapped reset');
},
onForm1Submit: function(button){
console.log('Tapped submit');
}
});
View
Ext.define('myapp.view.form1', {
extend: 'Ext.form.Panel',
requires: [
'Ext.form.FieldSet',
],
xtype: 'form1Me',
id: 'form1Me',
config: {
items: [
{
xtype: 'fieldset',
id: 'pressc',
instructions: 'Please enter the information above.',
defaults: {
labelWidth: '35%'
},
items: [
{
xtype:'textfield',
name: 'name',
label: 'Name',
id: 'eman',
placeHolder: 'Name'
},
{
xtype: 'textareafield',
name : 'Prescription',
id: 'pres',
label: 'Prescription',
placeHolder: 'Enter your prescription here...'
}
]
},
{
xtype: 'container',
defaults: {
xtype: 'button',
style: 'margin: .5em',
flex : 1
},
layout: {
type: 'hbox'
},
items: [
{
text: 'Submit',
id: 'subMe',
action: 'sub',
scope: this,
hasDisabled: false
//handler: function(btn){
/*var presscForm = Ext.getCmp('presscForm');
presscForm.submit({
url: '../../result.php',
method: 'POST',
success: function() {
alert('Thamk you for using our service');
}
});*/
//}
},
{
text: 'Reset',
id: 'resMe',
action: 'res'
/*handler: function(){
Ext.getCmp('form1Me').reset();
}*/
}
]
}
]
}
});
Help
You should do something like:
var form = Ext.ComponentQuery.query('form1Me');
form.reset();

Sencha touch nested list no data

I am new for sencha touch. I using mvc method. Please see my code below
Main.js
Ext.define('test.view.Main', {
extend: 'Ext.tab.Panel',
xtype: 'main',
requires: [
'Ext.TitleBar',
'Ext.Video',
'Ext.dataview.NestedList'
],
config: {
tabBarPosition: 'bottom',
items: [
{
title: 'Welcome',
iconCls: 'home',
styleHtmlContent: true,
scrollable: true,
items: {
docked: 'top',
xtype: 'titlebar',
title: 'Welcome to Sencha Touch 2'
},
html: [
"You've just generated a new Sencha Touch 2 project. What you're looking at right now is the ",
"contents of <a target='_blank' href=\"app/view/Main.js\">app/view/Main.js</a> - edit that file ",
"and refresh to change what's rendered here."
].join("")
},
{
title: 'Get Started',
iconCls: 'action',
items: [
{
docked: 'top',
xtype: 'titlebar',
title: 'Getting Started'
},
{
xtype: 'nestedlist',
}
]
}
]
}
});
Nestedlist.js
Ext.define('bluebutton.view.NestedList', {
extend: 'Ext.NestedList',
xtype: 'nestedlist',
requires: [
'Ext.field.Select',
'Ext.field.Search',
'Ext.plugin.ListPaging',
'Ext.plugin.PullRefresh',
],
config: {
store : { xclass : 'Test.store.data'},
detailContainer: detailContainer,
detailCard: true,
},
});
Test.store.data
Ext.define('Test.store.data', {
extend: 'Ext.data.TreeStore',
config: {
model: 'Test.model.data',
defaultRootProperty: 'items',
root: {
items: [
{
text: 'Drinks',
items: [
{
text: 'Water',
items: [
{ text: 'Still', leaf: true },
{ text: 'Sparkling', leaf: true }
]
},
{ text: 'Soda', leaf: true }
]
},
{
text: 'Snacks',
items: [
{ text: 'Nuts', leaf: true },
{ text: 'Pretzels', leaf: true },
{ text: 'Wasabi Peas', leaf: true }
]
}
]
}
}
});
model.js
Ext.define('Test.model.data', {
extend: 'Ext.data.Model',
config: {
fields: ['text']
}
});
But nested list no able to get the data. I get empty list. Any solution?
If you are providing inline data in store shouldn't it be data attribute instead of root?
Ext.define('Test.store.data', {
extend: 'Ext.data.TreeStore',
config: {
model: 'Test.model.data',
defaultRootProperty: 'items',
data: {
items: [
{
text: 'Drinks',
items: [
{
text: 'Water',
items: [
{ text: 'Still', leaf: true },
{ text: 'Sparkling', leaf: true }
]
},
{ text: 'Soda', leaf: true }
]
},
{
text: 'Snacks',
items: [
{ text: 'Nuts', leaf: true },
{ text: 'Pretzels', leaf: true },
{ text: 'Wasabi Peas', leaf: true }
]
}
]
}
}
});