Commit 9f4ec77c authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira

Merge branch '2-criar-validationhandler' into 'develop'

Issue #2: Resolve "Criar ValidationHandler"

Closes #2

See merge request !3
parents fa73adc5 22bc3f0b
Pipeline #19325 passed with stages
in 33 seconds
......@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 0.0.3 - 07-02-2019
### Changed
- Added a new type of enum ValitationType #2 (Horstmann)
### Added
- ValidationHandle to valited answer given a input #2 (Horstmann)
## 0.0.2 - 05-02-2019
### Added
- EnunHandler to handle types of inputs #4 (Horstmann)
......
{
"name": "form-creator-api",
"version": "0.0.2",
"version": "0.0.3",
"description": "RESTful API used to manage and answer forms.",
"main": "index.js",
"scripts": {
......
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { InputType, ValidationType } from "../utils/enumHandler";
/** Validation contains the type of it, and n arguments to validate if necessary */
export interface Validation {
/** Enum Validation Type to identify the type of current validation */
type: ValidationType;
/** Array of any, to store informations about validadtion */
arguments: any[];
}
/**
* Input Class to manage project's inputs forms
*/
export class Input {
/** Place where input should be in the form. */
public readonly placement: number;
/** Question of input */
public readonly question: string;
/** Type of input */
public readonly type: InputType;
/** Array contain all input's validation */
public readonly validation: Validation[];
/**
* Creates a new instance of Input Class
* @param placement - Question position in forms.
* @param question - Input question of this input
* @param type - InputType
* @param validation - Array of Validation
*/
constructor(placement: number, question: string, type: InputType, validation: Validation[]) {
this.placement = placement;
this.question = question;
this.type = type;
this.validation = validation;
}
}
......@@ -20,14 +20,15 @@
*/
import { expect } from "chai";
import { EnumHandler, InputType } from "./enumHandler";
import { EnumHandler, InputType, ValidationType } from "./enumHandler";
describe("Enum Handler", () => {
it("should stringfy input ", () => {
it("should stringfy InputType ", () => {
const inputNone = EnumHandler.stringfyInputType(InputType.NONE);
const inputText = EnumHandler.stringfyInputType(InputType.TEXT);
expect(inputNone).to.be.equal("");
expect(inputText).to.be.equal("text");
});
......@@ -37,10 +38,50 @@ describe("Enum Handler", () => {
const inputTextCapitalLetters = EnumHandler.parseInputType("TEXT");
const inputNone = EnumHandler.parseInputType("");
const inputFOOL = EnumHandler.parseInputType("fool");
expect(inputText).to.be.equal(InputType.TEXT);
expect(inputTextCapitalLetters).to.be.equal(InputType.TEXT);
expect(inputNone).to.be.equal(InputType.NONE);
expect(inputFOOL).to.be.equal(InputType.NONE);
});
it("should stringfy ValidationType ", () => {
const validationRegex = EnumHandler.stringfyValidationType(ValidationType.REGEX);
const validationMandatory = EnumHandler.stringfyValidationType(ValidationType.MANDATORY);
const validationMaxChar = EnumHandler.stringfyValidationType(ValidationType.MAXCHAR);
const validationMinChar = EnumHandler.stringfyValidationType(ValidationType.MINCHAR);
const validationNone = EnumHandler.stringfyValidationType(ValidationType.NONE);
expect(validationNone).to.be.equal("");
expect(validationRegex).to.be.equal("regex");
expect(validationMandatory).to.be.equal("mandatory");
expect(validationMaxChar).to.be.equal("maxchar");
expect(validationMinChar).to.be.equal("minchar");
});
it("should parse string to ValidationType", () => {
const validationRegex = EnumHandler.parseValidationType("regex");
const validationRegexCapitalized = EnumHandler.parseValidationType("REGEX");
const validationMandatory = EnumHandler.parseValidationType("mandatory");
const validationMandatoryCapitalized = EnumHandler.parseValidationType("MANDATORY");
const validationMaxChar = EnumHandler.parseValidationType("maxchar");
const validationMaxCharyCapitalized = EnumHandler.parseValidationType("MAXCHAR");
const validationMinChar = EnumHandler.parseValidationType("minchar");
const validationMinCharyCapitalized = EnumHandler.parseValidationType("MINCHAR");
const validationNone = EnumHandler.parseValidationType("");
const validatioFOOL = EnumHandler.parseValidationType("fool");
expect(validationRegex).to.be.equal(ValidationType.REGEX);
expect(validationRegexCapitalized).to.be.equal(ValidationType.REGEX);
expect(validationMandatory).to.be.equal(ValidationType.MANDATORY);
expect(validationMandatoryCapitalized).to.be.equal(ValidationType.MANDATORY);
expect(validationMaxChar).to.be.equal(ValidationType.MAXCHAR);
expect(validationMaxCharyCapitalized).to.be.equal(ValidationType.MAXCHAR);
expect(validationMinChar).to.be.equal(ValidationType.MINCHAR);
expect(validationMinCharyCapitalized).to.be.equal(ValidationType.MINCHAR);
expect(validationNone).to.be.equal(ValidationType.NONE);
expect(validatioFOOL).to.be.equal(ValidationType.NONE);
});
});
......@@ -30,13 +30,29 @@ export enum InputType {
}
/**
* Enum's handler. Manage parse through the project.
* Available Validation types
*/
export enum ValidationType {
/** Used as error code. No suitable validation found. */
NONE,
/** Regex type, when input need a regex to validate */
REGEX,
/** Mandatory type, when input is mandatory */
MANDATORY,
/** MaxChars type, when input has maximum char limit */
MAXCHAR,
/** MinChars type, when input has minimum char limit */
MINCHAR
}
/**
* Enum's handler. Manage parse through the project.
*/
export class EnumHandler {
/**
* Parse an enum(Input type) to string.
* @param a - Input type to be stringified.
* @returns - Input Type as string
*/
public static stringfyInputType(a: InputType): string {
switch (a) {
......@@ -49,6 +65,7 @@ export class EnumHandler {
/**
* Parse a string to enum(InputType).
* @param str - InputType in string format.
* @returns - Matching InputType
*/
public static parseInputType(str: string): InputType {
str = str.toLocaleLowerCase();
......@@ -60,4 +77,44 @@ export class EnumHandler {
}
}
/**
* Parse an enum(Validation type) to string.
* @param a - Validation Type to be stringified.
* @returns - Validation Type as string
*/
public static stringfyValidationType(a: ValidationType): string {
switch (a) {
case ValidationType.REGEX:
return "regex";
case ValidationType.MANDATORY:
return "mandatory";
case ValidationType.MAXCHAR:
return "maxchar";
case ValidationType.MINCHAR:
return "minchar";
default:
return "";
}
}
/**
* Parse a string to enum(InputType).
* @param str - InputType in string format.
* @returns - Matching ValidationType
*/
public static parseValidationType(str: string): ValidationType {
str = str.toLocaleLowerCase();
switch (str) {
case "mandatory":
return ValidationType.MANDATORY;
case "regex":
return ValidationType.REGEX;
case "maxchar":
return ValidationType.MAXCHAR;
case "minchar":
return ValidationType.MINCHAR;
default:
return ValidationType.NONE;
}
}
}
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { expect } from "chai";
import { Input, Validation } from "../core/input";
import { EnumHandler, InputType, ValidationType } from "./enumHandler";
import {ValidationHandler } from "./validationHandler";
describe("Validation Handler", () => {
it("should test when Input is mandatory", () => {
const validation: Validation[] = [{type: ValidationType.MANDATORY, arguments: []}];
const input = new Input (1, "Question", InputType.TEXT, validation);
expect(ValidationHandler.validateInput(input, "FOOL")).to.be.true;
expect(ValidationHandler.validateInput(input, "")).to.be.false;
expect(ValidationHandler.validateInput(input, null)).to.be.false;
expect(ValidationHandler.validateInput(input, undefined)).to.be.false;
});
it("should test when Input has a regex", () => {
const validation: Validation[] = [{type: ValidationType.REGEX, arguments: ["\\d{5}-\\d{3}"]}];
const input = new Input (1, "Question", InputType.TEXT, validation);
expect(ValidationHandler.validateInput(input, "88888-888")).to.be.true;
expect(ValidationHandler.validateInput(input, "88888-88")).to.be.false;
expect(ValidationHandler.validateInput(input, "")).to.be.false;
expect(ValidationHandler.validateInput(input, null)).to.be.false;
expect(ValidationHandler.validateInput(input, undefined)).to.be.false;
});
it("should test when Input has a minimum char number", () => {
const validation: Validation[] = [{type: ValidationType.MINCHAR, arguments: [5]}];
const input = new Input (1, "Question", InputType.TEXT, validation);
expect(ValidationHandler.validateInput(input, "12345")).to.be.true;
expect(ValidationHandler.validateInput(input, "123456")).to.be.true;
expect(ValidationHandler.validateInput(input, "1234")).to.be.false;
expect(ValidationHandler.validateInput(input, "")).to.be.false;
expect(ValidationHandler.validateInput(input, null)).to.be.false;
expect(ValidationHandler.validateInput(input, undefined)).to.be.false;
});
it("should test when Input has a maximum char number", () => {
const validation: Validation[] = [{type: ValidationType.MAXCHAR, arguments: [5]}];
const input = new Input (1, "Question", InputType.TEXT, validation);
expect(ValidationHandler.validateInput(input, "1234")).to.be.true;
expect(ValidationHandler.validateInput(input, "12345")).to.be.true;
expect(ValidationHandler.validateInput(input, "")).to.be.true;
expect(ValidationHandler.validateInput(input, "123456")).to.be.false;
expect(ValidationHandler.validateInput(input, null)).to.be.false;
expect(ValidationHandler.validateInput(input, undefined)).to.be.false;
});
});
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Input } from "../core/input";
import { ValidationType } from "./enumHandler";
/**
* Validation's handler. Manage parse validation through the project.
*/
export class ValidationHandler {
/**
* Validate a string according given a regex.
* @param answer - Answer to be validated.
* @param regex - Regex to validate answer.
* @returns - true if answer match regex, else false.
*/
private static validateByRegex(answer: string, regex: string): boolean{
const regexp = new RegExp(regex);
return regexp.test(answer);
}
/**
* Validate if is null, undefined nor ""
* @param answer - answer to be validated.
* @returns - true if not null, "" nor undefined, else false.
*/
private static validateMandatory(answer: string): boolean{
return ((!answer) === false);
}
/**
* Validate if answer has minimum number of chars
* @param answer - Answer to be validated.
* @param size - Minimum size that answer should have.
* @returns - true if has at least Size chars, else false.
*/
private static validateMinChar(answer: string, size: number): boolean{
return (answer !== null && answer !== undefined && size <= answer.length);
}
/**
* Validate if answer has minimum number of chars
* @param answer - Answer to be validated.
* @param size - Maximum size that answer should have.
* @returns - true if has at max Size chars, else false.
*/
private static validateMaxChar(answer: string, size: number): boolean{
return (answer !== null && answer !== undefined && size >= answer.length);
}
/**
* Validate if answer has minimum number of chars
* @param input - Input to validate answer.
* @param answer - Answer of input
* @returns - true if answer is valid according a given Input.
*/
public static validateInput(input: Input, answer: string): boolean{
let result: boolean = true;
for ( const validation of input.validation){
switch (validation.type) {
case ValidationType.REGEX:
result = result && this.validateByRegex(answer, validation.arguments[0]);
break;
case ValidationType.MANDATORY:
result = result && this.validateMandatory(answer);
break;
case ValidationType.MAXCHAR:
result = result && this.validateMaxChar(answer, validation.arguments[0]);
break;
case ValidationType.MINCHAR:
result = result && this.validateMinChar(answer, validation.arguments[0]);
break;
}
}
return result;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment