I wrote something like this last night:

(def user-metadata {:fields
  ((:name (required) (max-length 50))
   (:email (required) (max-length 250)))})

(defn create-user [name, email]
  (with-meta
    {:name name :email email}
    user-data))

I have a validate function which pulls the metadata from a given
instance and I can also access user-metadata directly for generating
UI forms and so on.  The required and max-length functions each
generate little validators which have a predicate, error message
formatter etc.

Is this sensible Clojure?

Cheers
Barry

Am

On Feb 2, 12:53 am, Erik Price <erikpr...@gmail.com> wrote:
> On Sun, Jan 31, 2010 at 10:33 PM, Barry Dahlberg
>
>
>
> <barry.dahlb...@gmail.com> wrote:
> > Perhaps I'll write up my findings once the language stops intimidating
> > me.
>
> > At the moment I'm writing an account "object".  I have some standard
> > CRUD type functions which operate on a map behind the scenes.  I would
> > like to add some metadata to start specifying rules for validation, UI
> > generation and so on.  The trouble is I can't really figure out where
> > to attach this metadata as I don't have a type definition as such,
> > e.g. I would do this in C#:
>
> >    [Required, MaxLength(50)]
> >    public string Name { get; set; }
>
> > defstruct seems promising though I can't find an example with
> > metadata.
>
> > Any pointers of where to start looking?
>
> You can attach metadata to Clojure constructs using the with-meta function:
>
> user=> (def x {:name "Frank"})
> #'user/x
> user=> (def x-with-metadata (with-meta x {:RequiredParameter true,
> :MaxLength 50}))
> #'user/x-with-metadata
> user=> (:MaxLength (meta x-with-metadata))
> 50
>
> As for whether you want to define the metadata directly on your map,
> or on functions that construct/operate on your map, or somewhere else,
> that's your design decision.
>
> e

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to