Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Trying to redefine Text which is a string #31

Closed
easyMaxi opened this issue Oct 30, 2015 · 4 comments
Closed

Trying to redefine Text which is a string #31

easyMaxi opened this issue Oct 30, 2015 · 4 comments
Assignees

Comments

@easyMaxi
Copy link

'appData.Text': 'object', 'appData.Text.size': 'required_if:appData.Text|integer',
is my schema.

Am I wrong at using the module this way? My idea is to check if appData.Text is there and an object. If it's an object I need the size attribute.

Error: Trying to redefine 'Text' which is a string at DotObject._fill (\node_modules\indicative\node_modules\dot-object\index.js:73:15) at DotObject._fill (\node_modules\indicative\node_modules\dot-object\index.js:79:10) at \node_modules\indicative\node_modules\dot-object\index.js:96:12 at Array.forEach (native) at DotObject.object (\node_modules\indicative\node_modules\dot-object\index.js:92:20) at Function.<anonymous> (\node_modules\indicative\node_modules\dot-object\index.js:59:31) at Validator.validate (\node_modules\indicative\lib\validator.js:133:11)

Beside this: Thanks for the module!

EDIT:
Ok, I could ask for appData.Text.size. This implicit appData.Text is a object.
Seams like a legit workaround for my case.

@thetutlage
Copy link
Member

Lemme reproduce this

@thetutlage
Copy link
Member

There are couple of issues here, first there is no object rule shipped with indicative. Next size property does not exists on a object.

You need to write custom validation rule for this, i added an example below

var objectWithLength = function (data, field, message, args) {
  return new Promise(function (resolve, reject) {

    var fieldValue = data[field]

    /**
     * skip if value does not exists, required validation
     * can take care of required fields
     */
    if(!fieldValue){
      resolve("skipping as value does not exists")
      return
    }

    /**
     * check if value is an object
     */
    if(typeof(fieldValue) === 'object') {

      if(Object.keys(fieldValue) && Object.keys(fieldValue).length > 0) {
        resolve("Validation passed")
        return
      }

    }

    reject(message)

  })
}

indicative.extend('objectWithLength', 'Field should be a valid object', objectWithLength)

and then use it as snake_case

var rules = {
  'appData.Text': 'object_with_length',
}

@thetutlage thetutlage self-assigned this Nov 1, 2015
@easyMaxi
Copy link
Author

easyMaxi commented Nov 3, 2015

Oh, my bad!
I don't know why but i was sure there must be a object rule 💃
Thanks for the example!

But I'm not sure what you mean with size property does not exists on a object.. size is an attribute on my JSON object. (This is just a minimal running example and not my real JSON code, but I see no problem with a size attribute)

@thetutlage
Copy link
Member

Ohh make sense that u have a size attribute. I believe custom rule will make things less complicated

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

No branches or pull requests

2 participants