Added logic.

This commit is contained in:
patelravi 2019-03-25 15:22:09 +05:30
parent 809fa5bcf3
commit 4da4be27e2
5 changed files with 159 additions and 39 deletions

13
TODO
View file

@ -1,12 +1,9 @@
How to validate languages with voice
1. Polly.*
Male female voices for polly+voice combination
2. How to validate languages with voice
Promise, chain will break
3. Male female voices for polly+voice combination
Will childs be there in ssml for validation?
4. Promise, chain will break
5. Will childs be there in ssml for validation?
6.
If Polly.* provided, is language validation required?

View file

@ -107,17 +107,16 @@ export function validateVoiceForSsml(content, voice, language) {
// Validate supported languages.
if (!languageWiseVoices[language]) {
resolve({
success: false, msg: "Invalid language."
success: false, msg: "Invalid language. Language `" + language + "` is not supported."
});
return;
}
// Validate voice for given language.
if (languageWiseVoices[language].indexOf(voiceParts[1]) == -1) {
console.log('==><? thrown here');
// If not *, Validate voice for given language.
if (voiceParts[1] != '*' && languageWiseVoices[language].indexOf(voiceParts[1]) == -1) {
resolve({
success: false,
msg: "XML Validation Error: <Speak> voice " + voice + " is not valid. Refer <link> for list of supported voices."
msg: "<Speak> voice " + voice + " is not valid. Refer <link> for list of supported voices."
});
return;
}
@ -126,10 +125,9 @@ export function validateVoiceForSsml(content, voice, language) {
var contenxtXml = "<root>" + content + "</root>";
parseString(contenxtXml, function (err, xmlJson) {
if (err || !xmlJson) {
// console.log('XMl parsing error validation result', err);
resolve({
success: false,
msg: "Invalid SSML xml structure. Content must be a valid xml. Parsing Error: " + err
msg: "Invalid SSML xml structure. Content must be a valid xml."
})
}
xmlJson = xmlJson.root;
@ -137,24 +135,43 @@ export function validateVoiceForSsml(content, voice, language) {
// Validate supported ssml tags
var validXmlTags = ['break', 'emphasis', 'lang', 'p', 'phoneme', 'prosody', 's', 'say-as', 'sub', 'w'];
var invalidXmlTag = null;
for (var key in xmlJson) {
// console.log('xml json key', key);
if (key !== '_' && key !== '$') {
if (validXmlTags.indexOf(key) == -1) {
invalidXmlTag = key;
break;
var validateNode = function (key, xmlNode) {
if (validXmlTags.indexOf(key) == -1) {
return { valid: false, msg: "Ssml tag <" + key + "> is not supported." };
}
// console.log('childs are ', xmlNode.length);
for (var i = 0; i < xmlNode.length; i++) {
if (typeof xmlNode[i] !== 'string') {
for (var childKey in xmlNode[i]) {
if (childKey !== '_' && childKey !== '$') {
let result = validateNode(childKey, xmlNode[i][childKey]);
if (!result.valid) {
return result;
}
}
}
}
}
return { valid: true };
};
if (typeof xmlJson !== 'string') {
for (var key in xmlJson) {
if (key !== '_' && key !== '$') {
var result = validateNode(key, xmlJson[key]);
if (!result.valid) {
resolve({
success: false,
msg: result.msg
});
return;
}
}
}
}
if (invalidXmlTag) {
resolve({
success: false,
msg: "SSML tag `" + invalidXmlTag + "` is not supported."
})
return;
}
resolve({
success: true

View file

@ -283,11 +283,23 @@ Response.prototype = {
}
var item = this;
plivoUtils.validateVoiceForSsml(body, attributes.voice, attributes.language).then((ssmlValidationResult) => {
console.log('validation result', ssmlValidationResult);
return new Promise(function (resolve, reject) {
plivoUtils.validateVoiceForSsml(body, attributes.voice, attributes.language).then(function (ssmlValidationResult) {
// console.log('result...', ssmlValidationResult);
if (ssmlValidationResult.success == true) {
var result = item.add(new Speak(Response), body, attributes);
resolve(result);
} else {
reject(new Exceptions.PlivoXMLValidationError(ssmlValidationResult.msg));
}
}).catch(function (err) {
// console.log('validation error result...', err);
reject(err);
});
});
return item.add(new Speak(Response), body, attributes);
// plivoUtils.validateVoiceForSsml(body, attributes.voice, attributes.language).then(function (validationResult) {
// console.log('===> validation result', validationResult);
// if (!validationResult.success) {

View file

@ -1,24 +1,117 @@
import assert from 'assert';
import sinon from 'sinon';
import { Response, Client } from '../lib/rest/client';
import { PlivoGenericResponse } from '../lib/base.js';
let client = new Client('sampleid', 'sammpletoken', 'sampleproxy');
describe('SsmlInterface', function () {
it('test', function () {
it('Ssml - Valid XML', function (done) {
let response = new Response();
// Valid speak body
let speak_body = ' Here is a number <w role="amazon: VBD">read</w> \
as a cardinal number: \
<say-ass interpret-as="cardinal">12345</say-ass>. \
<say-as interpret-as="cardinal">klsdjflksjfsldk</say-as>. \
Here is a word spelled out: \
<say-as interpret-as="spell-out">hello</say-as>.';
// response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Conchita' });
response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Conchita' });
console.log(response.toXML());
// response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.*' });
response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Conchita' }).then((result) => {
done();
}).catch((err) => {
done("Valid SSML xml should not be rejected.");
});
});
it('Ssml - Invalid SSML XML Structure', function (done) {
let response = new Response();
// Invalid speak body
let speak_body = ' Here is a number <w role="amazon: VBD">read</w> \
as a cardinal number: \
<say-as interpret-as="cardinal"><wa role="amazon: VBD"><cr>read</wa></say-as>. \
Here is a word spelled out: \
<say-as interpret-as="spell-out">hello</say-as>.';
// response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.*' });
response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Conchita' }).then((result) => {
done(new Error("Invalid xml should be rejected and should throw error."));
}).catch((err) => {
assert.equal('Invalid SSML xml structure. Content must be a valid xml.', err.message);
done();
});
});
it('Ssml - Invalid SSML Tags', function (done) {
let response = new Response();
// Invalid speak body
let speak_body = ' Here is a number <w role="amazon: VBD">read</w> \
as a cardinal number: \
<say-as interpret-as="cardinal"><wa role="amazon: VBD"><cr>read</cr></wa></say-as>. \
Here is a word spelled out: \
<say-as interpret-as="spell-out">hello</say-as>.';
// response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.*' });
response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Conchita' }).then((result) => {
done(new Error("Invalid xml tags should be rejected and should throw error."));
}).catch((err) => {
assert.equal('Ssml tag <wa> is not supported.', err.message);
done();
});
});
it('Ssml - Invalid Language Validation', function (done) {
let response = new Response();
// Invalid speak body
let speak_body = ' Here is a number';
response.addSpeak(speak_body, { language: 'Spanish-Castilian1', voice: 'Polly.Conchita' }).then((result) => {
done(new Error("Unsupported language `Spanish-Castilian1` should be rejected and should throw error."));
}).catch((err) => {
assert.equal('Invalid language. Language `Spanish-Castilian1` is not supported.', err.message);
done();
});
});
it('Ssml - Invalid Language-Voice Combination', function (done) {
let response = new Response();
// Invalid speak body
let speak_body = '<w>Here is a number</w>';
response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Maxim' }).then((result) => {
done(new Error("Invalid language voice combination should be rejected"));
}).catch((err) => {
assert.equal('<Speak> voice Polly.Maxim is not valid. Refer <link> for list of supported voices.', err.message);
done();
});
});
it('Ssml - Valid Language-Voice Combination', function (done) {
let response = new Response();
// Invalid speak body
let speak_body = '<w>Here is a number</w>';
response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Conchita' }).then((result) => {
done();
}).catch((err) => {
done('Validate Language Voice combination should be accepted.');
});
});

View file

@ -1,6 +1,6 @@
import assert from 'assert';
import sinon from 'sinon';
import {Response} from '../lib/utils/plivoxml';
import { Response } from '../lib/utils/plivoxml';
describe('PlivoXML', function () {
it('should work', function () {
@ -17,6 +17,7 @@ describe('PlivoXML', function () {
response.addPlay('url');
const dial = response.addDial();
dial.addNumber('123');
dial.addUser('sip:test@sip.plivo.com');
response.addMessage('∫test', {
src: '123',