Skip to content

Commit

Permalink
Update for multi-variable organization schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian de Bhal committed Nov 29, 2017
1 parent 3b71030 commit ef5d5a0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
}
},
"organization": { "$ref": "#/definitions/empty" }
}
},
"formField": "organization"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,44 @@ import validationSchema from './fr-schema';

const debug = debugFactory( 'calypso:components:domains:registrant-extra-info:validation' );

const ajv = new Ajv( { messages: false } );
const ajv = new Ajv( { messages: false, extendRefs: true, verbose: true } );
ajv.addMetaSchema( draft04 );
const validate = ajv.compile( validationSchema );

const schemaPathFilterRegex = /anyOf\/\d+/;

export function inferFormField( { keyword, params, dataPath } ) {
const possiblePathAddition =
keyword === 'required' && params && params.missingProperty
? `.${ params.missingProperty }`
: '';

return ( dataPath + possiblePathAddition ).slice( 1 );
}

/*
* @returns errors by field, like: { 'extra.field: name, errors: [ string ] }
*/
export default function validateContactDetails( contactDetails ) {
// Populate validate.errors
const valid = validate( contactDetails );
valid && debug( validate.errors );
valid || debug( validate.errors );

return reduce(
validate.errors,
( accumulatedErrors, { dataPath, keyword, schemaPath } ) => {
( accumulatedErrors, validationError ) => {
// Drop '.' prefix
const path = dataPath.slice( 1 );
const { keyword, schemaPath, schema } = validationError;

if ( schemaPathFilterRegex.test( schemaPath ) ) {
return accumulatedErrors;
}

const formFieldPath = schema.formField || inferFormField( validationError );

const appendThisMessage = before => [ ...( before || [] ), keyword ];

return update( accumulatedErrors, path, appendThisMessage );
return update( accumulatedErrors, formFieldPath, appendThisMessage );
},
{}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ describe( 'validateContactDetails', () => {

test( 'should reject SIRET for VAT', () => {
realSiretNumbers.forEach( ( [ registrantVatId ] ) => {
const testDetails = Object.assign( {}, contactDetails, { extra: { registrantVatId } } );

const testDetails = contactWithExtraProperty( 'registrantVatId', registrantVatId );
const result = validateContactDetails( testDetails );
expect( result, `expected to reject '${ registrantVatId }'` )
.to.have.property( 'extra' )
Expand Down Expand Up @@ -288,7 +287,7 @@ describe( 'validateContactDetails', () => {

test( 'should reject our bad SIRET examples', () => {
badTrademarkNumbers.forEach( ( [ trademarkNumber ] ) => {
const testDetails = Object.assign( {}, contactDetails, { extra: { trademarkNumber } } );
const testDetails = contactWithExtraProperty( 'trademarkNumber', trademarkNumber );

const result = validateContactDetails( testDetails );
expect( result, `expected to reject '${ trademarkNumber }'` )
Expand Down

0 comments on commit ef5d5a0

Please sign in to comment.