0

I want to write a checker to check if there are any invalid characters,

The user will write their rules in JSON file,

And I need to follow their rules than check if the text file has any invalid characters.

I need to define the rule in JSON ,

"range":["A-Z,a-z,0-9,!#$%-.@ ^_~\/;:?{[]}`*)(+|"]

The user may define their rule like the above rule,

In human view, we can know A-Z means A,B,...,Z

And !#$%-.@ ^_~\/;:?{[]}*)(+| means each character defines them self.

What's the good way , good practice to write the DSL in JSON file and be parsed by RUBY.

For example, how could I know the input rule is regular expression or general character in the RUBY program ? because when I load the JSON file, the program will see it as plain text.

newBike
  • 14,385
  • 29
  • 109
  • 192

1 Answers1

0

I would suggest using the JSON library that comes with ruby.

http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html

Parse it and have it spit out the JSON as hashes and arrays, then change logic by calling the .class method on the object and distinguishing among them.

This should make checking easier.

Saifis
  • 2,197
  • 1
  • 22
  • 36
  • Hi @saiis for example , if I captured "a-z" how should Ruby knows, it means a `range` from `a to z` not the plain text "a-z" – newBike May 23 '14 at 08:21
  • JSON, just as HTML, cannot even be parsed by regular expressions. See http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Patrick Oscity May 23 '14 at 08:43
  • oh I think I got the question wrong, I was thinking you wanted to know how to parse and check JSON, sorry I missed the line about having to define the rule in JSON. I'm with p11y, you can't do that in JSON, you can take the regex out from the JSON and use it inside ruby, I think. – Saifis May 23 '14 at 08:57