Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 250 #251

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Click on the desired API to see usage and more information
* [Templates](/docs/resources/templates.md) - `client.templates` ([examples](/examples/templates))
* [Transmissions](/docs/resources/transmissions.md) - `client.transmissions` ([examples](/examples/transmissions))
* [Webhooks](/docs/resources/webhooks.md) - `client.webhooks` ([examples](/examples/webhooks))
* [Recipient Validations](/docs/resources/recipientValidation.md) - `client.recipientValidation` ([examples](/examples/recipientValidation))


## Development
Expand Down
15 changes: 15 additions & 0 deletions docs/resources/recipientValidation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Recipient Validation

This library provides easy access to the [Recipient Validation](https://developers.sparkpost.com/api/recipient-validation/) Resource.

*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).*

## Methods
* **validate(email)**<br />
Validation of email address provided.
* `email` - the email address that you want to validate **required**

## Examples

Visit our examples section to see all of [our recipient validation examples](/examples/recipientValidation).

27 changes: 27 additions & 0 deletions examples/recipientValidation/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

// Promise
client.recipientValidation.validate('[email protected]')
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Callback
client.recipientValidation.validate('[email protected]', function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});
25 changes: 25 additions & 0 deletions lib/recipientValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const api = 'recipient-validation';

module.exports = function(client) {
return {
/**
* To validate a given email address
*
* @param {string} address - the email address of the recipient that requires validation
* @param {RequestCb} [callback]
* @returns {Promise}
*/
validate: function(address, callback) {
if (!address || typeof address !== 'string') {
return client.reject(new Error('Email Address is required'), callback);
}

const options = {
uri: `${api}/single/${address}`
};
return client.get(options, callback);
}
};
};
1 change: 1 addition & 0 deletions lib/sparkpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const SparkPost = function(apiKey, options) {
this.templates = require('./templates')(this);
this.transmissions = require('./transmissions')(this);
this.webhooks = require('./webhooks')(this);
this.recipientValidation = require('./recipientValidation')(this);
};

SparkPost.prototype.request = function(options, callback) {
Expand Down
45 changes: 45 additions & 0 deletions test/spec/recipientValidation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

var _ = require('lodash')
, chai = require('chai')
, expect = chai.expect
, SparkPost = require('../../lib/sparkpost')
, sinon = require('sinon');

require('sinon-as-promised');

chai.use(require('sinon-chai'));
chai.use(require('chai-as-promised'));

describe('Recipient Validation Library', function() {
var client, recipientValidation, callback;

beforeEach(function() {
client = {
get: sinon.stub().resolves({}),
post: sinon.stub().resolves({}),
put: sinon.stub().resolves({}),
delete: sinon.stub().resolves({}),
reject: SparkPost.prototype.reject
};

callback = function() {};

recipientValidation = require('../../lib/recipientValidation')(client);
});

describe('validate', function() {
it('should call client get method with the appropriate uri', function() {
return recipientValidation.validate('[email protected]', callback)
.then(function() {
expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'recipient-validation/single/[email protected]'});
expect(client.get.firstCall.args[1]).to.equal(callback);
});
});

it('should throw an error if domain is missing', function() {
return expect(recipientValidation.validate()).to.be.rejectedWith('Email Address is required');
});
});

});