reTHINK-project / core-framework

The main goal of WP3 is to provide the reTHINK core framework comprised by the runtime environment where Hyperties are executed and the messaging nodes used to support messages exchange between Hyperties.
Apache License 2.0
1 stars 0 forks source link

Validating data against a JSON schema #128

Open acheambe opened 8 years ago

acheambe commented 8 years ago

On our last telco for the Service Framework, was a proposal for an agile procedure of working between specification from WP2 and implementation on WP3.

In other words to have the option to automate the changes carried out on JSON schemas to be reflected on implementations without huge efforts.

Mechanisms for this needs to be investigated and it is not exactly clear at what point this should be done. Any proposals?

shumy commented 8 years ago

I have many doubts about the usefulness of the Massage Factory. We can use methods to build messages that conform with the schema, but we still have a problem to validate the received messages! I have different and simpler suggestion to build this. We can have a method, something like validate(schema: JsonSchema, obj: Json): [throw error if failed] and use it like:

//in a config file
Validator.debug = true;

//if Validator.debug === false -> don't load
Validator.loadSchema(<message-schema-url>);

//normal use........................................................
let msg = {
  from: x, to: y,
  body: { ... }
}

//throw error if validation fails, if Validator.debug === false -> does nothing
Validator.validate(<message-schema-url>, msg);

It's a simple implementation that has the following advantages:

  1. No hard dependency on a complex API
  2. It's able to validate created and received messages
  3. Validate other structures, not just messages, without a need to add another method
  4. Schemas should be static and stable in production, disable the overhead of schema validation. This will be very important for the ShyncherManager, I can't validate every change of the SyncObject in production, it will be overkill.
acheambe commented 8 years ago

Thx Micael. you are touching two points which i am not sure are related to each other? message factory and validator of schema. Are you saying there is no need for the MessageFactory?

acheambe commented 8 years ago

I agree that it is an overkill to validate the Objects all the time. So the question i guess is when to carry out this validation?

shumy commented 8 years ago

Yes, I think there is no need for the MessageFactory or any structure specific factory (just my opinion). Just create the data has any JSON object and validate if it's in agreement with the schema. But we may need other opinions about this.

acheambe commented 8 years ago

That will be a quick approach to handle it. For a small project maybe, but i guess for a project this size, we need to structure things a little bit with these factories. Yes, interested in other opinions.

emmelmann-fokus commented 8 years ago

Mhm,

not sure if I got the entire technical view, but my feeling is that having these factories is better for so many partners working on the project / code. It allows to have a single point of verification upon creation and should also allow to provide proper conversion methods to generate a data object from, e.g. an optimized representation received over interfaces from other components.

pchainho commented 8 years ago

I also think factories are useful to maintain the full project compliant with data models (minimise impact of changing the data models) and also because it provides an API that should abstract as much as possible the data models. But I like @shumy proposal to only use the "validator" when needed, by using some configuration flag, something similar to log4j but for schema validation.

And @shumy also raised an important issue: the framework should also provide a message parser. What do you think?

acheambe commented 8 years ago

@pchainho message parser? on this thread? Didn't see that. But anyways, the factory will offer a parser. Which in effect is provided automatically in JavaScript through the JSON API. JSON.parse and JSON.stringify. @shumy correct me if i am wrong

shumy commented 8 years ago

No parser needed (this is automatically done in JS), just validation of the received message.

acheambe commented 8 years ago

Thought so too. Okay

pchainho commented 8 years ago

ah ok, I was not aware of that.

pchainho commented 8 years ago

Another JSON schema validator tool:

https://github.com/garycourt/JSV

acheambe commented 8 years ago

This one is based on ECMAScript 3 and last commit was 3 years ago