How to use the validateResult function from tv4
Find comprehensive JavaScript tv4.validateResult code examples handpicked from public code repositorys.
GitHub: geraintluff/tv4
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
190
0
34
+ 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];
22
94
16
+ 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); }); }); });
4
32
4
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;
1
14
10
+ 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 }
9
13
2
GitHub: CARLI/select
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 {
0
5
8
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) {
16
3
22
+ 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;
6
2
14
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"); }
1
0
16
+ 5 other calls in file
tv4.validate is the most popular function in tv4 (982 examples)