Skip to content

Commit

Permalink
feat(generate): basic zine structure (#5)
Browse files Browse the repository at this point in the history
* Template stuff

* Copied template

* init

* Copy

* Remove validate

* Provide rankRules

* Actual function

* Remove cache dependency

* Use versions

* Basic controller

* Basic rank-score test

* Add tests

* Test empty items

* Update package json

* Args for testing

* Ignore test results

* Fixed formatting

* Test no rankRules

* Common libraries

* Error handler

* common files

* Include assert

* Fixed XO

* Fixed etst-results name

* Moved validation error to common

* Validate function

* Use common

* Fixed xo

* Moved validate.js to common

* Tested some basic things

* Test boolean

* Test all Booleans

* Added integers

* Decimal

* Boolean type

* Validated arrays

* Version Object

* TEst version

* Replaced with common

* Make it incomplete

* Fixed XO

* Validate int-test Dockerfile

* test empty rank

* Test single object

* Place identifier in output

* Support providing an identifier field

* Test numbers prefer lower

* Test for version

* Finish testing

* sonar scan

* Renamed to match service

* Fixed imports

* Dummy code for now

* Renamed file

* Basic routing

* make it simpler

* Fixed path

* Fixed correct order of error handling

* Renamed to apiDocs

* Renamed to match file

* Renamed
  • Loading branch information
superflyxxi authored Nov 2, 2021
1 parent 9e06b4d commit 08b51c0
Show file tree
Hide file tree
Showing 23 changed files with 969 additions and 137 deletions.
5 changes: 2 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ jobs:
working_directory: ./<< parameters.project >>
environment:
MOCHA_FILE: ./test-results.xml
MOCHA_ARGS: --forbid-pending --forbid-only --reporter mocha-junit-reporter
command: |
cp -v ../.c8rc.json ../.mocharc.json ./
npm test
npm test -- --forbid-pending --forbid-only --reporter mocha-junit-reporter
- run:
name: Tar coverage
command: tar -vcf << parameters.project >>-coverage.tar << parameters.project >>/coverage/
Expand Down Expand Up @@ -337,7 +336,7 @@ workflows:
- docker/hadolint:
name: validate-dockerfiles
context: default
dockerfiles: "common/Dockerfile:zine-generator/Dockerfile:template/Dockerfile"
dockerfiles: "common/Dockerfile:int-test/Dockerfile:template/Dockerfile:zine-generator/Dockerfile"
- build-docker:
context: default
project: common
Expand Down
34 changes: 12 additions & 22 deletions common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"mocha-junit-reporter": "^2.0.2"
},
"dependencies": {
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"validate.js": "^0.13.1"
}
}
27 changes: 26 additions & 1 deletion common/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import process from 'node:process';
import validatejs from 'validate.js';
import {v4 as uuidv4} from 'uuid';

export function errorHandler(error, req, res, next) {
Expand Down Expand Up @@ -41,4 +42,28 @@ export class RouteNotFoundError extends NotFoundError {
}
}

// Export default {errorHandler, RootError, NotFoundError, RouteNotFoundError};
export class ValidationError extends RootError {
constructor(result) {
super('/errors/VALIDATION_ERROR', 'Validation Error', 400, result);
}
}

export function validate(object, constraints) {
const result = validatejs(object, constraints);
if (result) {
throw new ValidationError(result);
}
}

export function getVersionObject(string) {
if (string) {
const splt = string.split('.');
return {
major: splt[0] ? Number.parseInt(splt[0], 10) : undefined,
minor: splt[1] ? Number.parseInt(splt[1], 10) : undefined,
patch: splt[2] ? Number.parseInt(splt[2], 10) : undefined,
};
}

return {};
}
249 changes: 249 additions & 0 deletions common/test/validation-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
import chai from 'chai';
import {validate, ValidationError} from '../src/index.js';

const {expect} = chai;

const everything = {s: 'a', b: true, i: 1, d: 1.1, a: [1, 2, 3], o: {name: 'Object'}};

describe('Basic Validation', function () {
it('Empty rules', function () {
validate(everything, {});
});

it('More attributes than expected', function () {
validate(everything, {
z: {presence: false, type: 'string'},
});
});

it('Required attributes', function () {
validate(everything, {
s: {presence: true},
});
});

describe('Sting Validations', function () {
it('String', function () {
expect(function () {
validate(everything, {
s: {presence: false, type: 'string'},
});
}).to.not.throw(ValidationError);
});
it('Bool', function () {
expect(function () {
validate(everything, {
b: {presence: false, type: 'string'},
});
}).to.throw(ValidationError);
});
it('Integer', function () {
expect(function () {
validate(everything, {
i: {presence: false, type: 'string'},
});
}).to.throw(ValidationError);
});
it('Decimal', function () {
expect(function () {
validate(everything, {
d: {presence: false, type: 'string'},
});
}).to.throw(ValidationError);
});
it('Array', function () {
expect(function () {
validate(everything, {
a: {presence: false, type: 'string'},
});
}).to.throw(ValidationError);
});
it('Object', function () {
expect(function () {
validate(everything, {
o: {presence: false, type: 'string'},
});
}).to.throw(ValidationError);
});
});

describe('Boolean Validations', function () {
it('String', function () {
expect(function () {
validate(everything, {
s: {presence: false, type: 'boolean'},
});
}).to.throw(ValidationError);
});
it('Bool', function () {
expect(function () {
validate(everything, {
b: {presence: false, type: 'boolean'},
});
}).to.not.throw(ValidationError);
});
it('Integer', function () {
expect(function () {
validate(everything, {
i: {presence: false, type: 'boolean'},
});
}).to.throw(ValidationError);
});
it('Decimal', function () {
expect(function () {
validate(everything, {
d: {presence: false, type: 'boolean'},
});
}).to.throw(ValidationError);
});
it('Array', function () {
expect(function () {
validate(everything, {
a: {presence: false, type: 'boolean'},
});
}).to.throw(ValidationError);
});
it('Object', function () {
expect(function () {
validate(everything, {
o: {presence: false, type: 'boolean'},
});
}).to.throw(ValidationError);
});
});

describe('Integer Validations', function () {
it('String', function () {
expect(function () {
validate(everything, {
s: {presence: false, type: 'integer'},
});
}).to.throw(ValidationError);
});
it('Bool', function () {
expect(function () {
validate(everything, {
b: {presence: false, type: 'integer'},
});
}).to.throw(ValidationError);
});
it('Integer', function () {
expect(function () {
validate(everything, {
i: {presence: false, type: 'integer'},
});
}).to.not.throw(ValidationError);
});
it('Decimal', function () {
expect(function () {
validate(everything, {
d: {presence: false, type: 'integer'},
});
}).to.throw(ValidationError);
});
it('Array', function () {
expect(function () {
validate(everything, {
a: {presence: false, type: 'integer'},
});
}).to.throw(ValidationError);
});
it('Object', function () {
expect(function () {
validate(everything, {
o: {presence: false, type: 'integer'},
});
}).to.throw(ValidationError);
});
});

describe('Decimal Validations', function () {
it('String', function () {
expect(function () {
validate(everything, {
s: {presence: false, type: 'number'},
});
}).to.throw(ValidationError);
});
it('Bool', function () {
expect(function () {
validate(everything, {
b: {presence: false, type: 'number'},
});
}).to.throw(ValidationError);
});
it('Integer', function () {
expect(function () {
validate(everything, {
i: {presence: false, type: 'number'},
});
}).to.not.throw(ValidationError);
});
it('Decimal', function () {
expect(function () {
validate(everything, {
d: {presence: false, type: 'number'},
});
}).to.not.throw(ValidationError);
});
it('Array', function () {
expect(function () {
validate(everything, {
a: {presence: false, type: 'number'},
});
}).to.throw(ValidationError);
});
it('Object', function () {
expect(function () {
validate(everything, {
o: {presence: false, type: 'number'},
});
}).to.throw(ValidationError);
});
});

describe('Arrray Validations', function () {
it('String', function () {
expect(function () {
validate(everything, {
s: {presence: false, type: 'array'},
});
}).to.throw(ValidationError);
});
it('Bool', function () {
expect(function () {
validate(everything, {
b: {presence: false, type: 'array'},
});
}).to.throw(ValidationError);
});
it('Integer', function () {
expect(function () {
validate(everything, {
i: {presence: false, type: 'array'},
});
}).to.throw(ValidationError);
});
it('Decimal', function () {
expect(function () {
validate(everything, {
d: {presence: false, type: 'array'},
});
}).to.throw(ValidationError);
});
it('Array', function () {
expect(function () {
validate(everything, {
a: {presence: false, type: 'array'},
});
}).to.not.throw(ValidationError);
});
it('Object', function () {
expect(function () {
validate(everything, {
o: {presence: false, type: 'array'},
});
}).to.throw(ValidationError);
});
});
});
Loading

0 comments on commit 08b51c0

Please sign in to comment.