Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

model validation

const userSchema = new Schema({
  phone: {
    type: String,
    validate: {
      validator: function(v) {
        return /d{3}-d{3}-d{4}/.test(v);
      },
      message: props => `${props.value} is not a valid phone number!`
    },
    required: [true, 'User phone number required']
  }
});

const User = db.model('user', userSchema);
const user = new User();
let error;

user.phone = '555.0123';
error = user.validateSync();
assert.equal(error.errors['phone'].message,
  '555.0123 is not a valid phone number!');

user.phone = '';
error = user.validateSync();
assert.equal(error.errors['phone'].message,
  'User phone number required');

user.phone = '201-555-0123';
// Validation succeeds! Phone number is defined
// and fits `DDD-DDD-DDDD`
error = user.validateSync();
assert.equal(error, null);
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to get array from number length 
Javascript :: get form data as object jquery 
Javascript :: javascript new date dd/mm/yyyy 
Javascript :: Error: Unable to resolve module ./index from 
Javascript :: http to https express js 
Javascript :: split string into int array javascript 
Javascript :: $(document).ready(function() alert 
Javascript :: js set canvas size 
Javascript :: bootstrap datepicker options 
Javascript :: $lookup in mongodb 
Javascript :: javascript auto hide navbar 
Javascript :: add query parameter to url react router v6 
Javascript :: js password generator 
Javascript :: unfocus javascript 
Javascript :: how to add new key value to json object in javascript 
Javascript :: urlencoded limit 
Javascript :: set timeout 
Javascript :: convert string to set in js 
Javascript :: server.js 
Javascript :: view my password jquery 
Javascript :: wordpress not loading jquery 
Javascript :: javascript remove first character from array list 
Javascript :: javascript getting input from console 
Javascript :: count the number of elements in an array javascript 
Javascript :: change root color js 
Javascript :: updateone mongoose example 
Javascript :: Map in Javascript in LWC 
Javascript :: find specific word string js 
Javascript :: split 2 arrays javascript 
Javascript :: referenceerror document is not defined node js 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =