How to use the validateResult function from tv4

Find comprehensive JavaScript tv4.validateResult code examples handpicked from public code repositorys.

37
38
39
40
41
42
43
44
45
46
## Usage 2: Multi-threaded validation

Storing the error and missing schemas does not work well in multi-threaded environments, so there is an alternative syntax:

```javascript
var result = tv4.validateResult(data, schema);
```

The result will look something like:
```json
fork icon190
star icon0
watch icon34

+ 3 other calls in file

50
51
52
53
54
55
56
57
58
const [schemaVersion] = [['draftV4', metaSchemaV4]].find(
  ([_, metaSchema]) => {
    tv4.reset();
    tv4.addSchema('', metaSchema);
    tv4.addSchema(metaSchema.$schema, metaSchema);
    const validationResult = tv4.validateResult(jsonSchema, metaSchema);
    return validationResult.valid;
  }
) || [null, null];
fork icon22
star icon94
watch icon16

+ 5 other calls in file

27
28
29
30
31
32
33
34
35
36
schemas.forEach((schemaFile) => {
  const schema = getSchema(schemaFile);
  tv4.addSchema(schema.id, schema);
  describe(`${schemaFile} (${schema.title || schema.description})`, () => {
    it('should validate against JSON meta schema', () => {
      const result = tv4.validateResult(schema, 'http://json-schema.org/draft-04/schema');
      chai.expect(result.valid).to.equal(true);
    });
  });
});
fork icon4
star icon32
watch icon4

11
12
13
14
15
16
17
18
19
20
        return new SchemaObject(opts);
}
//if(!schema) { throw new TypeError("schema not set"); }

// Validate using schema
//var result = tv4.validateResult(opts, schema);
//if(!result.valid) { throw new TypeError("Options are not valid"); }

//this._schema = schema;
this._data = opts;
fork icon1
star icon14
watch icon10

+ 5 other calls in file

8
9
10
11
12
13
14
15
16
17
        type: { enum: ['string-line', 'string-multiline', 'string-html', 'string-markdown'] },
        value: { type: 'string' },
      },
      options
    )
    return tv4.validateResult(entry, schema)
  }
}

module.exports = { StringEntryType }
fork icon9
star icon13
watch icon2

59
60
61
62
63
64
65
66
67
68
    throw new Error('Validator.validate(data) unrecognized type "' + data.type + '"');
}

var deferred = Q.defer();
_loadSchemas();
var result = tv4.validateResult(data, schemas[data.type]);
if (result.error) {
    deferred.reject(result.error.message + ':' + result.error.dataPath);
}
else {
fork icon0
star icon5
watch icon8

222
223
224
225
226
227
228
229
230
231
    throw new Error(
        "Unknown schema reference: " + JSON.stringify(schema['$ref'])
    );
}

var result = tv4.validateResult(obj, schema);

if (result.valid) return;

if (result.missing.length > 0) {
fork icon16
star icon3
watch icon22

+ 3 other calls in file

83
84
85
86
87
88
89
90
91
92
  }
}

log.trace('Validating', data, 'with', JSON.stringify(schema));

var result = tv4.validateResult(data, schema, true, true);

if (result.valid) {
  log.trace('Data passed validation');
  return null;
fork icon6
star icon2
watch icon14

38
39
40
41
42
43
44
45
46
47
  body = (body == '' ? '[empty]' : body);
  assert.doesNotThrow(function () {
    JSON.parse(body);
  }, JSON.SyntaxError, "Invalid JSON: " + body);
  var json = JSON.parse(body);
  var result = tv4.validateResult(json, schema);
  assert.lengthOf(result.missing, 0, "Missing/unresolved JSON schema $refs (" + result.missing && result.missing.join(', ') + ") in schema: " + JSON.stringify(schema, null, 4) + " Error");
  assert.ok(result.valid, "Got unexpected response body: " + result.error && result.error.message + " " + JSON.stringify(schema, null, 4) + " Error");
  
}
fork icon1
star icon0
watch icon16

+ 5 other calls in file