forked from AccelByte/justice-js-common-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AB-773: Completed initial validation functions and Validation class i…
…mplementation
- Loading branch information
1 parent
bd077cb
commit 90e0389
Showing
60 changed files
with
6,274 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from "./lib/input-validation/Validation"; | ||
export * from "./lib/input-validation"; |
Oops, something went wrong.