How to use the validate function from tv4

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

946
947
948
949
950
951
952
953
954
955
    const data = fs.readFileSync(file, "utf8");
    if (configRe.test(data)) {
      const config = data.replace(configRe, "$1");
      t.true(
        // @ts-ignore
        tv4.validate(JSON.parse(config), configSchemaStrict),
        file + "\n" + JSON.stringify(tv4.error, null, 2));
    }
  }
});
fork icon612
star icon0
watch icon56

+ 389 other calls in file

930
931
932
933
934
935
936
937
938
939
      // eslint-disable-next-line unicorn/prefer-json-parse-buffer
      "utf8"
    );
    t.true(
      // @ts-ignore
      tv4.validate(JSON.parse(data), configSchemaStrict),
      file + "\n" + JSON.stringify(tv4.error, null, 2));
  }
});

fork icon611
star icon0
watch icon55

+ 191 other calls in file

309
310
311
312
313
314
315
316
317
};
var data1 = [true, false];
var data2 = [true, 123];

alert("data 1: " + tv4.validate(data1, schema)); // true
alert("data 2: " + tv4.validate(data2, schema)); // false
alert("data 2 error: " + JSON.stringify(tv4.error, null, 4));
</pre>
</div>
fork icon190
star icon0
watch icon2

+ 31 other calls in file

6
7
8
9
10
11
12
13
14
There is support for `$ref` with JSON Pointer fragment paths (```other-schema.json#/properties/myKey```).

## Usage 1: Simple validation

```javascript
var valid = tv4.validate(data, schema);
```

If validation returns ```false```, then an explanation of why validation failed can be found in ```tv4.error```.
fork icon190
star icon0
watch icon13

+ 15 other calls in file

30
31
32
33
34
35
36
37
38
39
    return collection[method].apply(collection, args)
  })
}

validate (doc) {
  return tv4.validate(doc, this.schema)
}

giveCursorBack (method, args) {
  return new Cursor(this._collectionPromise, method, args)
fork icon0
star icon4
watch icon2