Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function recursiveObjectExtraction(node: YAMLNode | undefined, object: PlainObject, validationContext: ValidationContext, source: ProjectSource): any {
// ATTENTION: Typings of the yaml ast parser are wrong
if (!node) {
return object;
}
switch (node.kind) {
case Kind.MAP:
const mapNode = node as YamlMap;
mapNode.mappings.forEach(val => {
object[val.key.value] = recursiveObjectExtraction(val.value, {}, validationContext, source);
});
return object;
case Kind.MAPPING:
throw new Error('Should never be reached since a mapping can not exist without a map.');
case Kind.SCALAR:
const scalarNode = node as YAMLScalar;
// check whether string or number scalar
if (scalarNode.doubleQuoted || scalarNode.singleQuoted || isNaN(Number(scalarNode.value))) {
return scalarNode.value;
} else {
return Number(scalarNode.value);
}
case Kind.SEQ:
const seqNode = node as YAMLSequence;
return seqNode.items.map(val => recursiveObjectExtraction(val, {}, validationContext, source));
case Kind.INCLUDE_REF:
validationContext.addMessage(ValidationMessage.error(`Include references are not supported`, new MessageLocation(source, node.startPosition, node.endPosition)));
return undefined;
case Kind.ANCHOR_REF:
//Do some error checking on the current key
//If there is an error then throw the error on it and don't add the children
//Error: If key not found
if(!this.kuberSchema["childrenNodes"][currentNode.key.value]){
this.errorHandler.addErrorResult(currentNode.key, "Command \'" + currentNode.key.value + "\' is not found", DiagnosticSeverity.Error);
}
//Error: It did not validate correctly
if(!this.isValid(currentNodePath)){
this.errorHandler.addErrorResult(currentNode.key, "Command \'" + currentNode.key.value + "\' is not in a valid location in the file", DiagnosticSeverity.Error);
}
//Error: If type is mapping then we need to check the scalar type
if(currentNode.kind === Kind.MAPPING && currentNode.value !== null && this.hasInvalidType(currentNode)){
this.errorHandler.addErrorResult(currentNode.value, "Command \'" + currentNode.key.value + "\' has an invalid type. Valid type(s) are: " + this.validTypes(currentNode).toString(), DiagnosticSeverity.Error);
}
let childrenNodes = generateChildren(currentNode.value);
childrenNodes.forEach(child => {
//We are getting back a bunch of nodes which all have a key and we adding them
let newNodePath = currentNodePath.concat(child);
if(!this.isValid(newNodePath)){
if(!this.kuberSchema["childrenNodes"][child.key.value]){
this.errorHandler.addErrorResult(child, "Command \'" + child.key.value + "\' is not found", DiagnosticSeverity.Warning);
}
if(this.hasAdditionalProperties(currentNode.key.value)){
this.errorHandler.addErrorResult(child, "\'" + child.key.value + "\' is an additional property of " + currentNode.key.value, DiagnosticSeverity.Warning);
//When a parent node exists we need to check if the child types are invalid
if(currentNodePath.length >= 2){
parentNodeSearch = this.searchSchema(rootNode, currentNodePath[currentNodePath.length - 2]);
//Error: Check if this is the right child type of parent
if(this.isInvalidParentType(parentNodeSearch, currentNodePath[currentNodePath.length - 2].value , currentNode.key.value)){
this.errorHandler.addErrorResult(currentNode, "Node \'" + currentNode.key.value + "\' has an invalid type. Valid type(s) of key node are: " + this.collectTypesForParent(parentNodeSearch, currentNode.key.value).toString(), DiagnosticSeverity.Error);
}
}
if(!(currentNodeInSchema.length > 0)){
this.errorHandler.addErrorResult(currentNode.key, "Node \'" + currentNode.key.value + "\' is not found", DiagnosticSeverity.Error);
}
//Error: If type is mapping then we need to check the scalar type
if(currentNode.kind === Kind.MAPPING && currentNode.value !== null && this.isInvalidType(currentNode, currentNodeInSchema)){
this.errorHandler.addErrorResult(currentNode.value, "Node \'" + currentNode.key.value + "\' has an invalid type. Valid type(s) are: " + this.collectTypes(currentNodeInSchema).toString(), DiagnosticSeverity.Error);
}
let childrenNodes = generateChildren(currentNode.value);
childrenNodes.forEach(child => {
//We are getting back a bunch of nodes which all have a key and we adding them
let newNodePath = currentNodePath.concat(child);
let searchThroughSchema = this.searchSchema(rootNode, child);
let isValidInSchema = searchThroughSchema.length > 0;
if(!isValidInSchema){
if(this.hasAdditionalProperties(currentNodeInSchema)){
this.errorHandler.addErrorResult(child, "\'" + child.key.value + "\' is an additional property of " + currentNode.key.value, DiagnosticSeverity.Warning);
}else{
this.errorHandler.addErrorResult(child, "\'" + child.key.value + "\' is not a valid child node of " + currentNode.key.value, DiagnosticSeverity.Error);
}
function isMapping(node: YAMLNode) {
return node.kind === Kind.MAPPING;
}