Skip to content

Commit

Permalink
Merge pull request #1125 from Automattic/fix/domain-management-cname-…
Browse files Browse the repository at this point in the history
…display

Domains: DNS: Fix bug with CNAME display after adding domain
  • Loading branch information
rads committed Dec 2, 2015
2 parents 774c0bd + 9a52c6a commit 8f20724
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
36 changes: 29 additions & 7 deletions client/lib/domains/dns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@
* External dependencies
*/
const includes = require( 'lodash/collection/includes' ),
mapValues = require( 'lodash/object/mapValues' );
mapValues = require( 'lodash/object/mapValues' ),
endsWith = require( 'lodash/string/endsWith' );

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

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

function validateField( { name, value, type } ) {
function validateField( { name, value, type, selectedDomainName } ) {
switch ( name ) {
case 'name':
return isValidName( value, type, selectedDomainName );
case 'target':
return isValidName( value );
return isValidDomainName( value );
case 'data':
return isValidData( value, type );
case 'protocol':
Expand All @@ -36,10 +39,29 @@ function validateField( { name, value, type } ) {
}
}

function isValidName( name ) {
function isValidDomainName( name ) {
return /^([\da-z-]+\.)+[\da-z-]+$/i.test( name );
}

function isValidName( name, type, selectedDomainName ) {
switch ( type ) {
case 'CNAME':
return (
isValidCname( name, selectedDomainName ) &&
isValidDomainName( name )
);
default:
return isValidDomainName( name );
}
}

function isValidCname( name, selectedDomainName ) {
return (
name !== selectedDomainName &&
endsWith( name, selectedDomainName )
);
}

function isValidData( data, type ) {
switch ( type ) {
case 'A':
Expand All @@ -48,7 +70,7 @@ function isValidData( data, type ) {
return data.match( /^[a-f0-9\:]+$/i );
case 'CNAME':
case 'MX':
return isValidName( data );
return isValidDomainName( data );
case 'TXT':
return data.length < 256;
}
Expand Down
12 changes: 8 additions & 4 deletions client/lib/domains/dns/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import filter from 'lodash/collection/filter';
import React from 'react/addons';
import escapeRegExp from 'lodash/string/escapeRegExp';

/**
* Internal dependencies
Expand All @@ -18,11 +19,14 @@ function updateDomainState( state, domainName, dns ) {
}

function addDns( state, domainName, record ) {
const command = {
[ domainName ]: { records: { $push: [ record ] } }
};
const domainSuffix = new RegExp( '\\.' + escapeRegExp( domainName ) + '\\.$' ),
newRecord = Object.assign( {}, record, {
name: record.name.replace( domainSuffix, '' )
} );

return React.addons.update( state, command );
return React.addons.update( state, {
[ domainName ]: { records: { $push: [ newRecord ] } }
} );
}

function deleteDns( state, domainName, record ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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 8f20724

Please sign in to comment.