Skip to content

Commit

Permalink
Complexity fixes
Browse files Browse the repository at this point in the history
* Complexity fixes

* Added badge and attempted to fix a nesting issue
  • Loading branch information
bvego authored Jun 29, 2019
1 parent a2f2a12 commit 09d76ee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Nope 🙅

[![codebeat badge](https://codebeat.co/badges/ee202bf2-693c-4318-9425-e2c0f1a48a1f)](https://codebeat.co/projects/github-com-bvego-nope-validator-master)
[![CircleCI](https://circleci.com/gh/bvego/nope-validator.svg?style=svg)](https://circleci.com/gh/bvego/nope-validator)
[![Fast](https://badgen.now.sh/badge/speed/really%20fast/green)](https://npm.im/nope-validator)
[![Version](https://img.shields.io/npm/v/nope-validator.svg)](https://npm.im/nope-validator)
Expand Down
26 changes: 16 additions & 10 deletions src/NopePrimitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@ abstract class NopePrimitive<T> implements IValidatable<T> {
protected validationRules: Array<Rule<T>> = [];

public required(message = 'This field is required') {
this.validationRules.push(entry => {
const rule: Rule<T> = entry => {
if (entry === undefined || entry === null) {
return message;
}
});
};

this.validationRules.push(rule);

return this;
}

public oneOf(options: Array<T | NopeReference | Nil>, message = 'Invalid option') {
this.validationRules.push((entry, context) => {
const resolvedOptions = options.map(option => {
if (option instanceof NopeReference && context) {
return context[option.key];
}
const resolveNopeRef = (option: T | NopeReference | Nil, context?: { [key: string]: any }) => {
if (option instanceof NopeReference && context) {
return context[option.key];
}

return option;
});
return option;
};

const rule: Rule<T> = (entry, context) => {
const resolvedOptions = options.map(option => resolveNopeRef(option, context));

if (resolvedOptions.indexOf(entry) === -1) {
return message;
}
});
};

this.validationRules.push(rule);

return this;
}
Expand Down
22 changes: 7 additions & 15 deletions src/NopeString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Rule } from './types';
import { urlRegex, emailRegex } from './consts';

class NopeString extends NopePrimitive<string> {
public url(message = 'Input is not a valid url') {
public regex(regex: RegExp, message = 'Doesn\'t satisfy the rule') {
const rule: Rule<string> = entry => {
if (entry === undefined || entry === null) {
return;
}

if (!urlRegex.test(entry)) {
if (!regex.test(entry)) {
return message;
}
};
Expand All @@ -19,20 +19,12 @@ class NopeString extends NopePrimitive<string> {
return this;
}

public email(message = 'Input is not a valid email') {
const rule: Rule<string> = entry => {
if (entry === undefined || entry === null) {
return;
}

if (!emailRegex.test(entry)) {
return message;
}
};

this.validationRules.push(rule);
public url(message = 'Input is not a valid url') {
return this.regex(urlRegex, message);
}

return this;
public email(message = 'Input is not a valid email') {
return this.regex(emailRegex, message);
}

public min(length: number, message = 'Input is too short') {
Expand Down

0 comments on commit 09d76ee

Please sign in to comment.