Skip to content

Instantly share code, notes, and snippets.

@lemire
Created March 27, 2026 20:52
Show Gist options
  • Select an option

  • Save lemire/a027f8c401d6b32dbb356c5b76160dbf to your computer and use it in GitHub Desktop.

Select an option

Save lemire/a027f8c401d6b32dbb356c5b76160dbf to your computer and use it in GitHub Desktop.
const { Validator } = require('ata-validator');
// Define a JSON schema for a user object
const userSchema = {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
email: { type: 'string', format: 'email' },
age: { type: 'integer', minimum: 0 },
role: { type: 'string', default: 'user' }
},
required: ['name', 'email']
};
// Create a validator instance
const validator = new Validator(userSchema);
// Example data
const validUser = { name: 'John Doe', email: 'john@example.com', age: 30 };
const invalidUser = { name: '', email: 'invalid-email' };
// Validate objects
console.log('Valid user isValidObject:', validator.isValidObject(validUser)); // true
console.log('Invalid user isValidObject:', validator.isValidObject(invalidUser)); // false
// Full validation with details
const resultValid = validator.validate(validUser);
console.log('Valid user validation result:', resultValid); // { valid: true, data: {...} }
const resultInvalid = validator.validate(invalidUser);
console.log('Invalid user validation result:', resultInvalid); // { valid: false, errors: [...] }
// Validate JSON string
const jsonString = '{"name": "Jane Doe", "email": "jane@example.com"}';
console.log('JSON string isValidJSON:', validator.isValidJSON(jsonString)); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment