{"action":"created","issue":{"url":"https://api.github.com/repos/techfort/LokiJS/issues/593","repository_url":"https://api.github.com/repos/techfort/LokiJS","labels_url":"https://api.github.com/repos/techfort/LokiJS/issues/593/labels{/name}","comments_url":"https://api.github.com/repos/techfort/LokiJS/issues/593/comments","events_url":"https://api.github.com/repos/techfort/LokiJS/issues/593/events","html_url":"https://github.com/techfort/LokiJS/issues/593","id":247547429,"number":593,"title":"Quicky tip: adding JSON-Schema validators","user":{"login":"retorquere","id":132108,"avatar_url":"https://avatars0.githubusercontent.com/u/132108?v=4","gravatar_id":"","url":"https://api.github.com/users/retorquere","html_url":"https://github.com/retorquere","followers_url":"https://api.github.com/users/retorquere/followers","following_url":"https://api.github.com/users/retorquere/following{/other_user}","gists_url":"https://api.github.com/users/retorquere/gists{/gist_id}","starred_url":"https://api.github.com/users/retorquere/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/retorquere/subscriptions","organizations_url":"https://api.github.com/users/retorquere/orgs","repos_url":"https://api.github.com/users/retorquere/repos","events_url":"https://api.github.com/users/retorquere/events{/privacy}","received_events_url":"https://api.github.com/users/retorquere/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-08-02T23:02:16Z","updated_at":"2018-05-01T22:30:31Z","closed_at":null,"author_association":"NONE","body":"Not saying it needs to be added to LokiJS (I was contemplating adding it to the wiki, but I don't know whether outside contributions to the wiki are wanted), but here's a quick way to add JSON-Schema validation to LokiJS:\r\n\r\n```\r\nvar Ajv = require('ajv');\r\nvar Loki = require('lokijs');\r\nvar validator = new Ajv();\r\n\r\nLoki.prototype.addCollection = (function(original) {\r\n  return function(name, options) {\r\n    var coll;\r\n    coll = original.apply(this, arguments);\r\n    if (options.schema) {\r\n      coll.validate = validator.compile(options.schema);\r\n    }\r\n    return coll;\r\n  };\r\n})(Loki.prototype.addCollection);\r\n\r\nLoki.Collection.prototype.insert = (function(original) {\r\n  return function(doc) {\r\n    if (this.validate && !this.validate(doc)) {\r\n      throw this.validate.errors;\r\n    }\r\n    return original.apply(this, arguments);\r\n  };\r\n})(Loki.Collection.prototype.insert);\r\n\r\nLoki.Collection.prototype.update = (function(original) {\r\n  return function(doc) {\r\n    if (this.validate && !this.validate(doc)) {\r\n      throw this.validate.errors;\r\n    }\r\n    return original.apply(this, arguments);\r\n  };\r\n})(Loki.Collection.prototype.update);\r\n```"},"comment":{"url":"https://api.github.com/repos/techfort/LokiJS/issues/comments/385809889","html_url":"https://github.com/techfort/LokiJS/issues/593#issuecomment-385809889","issue_url":"https://api.github.com/repos/techfort/LokiJS/issues/593","id":385809889,"user":{"login":"Smolations","id":952681,"avatar_url":"https://avatars0.githubusercontent.com/u/952681?v=4","gravatar_id":"","url":"https://api.github.com/users/Smolations","html_url":"https://github.com/Smolations","followers_url":"https://api.github.com/users/Smolations/followers","following_url":"https://api.github.com/users/Smolations/following{/other_user}","gists_url":"https://api.github.com/users/Smolations/gists{/gist_id}","starred_url":"https://api.github.com/users/Smolations/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Smolations/subscriptions","organizations_url":"https://api.github.com/users/Smolations/orgs","repos_url":"https://api.github.com/users/Smolations/repos","events_url":"https://api.github.com/users/Smolations/events{/privacy}","received_events_url":"https://api.github.com/users/Smolations/received_events","type":"User","site_admin":false},"created_at":"2018-05-01T22:30:31Z","updated_at":"2018-05-01T22:30:31Z","author_association":"NONE","body":"This would be huge. As it stands, you can add almost any document to a collection (assuming no indices). I hope this gets considered, at least for v2!"}}
{"actor":{"display_login":"Smolations"}}