Skip to content

Feature request: schema_field_name= translate(field_name) #98

@paulbalomiri

Description

@paulbalomiri

The problem arose when creating a variable length array, each element being a validated object like this (coffee)

First apologies, for using coffee & jade, but it makes the issue a bit less verbose. I can translate the examples if that's an issue

new SimpleSchema client=
  contacts:
    type: String
    minCount: 1
    maxCount: 3
  'contacts.$.role':
    type:String
    optional: true
  'contacts.$.contact_id':
    type:String

Now creating a form would look like this (jade):

+form schema = client action = some_action
  +list field = "contacts"
   +input field = "contacts.$.role"
   +link field = "contacts.$.contacts_id"

Now the list template offers list-specific add/remove buttons e.t.c, while the contents of the list
validate according to the schema keys under contacts.$.contacts_id and contacts.$.role

The problem, however is that each element of the list references the same schema key, and thus share the same validation context state

Solution?
given that the validation state as supplied by validateOne is per instance, while the schema key is per type a mapping between the two would come in handy.

Note that this has little to do with the validation context, as the same field-schemas need to validate more than once in the same validation context. With arrays that is once per array element.

I am proposing to introduce a mapper function, with this sensible default:

schema_for_field = (field_name, schema)->
  if schema[field_name]
    return field_name
  transformed = field_name.split('.').map (field_name_element)-> 
    if /d+/.exec field_name_element
      "$" # This is a list index
    else
      field_name_element #this is a schema index
  transformed.join "."

The beauty of this is that it is exensible, such that validation would also be possible for patterns of field names. Say you wanted to validate according to some field-schema all fields starting with max, then you could map the field name (field)->field.startsWith "min" and "min" or field

The validation state would still need to be stored for each field_name as supplied by the field=<field_name> template parameter.

Sorry for the lengthy post, but i hope the proposal is worth considering.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions