Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. // require() some stuff from npm (like you were using browserify)
  2. // and then hit Run Code to run it on the right
  3. var mongoose = require('mongoose');
  4.  
  5. // Set up a Schema with some required elements, and a field with an array
  6. var ThingSchema = ({
  7. name: {
  8. type: String,
  9. required: true
  10. },
  11. quantity: {
  12. type: Number,
  13. required: true
  14. },
  15. age: {
  16. type: Number,
  17. required: true
  18. },
  19. // NOTE! Quantity ane Name share the same name as fields in their parent
  20. things: [{
  21. quantity: {
  22. type: Number,
  23. required: true
  24. },
  25. name: {
  26. type: String,
  27. required: true
  28. },
  29. weight: {
  30. type: Number,
  31. required: true
  32. }
  33. }]
  34. });
  35.  
  36. // Try to validate a document with none of the required fields
  37. var doc1 = new mongoose.Document({
  38. things: [{}]
  39. }, ThingSchema);
  40.  
  41. // The results don't display messages for things.name or things.quntity, but they DO for things.weight
  42. doc1.validate(function (result) {
  43. document.getElementById('log1').value = JSON.stringify(result.errors, null, ' ');
  44. });
  45.  
  46. // With values put in for name and quantity on the main doc, validation errors show up for things.name and things.quantity
  47. var doc2 = new mongoose.Document({
  48. name: 'Thingamajig',
  49. quantity: 2,
  50. things: [{}]
  51. }, ThingSchema);
  52.  
  53. doc2.validate(function (result) {
  54. document.getElementById('log2').value = JSON.stringify(result.errors, null, ' ');
  55. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement