How to use the compile function from ajv

Find comprehensive JavaScript ajv.compile code examples handpicked from public code repositorys.

ajv.compile compiles a JSON schema into a validation function.

363
364
365
366
367
368
369
370
371
372
373
  const schema = datasetUtils.jsonSchema(dataset.schema.filter(p => !p['x-calculated'] && !p['x-extension']))
  schema.additionalProperties = false
  schema.properties._id = { type: 'string' }
  // super-admins can set _updatedAt and so rewrite history
  if (adminMode) schema.properties._updatedAt = { type: 'string', format: 'date-time' }
  return ajv.compile(schema)
}


async function manageAttachment (req, keepExisting) {
  if (req.is('multipart/form-data')) {
fork icon6
star icon26
watch icon4

+ 13 other calls in file

36
37
38
39
40
41
42
43
44
45
const outputs = require('../utils/outputs')
const limits = require('../utils/limits')
const locks = require('../utils/locks')
const notifications = require('../utils/notifications')
const datasetPatchSchema = require('../../contract/dataset-patch')
const validatePatch = ajv.compile(datasetPatchSchema)
const datasetPostSchema = require('../../contract/dataset-post')
const validatePost = ajv.compile(datasetPostSchema.properties.body)
const userNotificationSchema = require('../../contract/user-notification')
const validateUserNotification = ajv.compile(userNotificationSchema)
fork icon6
star icon26
watch icon4

+ 35 other calls in file

How does ajv.compile work?

ajv.compile is a function that takes a JSON schema and compiles it into a validation function that can be used to validate data against that schema. The resulting validation function can be called with a data object as its argument, and will return a boolean indicating whether or not the data is valid according to the schema. The compiled validation function is optimized for performance, so it can be used to efficiently validate large amounts of data.

27
28
29
30
31
32
33
34
35
36
37
    publicationSites: settingSchema.properties.publicationSites,
    webhooks: settingSchema.properties.webhooks
  }
}
const validateSettings = ajv.compile(settingSchema)
const validateDepartmentSettings = ajv.compile(departmentSettingsSchema)


const validate = (settings) => {
  if (settings.department) {
    validateDepartmentSettings(settings)
fork icon6
star icon26
watch icon4

+ 23 other calls in file

12
13
14
15
16
17
18
19
20
21
const remoteServiceAPIDocs = require('../../contract/remote-service-api-docs')
const mongoEscape = require('mongo-escape')
const config = require('config')
const ajv = require('ajv')()
const createError = require('http-errors')
const validate = ajv.compile(require('../../contract/remote-service'))
const servicePatch = require('../../contract/remote-service-patch')
const validatePatch = ajv.compile(servicePatch)
const openApiSchema = require('../../contract/openapi-3.1.json')
openApiSchema.$id = openApiSchema.$id + '-2' // dirty hack to handle ajv error
fork icon6
star icon25
watch icon0

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const Ajv = require("ajv");
const ajv = new Ajv();

const schema = {
  type: "object",
  properties: {
    name: {
      type: "string",
      maxLength: 30,
    },
    age: {
      type: "number",
      minimum: 18,
    },
  },
  required: ["name", "age"],
  additionalProperties: false,
};

const validate = ajv.compile(schema);

const data1 = { name: "John", age: 25 };
const data2 = { name: "Alice", age: "twenty-five" };

console.log(validate(data1)); // true
console.log(validate(data2)); // false

In this example, ajv.compile() is used to create a validation function called validate based on the JSON schema defined in the schema object. This function can then be used to validate data against the schema.

32
33
34
35
36
37
38
39
40
41

//____________________________________________________________________________

console.log('Validating v'+status.version+' release '+status.status.release);

var validate = ajv.compile(jsonSchema);
validate(schema);
var errors = validate.errors;
if (errors) {
    console.warn(errors);
fork icon2
star icon22
watch icon4

+ 3 other calls in file

23
24
25
26
27
28
29
30
31
32
return new Promise(function (resolve, reject) {
        if (!payload) {
                reject(new error.InternalValidationError('Payload is falsy'));
        } else {
                try {
                        let validate = ajv.compile(schema);

                        let valid = validate(payload);
                        if (valid && !validate.errors) {
                                resolve(_.cloneDeep(payload));
fork icon0
star icon8
watch icon1

13
14
15
16
17
18
19
20
21
22
  },
  required            : ['key_name'],
  additionalProperties: false
};

const validate = ajv.compile(schema);

/**
 * Get the value of key. If the key does not exist the special value nil is returned
 * @param params
fork icon0
star icon1
watch icon2