Module: Treequel::Model::SchemaValidations

Included in:
Treequel::Model
Defined in:
lib/treequel/model/schemavalidations.rb

Overview

A collection of schema-based validations for LDAP model objects.

Constant Summary collapse

IGNORED_OPERATIONAL_ATTRS =

OpenLDAP servers with syncrepl include ‘entryCSN’ and ‘contextCSN’ attributes, but don’t define its attribute type in the subschema. This is a list of operational attribute types that don’t appear in the subschema that shouldn’t be considered when validating MUST and MAY attributes. (www.openldap.org/its/index.cgi/Development?id=5573)

[ :entryCSN, :contextCSN ]

Instance Method Summary collapse

Instance Method Details

#validate(options = {}) ⇒ Object

Entrypoint – run all the validations, adding any errors to the object’s #error collector.



21
22
23
24
25
26
27
28
# File 'lib/treequel/model/schemavalidations.rb', line 21

def validate( options={} )
	return unless options[:with_schema]

	self.validate_structural_objectclass
	self.validate_must_attributes
	self.validate_may_attributes
	self.validate_attribute_syntax
end

#validate_attribute_syntaxObject

Validate that the attribute values present in the entry are all valid according to the syntax rule for it.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/treequel/model/schemavalidations.rb', line 73

def validate_attribute_syntax
	@values.each do |attribute, values|
		[ values ].flatten.each do |value|
			begin
				self.get_converted_attribute( attribute.to_sym, value )
			rescue => err
				self.log.error "validation for %p failed: %s: %s" %
					[ attribute, err.class.name, err.message ]
				attrtype = self.find_attribute_type( attribute )
				self.errors.add( attribute, "isn't a valid %s value" %
					[ attrtype.syntax ? attrtype.syntax.desc : attrtype.syntax_oid ] )
			end
		end
	end
end

#validate_may_attributesObject

Validate that all attributes present in the entry are allowed by either a MUST or a MAY rule of one of its objectClasses.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/treequel/model/schemavalidations.rb', line 55

def validate_may_attributes
	hash = (self.entry || {} ).merge( @values )
	attributes = hash.keys.map( &:to_sym ).uniq
	valid_attributes = self.valid_attribute_oids +
		self.operational_attribute_oids +
		IGNORED_OPERATIONAL_ATTRS

	self.log.debug "Validating MAY attributes: %p against the list of valid OIDs: %p" %
		[ attributes, valid_attributes ]
	unknown_attributes = attributes - valid_attributes
	unknown_attributes.each do |oid|
		self.errors.add( oid, "is not allowed by entry's objectClasses" )
	end
end

#validate_must_attributesObject

Validate that all attributes that MUST be included according to the entry’s objectClasses have at least one value.



41
42
43
44
45
46
47
48
49
50
# File 'lib/treequel/model/schemavalidations.rb', line 41

def validate_must_attributes
	self.must_attribute_types.each do |attrtype|
		oid = attrtype.name
		if attrtype.single?
			self.errors.add( oid, "MUST have a value" ) if self[ oid ].nil?
		else
			self.errors.add( oid, "MUST have at least one value" ) if self[ oid ].empty?
		end
	end
end

#validate_structural_objectclassObject

Ensure that the object has at least one structural objectClass.



32
33
34
35
36
# File 'lib/treequel/model/schemavalidations.rb', line 32

def validate_structural_objectclass
	unless self.object_classes.any? {|oc| oc.structural? }
		self.errors.add( :entry, "must have at least one structural objectClass" )
	end
end