public
Description: Conformance checking of JSON values against ECMAScript 4.0 types
Homepage:
Clone URL: git://github.com/osteele/jcon.git
jcon /
name age message
file .gitignore Tue Apr 15 07:36:26 -0700 2008 move coverage dir [osteele]
file LICENSE Mon Apr 14 03:23:59 -0700 2008 +README, Rakefile [osteele]
file README.rdoc Sun Mar 22 19:41:26 -0700 2009 use jeweler for gemspec [osteele]
file Rakefile Sun Mar 22 19:41:26 -0700 2009 use jeweler for gemspec [osteele]
file TODO Wed Apr 16 19:11:54 -0700 2008 add example of js fu integration [osteele]
file VERSION.yml Sun Mar 22 19:41:26 -0700 2009 use jeweler for gemspec [osteele]
file jcon.gemspec Sun Mar 22 19:41:26 -0700 2009 use jeweler for gemspec [osteele]
directory lib/ Tue Apr 15 07:20:26 -0700 2008 Any type; StructureType -> RecordType [osteele]
directory spec/ Wed Apr 16 19:11:54 -0700 2008 add example of js fu integration [osteele]
README.rdoc

JCon — JavaScript Type Conformance Checking

JCON (the "JavaScript Conformance" gem), is a companion to JSON. It tests JSON values against ECMAScript 4.0-style type definitions (PDF), such as string?, (int, boolean), or [string, (int, boolean), {x:double, y:double}?].

JCON also defines an RSpec matcher, conforms_to_js.

Use JCON together with the JavaScript Fu Rails plugin to test the argument values in generated JavaScript function calls.

Install

  gem install rcon

Usage

  type = JCON::parse "[string, int]"
  type.contains?(['a', 1]) # => true
  type.contains?(['a', 'b']) # => false
  type.contains?(['a', 1, 2]) # => true

  type = JCON::parse "type S = (string, int); {a: [S], b: int}"
  type.contains?({:a => [1, 'b'], :b => 2}) # => true

RSpec Matcher

  [1, 'xyzzy'].should conform_to_js('[int, string]')
  [1, 2, 'xyzzy'].should_not conform_to_js('[int, string]')
  {:x => 1}.should conform_to_js('{x: int}')

Use this with the JavaScript Fu Rails plugin to test JSON arguments:

  '<script>fn("id", {x:1, y:2}, true)</script>'.should call_js('fn') do |args|
    args[0].should conform_to_js('string')
    args[1].should conform_to_js('{x:int, y:int}')
    args[2].should conform_to_js('boolean')
    # or:
    args.should conform_to_js('[string, {x:int, y:int}, boolean]')
  end

License

Copyright 2008 by Oliver Steele. All rights reserved. Released under the MIT License.