orbitjs / ember-orbit

Ember.js data layer built with Orbit.js
MIT License
320 stars 42 forks source link

Upgrading to 0.17 with custom serializers #392

Closed lucacorti closed 2 years ago

lucacorti commented 2 years ago

Hi,

I'm trying to upgrade to 0.17 and am encountering issues with validators. I have a custom serializer on my remote JSON:API data source:

export default {
  create(injections = {}) {
    applyStandardSourceInjections(injections);
    injections.name = 'remote';
    injections.host = config.api.host;
    injections.namespace = config.api.namespace;
    injections.URLBuilderClass = MyURLBuilder;
    injections.serializerClassFor = buildSerializerClassFor({
      timerange: TimerangeSerializer,
    });
    return new SourceClass(injections);
  },
};

The serializer appears to work, but syncing to the local store fails:

[source-event] store syncFail {
  operations: [ { op: 'updateRecord', record: [Object] } ],
  options: undefined,
  id: '1f336de6-7973-4dfc-9aa4-4a175e27df44'
} ValidationError: Validation failed
- record operation is invalid
  - validator has not been provided for attribute 'duration' of `type` 'timerange'
  - validator has not been provided for attribute 'saturday' of `type` 'timerange'
  - validator has not been provided for attribute 'sunday' of `type` 'timerange'
    at SyncSchemaValidationProcessor.validate (webpack://__ember_auto_import__/./node_modules/@orbit/record-cache/dist/modules/operation-processors/sync-schema-validation-processor.js?:11:672)
    at MemoryCache._applyTransformOperation (webpack://__ember_auto_import__/./node_modules/@orbit/record-cache/dist/modules/sync-record-cache.js?:28:3108)
    at MemoryCache._applyTransformOperations (webpack://__ember_auto_import__/./node_modules/@orbit/record-cache/dist/modules/sync-record-cache.js?:28:2739)
    at MemoryCache._update (webpack://__ember_auto_import__/./node_modules/@orbit/record-cache/dist/modules/sync-record-cache.js?:28:1396)
    at MemoryCache._update (webpack://__ember_auto_import__/./node_modules/@orbit/memory/dist/modules/memory-cache.js?:44:206)
    at MemoryCache.update (webpack://__ember_auto_import__/./node_modules/@orbit/record-cache/dist/modules/sync-record-cache.js?:18:3299)
    at MemorySource._applyTransform (webpack://__ember_auto_import__/./node_modules/@orbit/memory/dist/modules/memory-source.js?:65:360)
    at MemorySource._sync (webpack://__ember_auto_import__/./node_modules/@orbit/memory/dist/modules/memory-source.js?:13:75)
    at MemorySource.proto.__sync__ (webpack://__ember_auto_import__/./node_modules/@orbit/data/dist/modules/source-interfaces/syncable.js?:21:997)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  issues: [
    {
      validator: 'recordOperation',
      validation: 'operationValid',
      ref: [Object],
      details: [Array],
      description: 'record operation is invalid\n' +
        "- validator has not been provided for attribute 'duration' of `type` 'timerange'\n" +
        "- validator has not been provided for attribute 'saturday' of `type` 'timerange'\n" +
        "- validator has not been provided for attribute 'sunday' of `type` 'timerange'"
    }
  ]
}

Release notes for 0.17 mention validators can be specified, however I was not able to work out how. Also, apparently validation fails when syncing to the store, which is provided by e-o.

dgeb commented 2 years ago

Hi @lucacorti - thanks for reporting the lack of docs related to customizing validators. This should be addressed by #393, which updates the README to include a section that describes overriding the data-validator service to include custom validators. Let me know if you have any questions or problems.