Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahmed-musallam/6949234897cf966e4fda483e4445f945 to your computer and use it in GitHub Desktop.
Save ahmed-musallam/6949234897cf966e4fda483e4445f945 to your computer and use it in GitHub Desktop.
import javax.jcr.PropertyType
/**
* This script will find all PropertyDifinitions for a node instance from it's primaryType and mixins
* It will then determine if any of the current properties violate any of the PropertyDifinitions. it only checks property name and type.
* For example, a "dam:Asset" node with a property "test" and a property "jcr:mixinTypes" of Type Reference has two violations:
* 1. "test" is not on the list of allowed properties for that node type
* 2. "jcr:mixinTypes" must be of type "Name"
*/
def NODE_PATH = "/path/to/node/to/check"
def getPrpertyDefinitions(node){
def mixinTypes = node.getMixinNodeTypes() as List
def types = mixinTypes == null ? [] : mixinTypes
types.add(node.primaryNodeType)
def defs = [] as List
types.each {
it.propertyDefinitions.each{ defs.add(it)}
}
return defs
}
def hasConstraintViolations(node){
def definitions = getPrpertyDefinitions(node)
println "checking against property definitions: ${definitions}"
def acceptsAnyProps = 0 > definitions.findIndexOf{ d -> d.name == "*"}
def props = node.properties
while (props.hasNext()){
def prop = props.next()
def index = definitions.findIndexOf{ d -> prop.name == d.name }
if (index < 0 && !acceptsAnyProps) {
println "Property ${prop.name} does not match any propertyDefinition for the node [${node.path}]"
return true;
}
else if(index >= 0) {
definition = definitions[index]
if(definition.requiredType != prop.type){
println "Property ${prop.name} is of type ${PropertyType.nameFromValue(prop.type)} but the definition requires ${PropertyType.nameFromValue(definition.requiredType)}"
return true
}
}
}
return false;
}
println hasConstraintViolations(getNode(NODE_PATH))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment