Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Domains: DNS: Fix bug with CNAME display after adding domain #1125

Merged
merged 3 commits into from
Dec 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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