How to use the xml2js.ValidationError function in xml2js

To help you get started, we’ve selected a few xml2js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github peerlibrary / node-xml4js / lib / validator.js View on Github external
if (!currentElementSet[segment]) {
      throw new xml2js.ValidationError("Element (" + segment + ") does not match schema, xpath: " + xpath + ", allowed elements: " + util.inspect(currentElementSet, false, null));
    }
    else if (!parser.resolveElementTypeName(xpath, namespaces, defaultNamespace, stack[i][parser.attrkey], currentElementSet[segment])) {
      throw new xml2js.ValidationError("Element (" + segment + ") does not match schema, type not specified, xpath: " + xpath + ", element: " + util.inspect(currentElementSet[segment], false, null));
    }
    else {
      var type = parser.resolveType(xpath, parser.resolveElementTypeName(xpath, namespaces, defaultNamespace, stack[i][parser.attrkey], currentElementSet[segment]));
      currentElementSet = parser.tryChildren(xpath, type);
    }
  });

  var lastSegment = path[path.length - 1];

  if (!currentElementSet[lastSegment]) {
    throw new xml2js.ValidationError("Element (" + lastSegment + ") does not match schema, xpath: " + xpath + ", allowed elements: " + util.inspect(currentElementSet, false, null));
  }

  var lastSegmentTypeName = parser.resolveElementTypeName(xpath, namespaces, defaultNamespace, newValue[parser.attrkey], currentElementSet[lastSegment]);

  var attributes = parser.resolveToAttributes(xpath, lastSegmentTypeName);
  _.each(newValue[parser.attrkey] || {}, function (value, attribute) {
    var attributeName = parser.namespacedName(namespaces, defaultNamespace, attribute);
    if (attribute.slice(0, 5) === 'xmlns') {
      delete newValue[parser.attrkey][attribute];
    }
    // TODO: xsi prefix should probably not be hard-coded
    else if (attribute.slice(0, 4) === 'xsi:') {
      delete newValue[parser.attrkey][attribute];
    }
    else if (!attributes[attributeName]) {
      throw new xml2js.ValidationError("Unexpected attribute " + attributeName + ", xpath: " + xpath + ", allowed attributes: " + util.inspect(attributes, false, null))
github peerlibrary / node-xml4js / lib / validator.js View on Github external
_.each(newValue[parser.attrkey] || {}, function (value, attribute) {
    var attributeName = parser.namespacedName(namespaces, defaultNamespace, attribute);
    if (attribute.slice(0, 5) === 'xmlns') {
      delete newValue[parser.attrkey][attribute];
    }
    // TODO: xsi prefix should probably not be hard-coded
    else if (attribute.slice(0, 4) === 'xsi:') {
      delete newValue[parser.attrkey][attribute];
    }
    else if (!attributes[attributeName]) {
      throw new xml2js.ValidationError("Unexpected attribute " + attributeName + ", xpath: " + xpath + ", allowed attributes: " + util.inspect(attributes, false, null))
    }
    else {
      var parse = parser.resolveToParse(xpath, parser.resolveAttributeType(xpath, attributes[attributeName]));
      if (_.isString(value)) {
        delete newValue[parser.attrkey][attribute];
        newValue[parser.attrkey][parser.namespacedOrNotName(namespaces, defaultNamespace, attribute, options.outputWithNamespace)] = parser.tryParse(parse, value);
      }
      else if (value.value) {
        // TODO: What if user wants namespace information, we should not replace with only the value in that case
        delete newValue[parser.attrkey][attribute];
        newValue[parser.attrkey][parser.namespacedOrNotName(namespaces, defaultNamespace, attribute, options.outputWithNamespace)] = parser.tryParse(parse, value.value);
      }
      else {
        throw new xml2js.ValidationError("Invalid attribute " + attributeName + " value, xpath: " + xpath + ": " + util.inspect(value, false, null))
      }
    }
github oracle / netsuite-suitecloud-node-cli / src / services / ProjectInfoService.js View on Github external
_validateXml(xmlPath, previousValue, newValue) {
		//TODO Add more cases
		if (xmlPath === MANIFEST_TAG_XML_PATH) {
			let manifestTagAttributes = newValue['$'];
			if (!manifestTagAttributes || !manifestTagAttributes[PROJECT_TYPE_ATTRIBUTE]) {
				throw new xml2js.ValidationError(
					TranslationService.getMessage(ERRORS.XML_PROJECTTYPE_ATTRIBUTE_MISSING)
				);
			} else if (
				manifestTagAttributes[PROJECT_TYPE_ATTRIBUTE] !== PROJECT_SUITEAPP &&
				manifestTagAttributes[PROJECT_TYPE_ATTRIBUTE] !== PROJECT_ACP
			) {
				throw new xml2js.ValidationError(
					TranslationService.getMessage(ERRORS.XML_PROJECTTYPE_INCORRECT)
				);
			}
		}
		return newValue;
	}