Get started with ajv CDN

MIT licensed

AJV is a library for validating JSON data against given JSON schemas.

Tags:
  • JSON
  • schema
  • validator

Stable version

Copied!

How to start using ajv CDN


// Include the AJV library from the CDN
const Ajv = (() => {
  const script = document.createElement('script');
  script.src = 'https://cdn.cdnhub.io/ajv/8.12.0/ajv7.min.js';
  document.head.appendChild(script);

  return new Promise(resolve => {
    script.onload = () => {
      document.head.removeChild(script);
      resolve(Ajv);
    };
  });
})();

// Define the JSON schema
const schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number' },
  },
  required: ['name'],
};

// Create an instance of AJV
const validate = new Ajv().compile(schema);

// Validate an object against the schema
const data = { name: 'John Doe', age: 30 };
const valid = validate(data);

if (valid) {
  console.log('Valid data:', data);
} else {
  console.log('Invalid data:', data, validate.errors);
}
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!

All versions

3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.4.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.8.0 3.8.1 3.8.10 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.10.0 4.10.1 4.10.2 4.10.3 4.10.4 4.11.0 4.11.1 4.11.2 4.11.3 4.11.4 4.11.5 4.11.6 4.11.7 4.11.8 4.2.0 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.8.0 4.8.1 4.8.2 4.9.0 4.9.1 4.9.2 4.9.3 5.0.0 5.0.0-beta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