如何在保存期间验证mongoose模式中所有必填字段中的某些字段?

时间:2016-03-10 04:08:44

标签: node.js mongoose

如何在保存或更新我的代码期间验证mongoose模式中所有必填字段中的某些字段是这样的......

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import java.util.Scanner;
import java.io.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;
import javax.swing.text.JTextComponent;
import java.net.URI;
import java.util.*;

public class MyTextEditor extends JFrame implements ActionListener
{
  private JPanel panel = new JPanel(new BorderLayout());
  private JTextArea textArea = new JTextArea(0,0);
  private static final Color TA_BKGRD_CL = Color.BLACK;
  private static final Color TA_FRGRD_CL = Color.GREEN;
  private static final Color TA_CARET_CL = Color.WHITE;

  private JScrollPane scrollPane;
  private MenuBar menuBar = new MenuBar();


 public MyTextEditor()
 {

  this.setSize(750,800);
  this.setTitle("Zenith");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  textArea.setFont(new Font("Consolas", Font.BOLD, 14));
  textArea.setForeground(TA_FRGRD_CL);
  textArea.setBackground(TA_BKGRD_CL);
  textArea.setCaretColor(TA_CARET_CL);
  scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  scrollPane.setVisible(true);
  textArea.add(scrollPane,BorderLayout.EAST);
  final LineNumberingTextArea lineNTA = new LineNumberingTextArea(textArea);
  DocumentListener documentListen = new DocumentListener()
  {
     public void insertUpdate(DocumentEvent documentEvent)
     {
        lineNTA.updateLineNumbers();  
     }
     public void removeUpdate(DocumentEvent documentEvent)
     {
        lineNTA.updateLineNumbers();
     }
     public void changedUpdate(DocumentEvent documentEvent)
     {
        lineNTA.updateLineNumbers();
     }
  };
  textArea.getDocument().addDocumentListener(documentListen);
  // Line numbers
  lineNTA.setBackground(Color.BLACK);
  lineNTA.setForeground(Color.WHITE);
  lineNTA.setFont(new Font("Consolas", Font.BOLD, 13));
  lineNTA.setEditable(false);
  lineNTA.setVisible(true);
  textArea.add(lineNTA);
  scrollPane.setVisible(true);
  scrollPane.add(textArea);

  getContentPane().add(scrollPane);

 }


public void findKeyWords(String ext)
{
  ArrayList<String> wordsInTA = new ArrayList<String>();
  int index = 0;

  if(ext == "java")
  {
     for(String line : textArea.getText().split(" "))
     {
        wordsInTA.add(line);
        index++;
     }
     try
     {

      while(index>0)
      {
           String temp = wordsInTA.get(index);
           boolean isKeyWord = binarySearch(temp);
           if(isKeyWord)
           {
             //Code that has not yet been made
           } 
           index--;    
     }
   }
   catch(IOException ex)
     {
        ex.printStackTrace();
     }
  }

}
private ArrayList<String> loadJavaWords() throws FileNotFoundException
{
  ArrayList<String> javaWords = new ArrayList<String>();
  Scanner scan = new Scanner(new File("JavaKeyWords.txt"));
  while(scan.hasNext())
  {
     javaWords.add(scan.next());
  }
  scan.close();
  return javaWords;
}
private boolean binarySearch(String word) throws FileNotFoundException
{
  ArrayList<String> javaWords = loadJavaWords();
  int min = 0;
  int max = javaWords.size()-1;
  while(min <= max)
  {
     int index = (max + min)/2;
     String guess = javaWords.get(index);
     int result = word.compareTo(guess);
     if(result == 0)
     {
        return true;
     }
     else if(result > 0)
     {
        min = index +1;
     }
     else if(result < 0)
     {
        max = index -1;
     }
  }
  return false;
}

public void replace()
{
  String wordToSearch = JOptionPane.showInputDialog(null, "Word to replace:");
  String wordToReplace = JOptionPane.showInputDialog(null, "Replacement word:");

  int m;
  int total = 0;
  int wordLength = wordToSearch.length();
  for (String line : textArea.getText().split("\\n")) 
  {
     m = line.indexOf(wordToSearch);
     if(m == -1)
     {
        total += line.length() + 1; 
        continue;
     }

     String newLine = line.replaceAll(wordToSearch, wordToReplace);
     textArea.replaceRange(newLine, total, total + line.length());
     total += newLine.length() + 1;
  }
}

public static void main(String args[])
{
  MyTextEditor textEditor = new MyTextEditor();
  textEditor.setVisible(true);
}

}); module.exports = mongoose.model('User',userSchema);

我想只保存下面的字段。其他人将在更新资料中保存。

var userSchema = new Schema({
first_name      : { type: String, required: 'First name is required!'},
last_name       : { type: String, required: 'Last name is required!'},
user_name       : { type: String, required: 'Product cost is required!', unique: true},
email           : { type: String, required: 'E-mail cost is required!', unique: true},
password        : { type: String, required: 'Password is required!'},
country         : { type: String, required: 'country is required!'}, 
address         : { type: String, required: 'address is required!'},
phone           : { type: Number, required: 'Phone number is required!'},
profile_image   : { type: String },
create_date     : { type: Date }

它会返回我在创建过程中不想保存的所有必填字段的错误,但这些字段也是配置文件更新部分的必填字段。

0 个答案:

没有答案