Skip to content

Commit

Permalink
AB-773: Completed initial validation functions and Validation class i…
Browse files Browse the repository at this point in the history
…mplementation
  • Loading branch information
satrioaccelbyte committed Aug 2, 2019
1 parent bd077cb commit 90e0389
Show file tree
Hide file tree
Showing 60 changed files with 6,274 additions and 309 deletions.
6 changes: 5 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 80
max_line_length = 120
trim_trailing_whitespace = true

[**/Makefile]
indent_size = 2
indent_style = tab

[*.md]
max_line_length = 0
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
.Trash*
src
scripts
test
tsconfig.json
tsconfig.module.json
tslint.json
.idea
.yarn.lock
.travis.yml
.editorconfig
.github
.prettierrc
docker-compose.yaml
Dockerfile
Makefile
.prettierignore
.gitignore
.vscode
config
build/docs
**/*.spec.*
coverage
Expand Down
2 changes: 0 additions & 2 deletions .prettierignore

This file was deleted.

31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Changelog

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).

## [v0.2.0]
### Added
- Added Validation class
- Added validateAlphanumeric
- Added validateComplexPassword
- Added validateCpuSize
- Added validateDisplayName
- Added validateDockerImage
- Added validateEmail
- Added validateLength
- Added validateMemorySize
- Added validateNumeric
- Added validateOrderNumber
- Added validatePath
- Added validatePermissionResource
- Added validateTag
- Added validateTemplateSlug
- Added validateTopic
- Added validateUrl
- Added validateUserDisplayName
- Added validateUuidV4WithoutHyphen

## [v0.1.0]
- Repo set-up
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ build:
$(RUN) $(BUILDER_IMAGE) yarn install
$(RUN) $(BUILDER_IMAGE) yarn build

publish:
$(RUN) $(BUILDER_IMAGE) yarn install
$(RUN) $(BUILDER_IMAGE) yarn build
$(RUN) $(BUILDER_IMAGE) yarn run prepare-release
$(RUN) $(BUILDER_IMAGE) npm publish
run:
$(RUN) -it $(BUILDER_IMAGE) yarn watch

clean:
$(RUN) $(BUILDER_IMAGE) yarn run clean
$(RUN) $(BUILDER_IMAGE) yarn run clean
-$(RUN) --user=root $(BUILDER_IMAGE) rm -rf node_modules
-$(RUN) --user=root $(BUILDER_IMAGE) rm -rf build
-docker rmi -f $(BUILDER_IMAGE) $(SERVICE):$(REVISION_ID)
Expand All @@ -43,3 +40,6 @@ rebuild:
test:
$(RUN) $(BUILDER_IMAGE) yarn test

publish:
echo 'registry=${NEXUS_REPOSITORY_URL}\nemail=${NEXUS_EMAIL}\nalways-auth=true\n_auth=${NEXUS_AUTH}' > .npmrc
npm publish
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ Justice JS Common Utils

## High Level Features

- Common javascript utilities for Justice Platform
- Fully containerized development
Common Javascript utilities for Justice Platform, including:
- Input Validation

## Quick Start
And fully containerized development

## Information About the Common Utility Functions
Each library has it's own `README.md` on it's folder under `/src/lib`. e.g for `input-validation`, the README is in `/src/lib/input-validation/README.md`

## Development Quick Start

run `make build`, to build the project
run `make publish`, to publish the project
run `make run`, to run a test watch and start development

## Usage Quick Start
run `npm install justice-js-common-utils --registry=https://nexus.accelbyte.net/repository/npm-group/` to install in your project

## Build Requirements

Expand All @@ -25,4 +33,3 @@ run `make publish`, to publish the project
- See _Build Requirements_
- Docker Compose
- IDE (we like WebStorm)

38 changes: 38 additions & 0 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) 2018-2019 AccelByte Inc. All Rights Reserved.
# This is licensed software from AccelByte Inc, for limitations
# and restrictions contact your company contract manager.

image: atlassian/default-image:2
clone:
depth: 2

pipelines:
default:
- step:
name: Build
script:
- export NEXUS_AUTH=$NEXUS_AUTH
- export NEXUS_REPOSITORY_URL=$NEXUS_REPOSITORY_URL
- export NEXUS_EMAIL=$NEXUS_EMAIL
- make build
- make test

branches:
master:
- step:
name: Build and publish to npm
script:
- export NEXUS_AUTH=$NEXUS_AUTH
- export NEXUS_REPOSITORY_URL=$NEXUS_REPOSITORY_URL
- export NEXUS_EMAIL=$NEXUS_EMAIL
- make build
- make test
- make publish

options:
docker: true

definitions:
services:
docker:
memory: 3072
1 change: 0 additions & 1 deletion config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const resolveModule = (resolveFn, filePath) => {
module.exports = {
appBuild: resolveApp("dist"),
appPackageJson: resolveApp("package.json"),
appVersionJson: resolveApp("version.json"),
changelog: resolveApp("CHANGELOG.md"),
appSrc: resolveApp("packages"),
appTsConfig: resolveApp("tsconfig.json"),
Expand Down
37 changes: 17 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "@accelbyte/justice-js-common-utils",
"version": "0.1.0",
"private": false,
"name": "justice-js-common-utils",
"version": "0.2.0",
"description": "AccelByte's Justice Common Javascript Utilities",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand All @@ -22,14 +21,13 @@
"test:unit": "NODE_ENV=test jest --config \"config/loadJestConfig.js\" --coverage --verbose --detectOpenHandles --forceExit",
"test:watch": "NODE_ENV=test jest --config \"config/loadJestConfig.js\" --coverage --verbose --watchAll",
"watch": "run-s clean build:main && run-p \"build:main -- -w\" \"test:watch\"",
"doc": "run-s doc:html && open-cli build/docs/index.html",
"doc:html": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --out build/docs",
"doc:json": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --json build/docs/typedoc.json",
"version": "standard-version",
"doc": "run-s doc:html && npx open-cli build/docs/index.html",
"doc:html": "npx typedoc src/ --exclude **/*.test.ts --target ES6 --mode file --out build/docs",
"doc:json": "npx typedoc src/ --exclude **/*.test.ts --target ES6 --mode file --json build/docs/typedoc.json",
"reset": "git clean -dfx && git reset --hard && yarn install",
"clean": "trash build test",
"prepare-release": "run-s reset test doc:html version",
"preinstall": "node -e \"if(process.env.npm_execpath.indexOf('yarn') === -1) throw new Error('test must be installed with Yarn: https://yarnpkg.com/')\""
"preinstall": "node -e \"if(process.env.npm_execpath.indexOf('yarn') === -1) throw new Error('test must be installed with Yarn: https://yarnpkg.com/')\"",
"precommit": "run-s test && node scripts/preCommit"
},
"scripts-info": {
"info": "Display information about the package scripts",
Expand All @@ -39,41 +37,40 @@
"watch": "Watch and rebuild the project on save, then rerun relevant tests",
"doc": "Generate HTML API documentation and open it in a browser",
"doc:json": "Generate API documentation in typedoc JSON format",
"version": "Bump package.json version, update CHANGELOG.md, tag release",
"reset": "Delete all untracked files and reset the repo to the last commit",
"prepare-release": "One-step: clean, build, test, publish docs, and prep a release"
"reset": "Delete all untracked files and reset the repo to the last commit"
},
"engines": {
"node": ">=8.9"
},
"dependencies": {
"sha.js": "^2.4.11"
"validator": "^11.1.0"
},
"devDependencies": {
"@bitjson/npm-scripts-info": "^1.0.0",
"@types/jest": "^24.0.15",
"@types/node": "^12.6.8",
"@types/validator": "^10.11.2",
"husky": "^3.0.1",
"jest": "^24.8.0",
"jest-cli": "^24.8.0",
"cz-conventional-changelog": "^2.1.0",
"npm-run-all": "^4.1.5",
"@bitjson/npm-scripts-info": "^1.0.0",
"object-assign": "^4.1.1",
"raf": "^3.4.1",
"open-cli": "^5.0.0",
"prettier": "^1.18.2",
"raf": "^3.4.1",
"semver-parser": "^3.0.1",
"standard-version": "^6.0.1",
"trash-cli": "^3.0.0",
"ts-jest": "^24.0.2",
"tslint": "^5.18.0",
"tslint-config-prettier": "^1.18.0",
"tslint-immutable": "^6.0.1",
"typedoc": "^0.15.0",
"typescript": "^3.5.3",
"whatwg-fetch": "^3.0.0"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
"husky": {
"hooks": {
"pre-commit": "yarn run precommit"
}
}
}
95 changes: 95 additions & 0 deletions scripts/preCommit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
*
* * Copyright (c) 2018-2019 AccelByte Inc. All Rights Reserved.
* * This is licensed software from AccelByte Inc, for limitations
* * and restrictions contact your company contract manager.
*
*/

