JavaScript check if a string is valid SSH git URL using check-more-types library

In this JavaScript tutorial we learn how to check whether a given string is a valid SSH git URL or not using the check-more-types library.

Create the sample project

Firstly we create an empty project with the following command.

mkdir sample-project
cd sample-project
npm init -y

And install the check-more-types module using the following command.

npm install check-more-types --save

How to validate SSH git URL in JavaScript

In this section we show you how to use the check-more-types library git() method to check if a given string is a valid SSH git URL or not.

In the sample-project directory create a new file named check-git.js as follows.

check-git.js

var check = require('check-more-types');

console.log(check.git('git@github.com:toricode/toricode.github.io.git'));
console.log(check.git('https://github.com/toricode/toricode.github.io'));
console.log(check.git('test string'));

Run the check-git.js file using the following command.

node check-git.js 
The output is:
true
false
false

More examples of using check.git() method

check-git-example.js

var check = require('check-more-types');
var url = 'git@github.com:toricode/toricode.github.io.git';

if(check.git(url)) {
    console.log('The value is valid SSH git URL');
} else {
    console.log('The value is invalid SSH git URL');
}

Run the check-git-example.js file using the following command.

node check-git-example.js
The output is:
The value is valid SSH git URL

Happy Coding 😊