Skip to content

Commit

Permalink
Domains: allow different variations of root domain for supported records
Browse files Browse the repository at this point in the history
Before, we only support an empty name (subdomain) as means of inputting the root
domain. Since some users might by mistake or out of habit input other
variations, this change will treat the following the same as empty
subdomain and add a root domain (for records that it's allowed):
@
@.example.com
@.example.com.
example.com
example.com.

Fixes #3318
  • Loading branch information
klimeryk committed Feb 22, 2016
1 parent 9ae0b11 commit 0c2962d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
31 changes: 21 additions & 10 deletions client/lib/domains/dns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ const includes = require( 'lodash/includes' ),
mapValues = require( 'lodash/mapValues' ),
endsWith = require( 'lodash/endsWith' );

function validateAllFields( fieldValues ) {
function validateAllFields( fieldValues, domainName ) {
return mapValues( fieldValues, ( value, fieldName ) => {
const isValid = validateField( {
value,
domainName,
name: fieldName,
value: value,
type: fieldValues.type,
} );

return isValid ? [] : [ 'Invalid' ];
} );
}

function validateField( { name, value, type } ) {
function validateField( { name, value, type, domainName } ) {
switch ( name ) {
case 'name':
return isValidName( value, type );
return isValidName( value, type, domainName );
case 'target':
return isValidDomain( value, type );
case 'data':
Expand All @@ -45,8 +46,8 @@ function isValidDomain( name ) {
return /^([a-z0-9-_]{1,63}\.)*[a-z0-9-]{1,63}\.[a-z]{2,63}$/i.test( name );
}

function isValidName( name, type ) {
if ( isNameEmptyAndItsAllowed( name, type ) ) {
function isValidName( name, type, domainName ) {
if ( isRootDomain( name, domainName ) && canBeRootDomain( type ) ) {
return true;
}

Expand Down Expand Up @@ -87,8 +88,8 @@ function getNormalizedData( record, selectedDomainName ) {
function getNormalizedName( name, type, selectedDomainName ) {
const endsWithDomain = endsWith( name, '.' + selectedDomainName );

if ( isNameEmptyAndItsAllowed( name, type ) ) {
return name;
if ( isRootDomain( name, selectedDomainName ) && canBeRootDomain( type ) ) {
return '';
}

if ( endsWithDomain ) {
Expand All @@ -106,8 +107,18 @@ function isIpRecord( type ) {
return includes( [ 'A', 'AAAA' ], type );
}

function isNameEmptyAndItsAllowed( name, type ) {
return ! name && includes( [ 'MX', 'SRV', 'TXT' ], type );
function isRootDomain( name, domainName ) {
const rootDomainVariations = [
'@',
domainName,
domainName + '.',
'@.' + domainName,
'@.' + domainName + '.' ];
return ! name || includes( rootDomainVariations, name );
}

function canBeRootDomain( type ) {
return includes( [ 'MX', 'SRV', 'TXT' ], type );
}

function getFieldWithDot( field ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const DnsAddNew = React.createClass( {
initialFields: this.getInitialFields(),
onNewState: this.setFormState,
validatorFunction: ( fieldValues, onComplete ) => {
onComplete( null, validateAllFields( fieldValues ) );
onComplete( null, validateAllFields( fieldValues, this.props.selectedDomainName ) );
}
} );

Expand Down

0 comments on commit 0c2962d

Please sign in to comment.