Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // require() some stuff from npm (like you were using browserify)
- // and then hit Run Code to run it on the right
- var mongoose = require('mongoose');
- // Set up a Schema with some required elements, and a field with an array
- var ThingSchema = ({
- name: {
- type: String,
- required: true
- },
- quantity: {
- type: Number,
- required: true
- },
- age: {
- type: Number,
- required: true
- },
- // NOTE! Quantity ane Name share the same name as fields in their parent
- things: [{
- quantity: {
- type: Number,
- required: true
- },
- name: {
- type: String,
- required: true
- },
- weight: {
- type: Number,
- required: true
- }
- }]
- });
- // Try to validate a document with none of the required fields
- var doc1 = new mongoose.Document({
- things: [{}]
- }, ThingSchema);
- // The results don't display messages for things.name or things.quntity, but they DO for things.weight
- doc1.validate(function (result) {
- document.getElementById('log1').value = JSON.stringify(result.errors, null, ' ');
- });
- // With values put in for name and quantity on the main doc, validation errors show up for things.name and things.quantity
- var doc2 = new mongoose.Document({
- name: 'Thingamajig',
- quantity: 2,
- things: [{}]
- }, ThingSchema);
- doc2.validate(function (result) {
- document.getElementById('log2').value = JSON.stringify(result.errors, null, ' ');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement