Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: schema_field_name= translate(field_name) #98

Open
paulbalomiri opened this issue Mar 13, 2016 · 0 comments
Open

Feature request: schema_field_name= translate(field_name) #98

paulbalomiri opened this issue Mar 13, 2016 · 0 comments

Comments

@paulbalomiri
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant