Module: Respect
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/respect.rb,
lib/respect/schema.rb,
lib/respect/version.rb,
lib/respect/hash_def.rb,
lib/respect/items_def.rb,
lib/respect/validator.rb,
lib/respect/array_def.rb,
lib/respect/global_def.rb,
lib/respect/schema_def.rb,
lib/respect/doc_helper.rb,
lib/respect/doc_parser.rb,
lib/respect/dsl_dumper.rb,
lib/respect/uri_schema.rb,
lib/respect/any_schema.rb,
lib/respect/null_schema.rb,
lib/respect/hash_schema.rb,
lib/respect/org3_dumper.rb,
lib/respect/float_schema.rb,
lib/respect/in_validator.rb,
lib/respect/array_schema.rb,
lib/respect/string_schema.rb,
lib/respect/regexp_schema.rb,
lib/respect/boolean_schema.rb,
lib/respect/integer_schema.rb,
lib/respect/numeric_schema.rb,
lib/respect/ip_addr_schema.rb,
lib/respect/match_validator.rb,
lib/respect/fake_name_proxy.rb,
lib/respect/utc_time_schema.rb,
lib/respect/core_statements.rb,
lib/respect/datetime_schema.rb,
lib/respect/has_constraints.rb,
lib/respect/ipv6_addr_schema.rb,
lib/respect/composite_schema.rb,
lib/respect/format_validator.rb,
lib/respect/def_without_name.rb,
lib/respect/ipv4_addr_schema.rb,
lib/respect/unit_test_helper.rb,
lib/respect/equal_to_validator.rb,
lib/respect/less_than_validator.rb,
lib/respect/min_length_validator.rb,
lib/respect/max_length_validator.rb,
lib/respect/multiple_of_validator.rb,
lib/respect/greater_than_validator.rb,
lib/respect/divisible_by_validator.rb,
lib/respect/json_schema_html_formatter.rb,
lib/respect/less_than_or_equal_to_validator.rb,
lib/respect/greater_than_or_equal_to_validator.rb
Overview
Provide methods and classes to define, validate, sanitize and dump object schema.
Classes in this module are split in 5 groups:
-
The schema classes are the core of this module since they support the validation process and are the internal representation of schema specification (see Schema).
-
The definition classes (aka def classes) are the front-end of this module since they implement the schema definition DSL (see GlobalDef).
-
The validator classes implement validation routine you can attach to your schema. accessible via the schema's options (see Validator).
-
The dumper classes are the back-end of this module since they implement the convertion of the internal schema representation to different formats.
-
The miscellaneous classes provides various support for the other categories.
You can extend this library in many ways:
-
If you want to add your own schema class, you can sub-class the CompositeSchema class. Sub-classing of the Schema class is not well supported yet as it may have some issues with the current dumpers (see DslDumper and Org3Dumper). Fortunately, most of the cases can be handled by CompositeSchema.
-
If you want to simply add new statements to the schema definition DSL, you can just bundle them in a module and call Respect.extend_dsl_with (see CoreStatements for further information).
Extension of the validator and dumper classes is still experimental. Also, creating custom definition classes is not recommended yet.
Defined Under Namespace
Modules: CoreStatements, DefWithoutName, DocHelper, HasConstraints, UnitTestHelper Classes: AnySchema, ArrayDef, ArraySchema, BooleanSchema, CompositeSchema, DatetimeSchema, DivisibleByValidator, DocParser, DslDumper, EqualToValidator, FakeNameProxy, FloatSchema, FormatValidator, GlobalDef, GreaterThanOrEqualToValidator, GreaterThanValidator, HashDef, HashSchema, IPAddrSchema, InValidator, IntegerSchema, InvalidSchemaError, Ipv4AddrSchema, Ipv6AddrSchema, ItemsDef, JSONSchemaHTMLFormatter, LessThanOrEqualToValidator, LessThanValidator, MatchValidator, MaxLengthValidator, MinLengthValidator, MultipleOfValidator, NullSchema, NumericSchema, Org3Dumper, RegexpSchema, RespectError, Schema, SchemaDef, StringSchema, URISchema, UTCTimeSchema, ValidationError, Validator
Constant Summary
- STATEMENT_NAME_REGEXP =
/^[a-z_][a-z_0-9]*$/
- VERSION =
"0.1.1"
Class Method Summary (collapse)
-
+ (Object) extend_dsl_with(mod)
Extend the schema definition DSL with the statements defined in the given module mod.
-
+ (Object) sanitize_object!(object, sanitized_object)
Sanitize the given object in-place according to the given sanitized_object.
-
+ (Boolean) schema_defined_for?(statement_name)
Test whether a schema is defined for the given statement_name.
-
+ (Object) schema_for(statement_name)
Return the schema class associated to the given statement_name.
-
+ (Object) schema_name_for(statement_name)
Build a schema class name from the given statement_name.
-
+ (Boolean) validator_defined_for?(constraint_name)
Test whether a validator is defined for the given constraint_name.
-
+ (Object) validator_for(constraint_name)
Turn the given constraint_name into a validator class symbol.
-
+ (Object) validator_name_for(constraint_name)
Turn the given string (assuming it is a constraint name) into a validator class name string.
Class Method Details
+ (Object) extend_dsl_with(mod)
Extend the schema definition DSL with the statements defined in the given module mod. Its methods would be available to each definition class calling Respect::GlobalDef.include_core_statements.
121 122 123 124 125 126 127 128 |
# File 'lib/respect.rb', line 121 def extend_dsl_with(mod) raise ArugmentError, "cannot extend DSL with CoreStatements" if mod == CoreStatements CoreStatements.send(:include, mod) # We must "refresh" all the classes include "CoreStatements" by re-including it to # work around the # {dynamic module include problem}[http://eigenclass.org/hiki/The+double+inclusion+problem] GlobalDef.core_contexts.each{|c| c.send(:include, CoreStatements) } end |
+ (Object) sanitize_object!(object, sanitized_object)
Sanitize the given object in-place according to the given sanitized_object. A sanitized object contains value with more specific data type. Like a URI object instead of a plain string.
Non-sanitized value are not touch (i.e. values present in object but not in sanitized_object). However, +object+ and <a href="http://:key">object</a> are considered as referring to the same value, but they original key would be preserved.
Example:
object = { "int" => "42" }
Respect.sanitize_object!(object, { "int" => 42 }
object #=> { "int" => 42 }
object = { :int => "42" }
Respect.sanitize_object!(object, { "int" => 42 }
object #=> { :int => 42 }
The sanitized object is accessible via the Respect::Schema#sanitized_object method after a successful validation.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/respect.rb', line 200 def sanitize_object!(object, sanitized_object) case object when Hash if sanitized_object.is_a? Hash sanitized_object.each do |name, value| if object.has_key?(name) object[name] = sanitize_object!(object[name], value) else object[name.to_sym] = sanitize_object!(object[name.to_sym], value) end end object else sanitized_object end when Array if sanitized_object.is_a? Array sanitized_object.each_with_index do |value, index| object[index] = sanitize_object!(object[index], value) end object else sanitized_object end else sanitized_object end end |
+ (Boolean) schema_defined_for?(statement_name)
Test whether a schema is defined for the given statement_name.
161 162 163 |
# File 'lib/respect.rb', line 161 def schema_defined_for?(statement_name) !!schema_for(statement_name) end |
+ (Object) schema_for(statement_name)
151 152 153 154 155 156 157 158 |
# File 'lib/respect.rb', line 151 def schema_for(statement_name) klass = Respect.schema_name_for(statement_name).safe_constantize if klass && klass < Schema && klass.public_methods.include?(:new) klass else nil end end |
+ (Object) schema_name_for(statement_name)
Build a schema class name from the given statement_name.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/respect.rb', line 133 def schema_name_for(statement_name) unless statement_name =~ STATEMENT_NAME_REGEXP raise ArgumentError, "statement '#{statement_name}' name must match #{STATEMENT_NAME_REGEXP.inspect}" end const_name = statement_name.to_s if const_name == "schema" "#{self.name}::Schema" else "#{self.name}::#{const_name.camelize}Schema" end end |
+ (Boolean) validator_defined_for?(constraint_name)
Test whether a validator is defined for the given constraint_name.
178 179 180 |
# File 'lib/respect.rb', line 178 def validator_defined_for?(constraint_name) !!validator_for(constraint_name) end |
+ (Object) validator_for(constraint_name)
Turn the given constraint_name into a validator class symbol. Return nil if the validator class does not exist.
173 174 175 |
# File 'lib/respect.rb', line 173 def validator_for(constraint_name) validator_name_for(constraint_name).safe_constantize end |
+ (Object) validator_name_for(constraint_name)
Turn the given string (assuming it is a constraint name) into a validator class name string.
167 168 169 |
# File 'lib/respect.rb', line 167 def validator_name_for(constraint_name) "#{self.name}::#{constraint_name.to_s.camelize}Validator" end |