Skip to content

Commit

Permalink
Moved verb validation
Browse files Browse the repository at this point in the history
  • Loading branch information
juanhapes committed Jun 14, 2019
1 parent 880d837 commit 701c945
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.2.0] - 2019-06-14
### Changed
- Verb validation moved to validate method

## [1.1.0] - 2019-05-29
### Added
- `Travis` and `Coveralls` integrations
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@janiscommerce/schema-validator",
"version": "1.1.0",
"version": "1.2.0",
"description": "A local schema validator",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 9 additions & 9 deletions schema-validator/schema-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@ class SchemaValidator {
}

constructor(endpoint, verb = 'get') {

this.endpoint = this.constructor.fixEndpoint(endpoint);

if(!this.isValidVerb(verb))
throw new SchemaValidatorError('Invalid verb', SchemaValidatorError.codes.INVALID_VERB);

this.verb = verb.toLowerCase();
this.verb = verb;
}

static cachePaths() {
Expand Down Expand Up @@ -114,6 +109,11 @@ class SchemaValidator {

validate() {

if(!this.isValidVerb())
throw new SchemaValidatorError('Invalid verb', SchemaValidatorError.codes.INVALID_VERB);

this.verb = this.verb.toLowerCase();

if(!this.paths)
this.constructor.cachePaths();

Expand All @@ -126,9 +126,9 @@ class SchemaValidator {
return true;
}

isValidVerb(verb) {
return typeof verb === 'string'
&& this.constructor.verbs.includes(verb.toLowerCase());
isValidVerb() {
return typeof this.verb === 'string'
&& this.constructor.verbs.includes(this.verb.toLowerCase());
}

shouldValidateClient() {
Expand Down
3 changes: 2 additions & 1 deletion tests/schema-validator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ describe('SchemaValidator', function() {

it('should throw when construct with invalid verb', function() {
assert.throws(() => {
new SchemaValidator('/api/foo', 'foo');
const schemaValidator = new SchemaValidator('/api/foo', 'foo');
schemaValidator.validate();
}, {
name: 'SchemaValidatorError',
code: SchemaValidatorError.codes.INVALID_VERB
Expand Down

0 comments on commit 701c945

Please sign in to comment.