Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Last active March 12, 2020 01:18
Show Gist options
  • Select an option

  • Save jebai0521/b3dc6f677cf3d0c5f6b2c7b9d18cae26 to your computer and use it in GitHub Desktop.

Select an option

Save jebai0521/b3dc6f677cf3d0c5f6b2c7b9d18cae26 to your computer and use it in GitHub Desktop.
ajv-email-validate.js
// Map
// Payment
// import Ajv from 'ajv';
const Ajv = require('ajv');
var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
ajv.addFormat('email', '^(([^<>()\\[\\]\\\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$');
let schema = {
type: 'object',
required: ['username', 'email', 'email1'],
properties: {
username: {
type: 'string',
minLength: 4
},
email: {
type: 'string',
format: 'regex',
pattern: '^(([^<>()\\[\\]\\\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$'
},
email1: {
type: 'string',
format: 'email'
}
}
};
const data = {
username : "11111",
email: "[email protected]",
email1: "jebai@waveo"
}
// const p = '^(([^<>()\\[\\]\\\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$';
let validate = ajv.compile(schema);
let valid = validate(data);
if (!valid) console.log(validate.errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment