Validation of fields before upsertion


#1

Is it possible to check that the fields of an instance of a type respect some sophisticated constraints? (i.e. more complicated than the constraints already available in the Type System)

For example I would like to check that the value of the field MyType.stringField in not in the field MyType.stringsListField. As far as I know, we cannot enforce that with the Type System syntax. What I would like is a member function MyType.validate() to override, in which I would check that my constraint is respected, and that function would be called before upserting the instance.

If I remember correctly, we had callbacks like beforeCreate or beforeUpsert previously, but I do not see them anymore in the documentation of Persistable… (I am using v.7.8.2)
I see that there is a function Obj.validateObj() but if I override it, I will remove the existing validation enforced by the Type System, won’t I?


#2

We still have those callbacks, I tested on 7.8.2.50-1:

> Persistable.beforeCreate
ƒ anonymous(objs
) {
  if (arguments.length > 1)
    throw new C3.typesys.Error('Function "beforeCreate" on Persistable takes one argument.', "InvalidInputParam");
  return this._call("beforeCreate", {…

#3

Yes the Persistable.before* functions can be used to perform complex validation before data is created/updated.

(you may have to view ‘private’ fields in console to see the docs for these functions)


#4

Can’t you achieve what you want with an enum type?


#5

Those before… functions are not private and they do show up in c3ShowType(Persistable)


#6

Indeed… My eyes were probably malfunctioning… ^^’


#7

I don’t think so. I want to ensure that the value of one field is NOT in a collection which is the value of another field. These values can be anything, not a list of predefined values (so not an enum).

Anyway the question can be useful for other kind of custom validations.