const { compareSemVer, isValidSemVer } = require("semver-parser");
const childProcess = require("child_process");
const paths = require("../config/paths");

function printMessage(message) {
console.log(`===PRECOMMIT: ${message}`);
}

function printErrorMessage(message) {
console.log(`===PRECOMMIT: Error: ${message}`);
}

function exitAndAbortCommit(message) {
printErrorMessage(message);
process.exit(1);
}

function isVersionHigher(oldVersion, newVersion) {
try {
const result = compareSemVer(newVersion, oldVersion, false);
if (result < 0) {
exitAndAbortCommit(`Downgrade version is not allowed, version should higher than '${oldVersion}'`);
}
return result;
} catch (e) {
exitAndAbortCommit(e.message);
}
}

function validateDiff(diffString) {
const lines = diffString.split("\n");
let versionBeforeChange = "0.0.0";
let versionAfterChange = "0.0.0";
lines.map((line) => {
// Find specific line that contain "version" changes, the line should be like '+ "version": "1.0.0"' and use regex to extract version number 1.0.0
if (line.startsWith('+ "version":')) versionAfterChange = line.match(/\"(.*?)\"/g)[1].replace(/\"/g, "");
if (line.startsWith('- "version":')) versionBeforeChange = line.match(/\"(.*?)\"/g)[1].replace(/\"/g, "");
});
if (!versionAfterChange) return false;
if (!isValidSemVer(versionAfterChange, false)) {
exitAndAbortCommit(`Invalid version format on '${paths.appPackageJson}'`);
}
return isVersionHigher(versionBeforeChange, versionAfterChange);
}

async function getGitDiff(filePath) {
return await new Promise((resolve, reject) => {
childProcess.exec(`git diff --staged ${filePath}`, (error, stdout, stderr) => {
if (error) {
return reject();
}
return resolve(stdout.toString());
});
});
}

async function validateAppVersion() {
try {
const output = await getGitDiff(paths.appPackageJson);
if (output.length < 1 || !validateDiff(output)) {
exitAndAbortCommit(
`App version on '${paths.appPackageJson}' must be bumped up after doing changes, if you feel like you have bumped it, check if the change has been staged for commit.`
);
}
} catch (error) {
exitAndAbortCommit(`Failed to validate app version. Make sure ${paths.appPackageJson} file exists`);
}
}

async function validateChangelog() {
try {
const changelogPath = paths.changelog;
const output = await getGitDiff(changelogPath);
if (output.length < 1) {
exitAndAbortCommit(
`${changelogPath} must be updated after doing change, if you feel like you have updated it, check if the change has been staged for commit.`
);
}
} catch (error) {
exitAndAbortCommit(`Failed diffing the ${changelogPath}. Make sure the file: ${changelogPath} exists`);
}
}

printMessage("Checking version");
validateAppVersion();
printMessage("Checking changelog");
validateChangelog();
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./lib/input-validation/Validation";
export * from "./lib/input-validation";
Loading

0 comments on commit 90e0389

Please sign in to comment.