mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-19 04:16:08 +00:00
Init commit with domain validation tests
This commit is contained in:
commit
5e20f5ea74
6 files changed
+3665
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
node_modules/
|
||||||
|
*.log
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"forceHttps": true,
|
||||||
|
"record": {
|
||||||
|
"CNAME": "phenax.github.io"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "is-a-dev-core",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Register *.is-a.dev domains for free",
|
||||||
|
"scripts": {
|
||||||
|
"test": "jest"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/is-a.dev/register-domain"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"__"
|
||||||
|
],
|
||||||
|
"author": "Akshay Nair <phenax5@gmail.com>",
|
||||||
|
"license": "GPL-3.0",
|
||||||
|
"dependencies": {
|
||||||
|
"jest": "^26.4.2",
|
||||||
|
"ramda": "^0.27.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
const R = require('ramda');
|
||||||
|
const { getDomains, validateDomainData } = require('../utils/domain');
|
||||||
|
|
||||||
|
describe('Domains', () => {
|
||||||
|
describe('but first... a test for this test', () => {
|
||||||
|
describe('getDomains', () => {
|
||||||
|
it('should resolve with the list of domains', async () => {
|
||||||
|
const list = await getDomains();
|
||||||
|
expect(Array.isArray(list)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('isValidDomainData', () => {
|
||||||
|
it('should return true for a valid object', () => {
|
||||||
|
const { valid, errors } = validateDomainData({
|
||||||
|
forceHttps: true,
|
||||||
|
record: { CNAME: ['hello.com'] },
|
||||||
|
});
|
||||||
|
expect(valid).toBe(true);
|
||||||
|
expect(errors).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
xit('should have a the correct keys', async () => {
|
||||||
|
const list = await getDomains();
|
||||||
|
list.forEach(data => {
|
||||||
|
const { errors } = validateDomainData(data);
|
||||||
|
if (errors.length) {
|
||||||
|
console.log(errors);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const R = require('ramda');
|
||||||
|
|
||||||
|
const DOMAINS_PATH = path.resolve('domains');
|
||||||
|
|
||||||
|
const toDomain = str => path.join(DOMAINS_PATH, str);
|
||||||
|
|
||||||
|
const toDomainData = R.compose(require, toDomain);
|
||||||
|
|
||||||
|
const getDomains = () =>
|
||||||
|
fs.promises.readdir(DOMAINS_PATH, {})
|
||||||
|
.then(R.map(name => ({
|
||||||
|
...toDomainData(name),
|
||||||
|
name: name.replace(/\.json$/, ''),
|
||||||
|
})));
|
||||||
|
|
||||||
|
const hasLengthLessThan = len => R.compose(R.not, R.lt(len), R.length);
|
||||||
|
|
||||||
|
const validate = pattern => data => R.compose(
|
||||||
|
invalidPairs => invalidPairs.length ? { errors: invalidPairs, valid: false } : { errors: [], valid: true },
|
||||||
|
R.filter(([key, { fn }]) => fn ? !fn(data[key]) : false),
|
||||||
|
R.toPairs,
|
||||||
|
)(pattern);
|
||||||
|
|
||||||
|
const validateDomainData = validate({
|
||||||
|
//name: {
|
||||||
|
//reason: 'The name of the file is invalid',
|
||||||
|
//fn: R.match(/[A-Za-z0-9]{3,}/g),
|
||||||
|
//},
|
||||||
|
description: {
|
||||||
|
reason: 'Description has to be shorter than 100 characters',
|
||||||
|
fn: R.anyPass([
|
||||||
|
R.empty,
|
||||||
|
hasLengthLessThan(100),
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
forceHttps: {
|
||||||
|
reason: 'forceHttp is required to be true or false',
|
||||||
|
fn: R.is(Boolean),
|
||||||
|
},
|
||||||
|
record: {
|
||||||
|
reason: 'Invalid record',
|
||||||
|
fn: R.allPass([
|
||||||
|
R.is(Object),
|
||||||
|
R.anyPass([
|
||||||
|
R.propSatisfies(R.is(Array), 'CNAME'),
|
||||||
|
R.propSatisfies(R.is(Array), 'A'),
|
||||||
|
]),
|
||||||
|
R.compose(hasLengthLessThan(1), R.keys),
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = { getDomains, validateDomainData };
|
||||||
Reference in new issue
Block a user