jaystack / jaydata

Notice: this library isn't maintained anymore
http://jaydata.org
GNU General Public License v2.0
352 stars 94 forks source link

jaydata maps an empty string to null #276

Open bizlina opened 8 years ago

bizlina commented 8 years ago

Hi,

I've used JaySvcUtil to generate the entity model out of my oData service. The description is correct generated. I have multiple string keys for one Entity. A oData call for that Entity gives me, for example, the following result (the yellow marked properties are the key columns):

image

One of the key columns (THEME) contains an empty string. After mapping with jaydata the empty string is transformed to "null". This gives me an valildation error because the key filed is required and seems to be null. See the following screenshot. image

How can I resolve this issue? Is there any possibility to tell jaydata that an empty string is okay for the a field that is defined as "Edm.String"?

ysmoradi commented 8 years ago

I've no problem with this default behavior, but if you have a problem with that, you can do something like this:

                var originalRequired = $data.Validation.EntityValidation.prototype.supportedValidations["$data.Array"].required;

                $data.Validation.EntityValidation.prototype.supportedValidations["$data.Array"].required = function required(value, definedValue) {
                    return originalRequired.apply(this, arguments) && value.length != 0;
                }

The original required of jaydata for arrays is (array != null) condition, but I think that empty array should results in a required error too, so I've rewritten that with a code I've provided to you (-:

bizlina commented 8 years ago

Thanks for your answer. If i replace "$data.Array" with "$data.String" then I can stop for all key fields with type "Edm.String". So far so good. But at this point my key filed has already the value "null".

Is there a possibility to step in earlier, for example, at the point of mapping from odata to jaydata?

bizlina commented 8 years ago

In the meantime I found out, that the "problem" is in the saveChanges() method of EntityContext.js. This is an excerpt of the saveChanges-method: image

In the variable "memDef" in line 1387 the field is still correct filled with an empty string. In the following secquence the control flow get into the if-condition and with String as value for "memDefType" in the switch-case. In line 1404 my property get's assigned to null. As you can see here: image

The empty string is technical correct and I need to display this empty string in my application. Is there a similar way to override the default value as @ymoradi provided in his answer for the validation?