mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-08-07 02:25:03 +00:00
validations: adds restriction for mx with cname
This commit is contained in:
@@ -53,7 +53,9 @@ describe('validateDomainData', () => {
|
||||
{ ...defaultDomain, record: { CNAME: 'https://foobar.com' } },
|
||||
{ ...defaultDomain, record: { URL: 'foobar.com' } },
|
||||
{ ...defaultDomain, record: { CNAME: 'foobar.com', A: ['11.22.22.33'] } },
|
||||
{ ...defaultDomain, record: { CNAME: 'foobar.com', MX: ['ALT4.ASPMX.L.GOOGLE.COM'] } },
|
||||
...INVALID_NAMES.map(name => ({ ...defaultDomain, name })).slice(0, 1),
|
||||
{ ...defaultDomain, record: { TXT: ['foobar wow nice!!!'] } },
|
||||
];
|
||||
|
||||
const validCases = [
|
||||
@@ -69,7 +71,9 @@ describe('validateDomainData', () => {
|
||||
{ ...defaultDomain, record: { CNAME: 'aa.sd' } },
|
||||
{ ...defaultDomain, record: { URL: 'https://foobar.com' } },
|
||||
{ ...defaultDomain, record: { URL: 'http://foobar.com/foobar/' } },
|
||||
{ ...defaultDomain, record: { CNAME: 'foobar.com', MX: ['ALT4.ASPMX.L.GOOGLE.COM'] } },
|
||||
{ ...defaultDomain, record: { MX: ['ALT4.ASPMX.L.GOOGLE.COM'] } },
|
||||
{ ...defaultDomain, record: { TXT: 'foobar wow nice!!!' } },
|
||||
{ ...defaultDomain, record: { A: ['1.1.1.1'], MX: ['mx1.example.com'] } },
|
||||
];
|
||||
|
||||
it('should return false for invalid data', () => {
|
||||
|
||||
+15
-17
@@ -3,35 +3,32 @@ const { VALID_RECORD_TYPES } = require('./constants');
|
||||
const { or, and, validate, between, testRegex, withLengthEq, withLengthGte } = require('./helpers');
|
||||
const INVALID_NAMES = require('./invalid-domains.json');
|
||||
|
||||
const isValidURL = testRegex(/^https?:\/\//ig);
|
||||
const isValidURL = and([R.is(String), testRegex(/^https?:\/\//ig)]);
|
||||
|
||||
const isValidDomain = testRegex(/^(([a-z0-9\-]+)\.)+[a-z]+$/ig)
|
||||
|
||||
// TODO: Add priority to records
|
||||
const isValidDomain = and([R.is(String), testRegex(/^(([a-z0-9\-]+)\.)+[a-z]+$/ig)]);
|
||||
|
||||
const allowMXRecord = R.compose(
|
||||
R.ifElse(R.includes('MX'), withLengthEq(2), withLengthEq(1)),
|
||||
R.keys,
|
||||
);
|
||||
|
||||
const validateCnameRecord = key => and([
|
||||
R.propSatisfies(R.is(String), key),
|
||||
allowMXRecord,
|
||||
R.propSatisfies(withLengthGte(4), key),
|
||||
R.propSatisfies(isValidDomain, key),
|
||||
//R.propSatisfies(R.complement(isValidURL), key),
|
||||
const validateCnameRecord = type => and([
|
||||
R.propIs(String, type),
|
||||
R.compose(withLengthEq(1), R.keys), // CNAME cannot be used with any other record
|
||||
R.propSatisfies(withLengthGte(4), type),
|
||||
R.propSatisfies(isValidDomain, type),
|
||||
]);
|
||||
|
||||
const validateARecord = key => and([
|
||||
const validateARecord = type => and([
|
||||
R.propIs(Array, type),
|
||||
allowMXRecord,
|
||||
R.propSatisfies(withLengthGte(1), key),
|
||||
R.propIs(Array, key),
|
||||
R.propSatisfies(withLengthGte(1), type),
|
||||
]);
|
||||
|
||||
const validateMXRecord = key => and([
|
||||
R.propSatisfies(withLengthGte(1), key),
|
||||
R.propSatisfies(R.all(isValidDomain), key),
|
||||
R.propIs(Array, key),
|
||||
const validateMXRecord = type => and([
|
||||
R.propIs(Array, type),
|
||||
R.propSatisfies(withLengthGte(1), type),
|
||||
R.propSatisfies(R.all(isValidDomain), type),
|
||||
]);
|
||||
|
||||
const validateDomainData = validate({
|
||||
@@ -69,6 +66,7 @@ const validateDomainData = validate({
|
||||
[R.has('A'), validateARecord('A')],
|
||||
[R.has('URL'), R.propSatisfies(isValidURL, 'URL')],
|
||||
[R.has('MX'), validateMXRecord('MX')],
|
||||
[R.has('TXT'), R.propSatisfies(R.is(String), 'TXT')],
|
||||
[R.T, R.T],
|
||||
]),
|
||||
]),
|
||||
|
||||
Reference in New Issue
Block a user