How to use the fast-json-patch.validate function in fast-json-patch

To help you get started, we’ve selected a few fast-json-patch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Asymmetrik / node-fhir-server-mongo / src / services / patient / patient.service.js View on Github external
collection.findOne({ id: id.toString() }, (err, data) => {
		if (err) {
			logger.error('Error with Patient.searchById: ', err);
			return reject(err);
		}

		// Validate the patch
		let errors = jsonpatch.validate(patchContent, data);
		if (errors && Object.keys(errors).length > 0) {
			logger.error('Error with patch contents');
			return reject(errors);
		}
		// Make the changes indicated in the patch
		let resource = jsonpatch.applyPatch(data, patchContent).newDocument;

		let Patient = getPatient(base_version);
		let patient = new Patient(resource);

		if (data && data.meta) {
			let foundPatient = new Patient(data);
			let meta = foundPatient.meta;
			meta.versionId = `${parseInt(foundPatient.meta.versionId) + 1}`;
			patient.meta = meta;
		} else {
github DefinitelyTyped / DefinitelyTyped / types / fast-json-patch / fast-json-patch-tests.ts View on Github external
jsonpatch.apply(myobj, patches);

var myobj2 = { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [{ number: "555-123" }] } };
var observer = jsonpatch.observe(myobj2);
myobj2.firstName = "Albert";
myobj2.contactDetails.phoneNumbers[0].number = "123";
myobj2.contactDetails.phoneNumbers.push({ number: "456" });
var patches2 = jsonpatch.generate(observer);

var objA = { user: { firstName: "Albert", lastName: "Einstein" } };
var objB = { user: { firstName: "Albert", lastName: "Collins" } };
var diff = jsonpatch.compare(objA, objB);

var obj = { user: { firstName: "Albert" } };
var patches3 = [{ op: "replace", path: "/user/firstName", value: "Albert" }, { op: "replace", path: "/user/lastName", value: "Einstein" }];
var errors = jsonpatch.validate(patches, obj);
if (errors.length === 0) {
  //there are no errors!
} else {
  for (var i = 0; i < errors.length; i++) {
    if (!errors[i]) {
      console.log("Valid patch at index", i, patches[i]);
    } else {
      console.error("Invalid patch at index", i, errors[i], patches[i]);
    }
  }
}
github geeklearningio / gl-vsts-tasks-file-patch / Common / Node / jsonPatcher.ts View on Github external
apply(content: string): string {
    var json = this.parse(content);
    var patchError = jsonPatch.validate(this.patches, json);

    if (patchError) {
      tl.warning(
        'Invalid patch at index `' +
          String(patchError.index) +
          '`' +
          '\n' +
          SlickPatchParser.stringify(patchError.operation) +
          '\n' +
          patchError.name +
          '\n' +
          patchError.message
      );
      throw new Error(
        'Invalid patch at index `' +
          String(patchError.index) +