Skip to content

JSON Schemas and OpenAPI specs for the SafetyCulture Public API

License

Notifications You must be signed in to change notification settings

SafetyCulture/api-json-schemas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SafetyCulture API JSON schemas

JSON schemas of data structures exposed by the SafetyCulture public API.

They are useful e.g. to integrators wishing to validate data exported by the SafetyCulture API.

For more details see the SafetyCulture developer portal.

Usage

npm install @safetyculture/api-json-schemas
const safetyCultureApiJsonSchemas = require('@safetyculture/api-json-schemas');

// Outputs the audit public JSON schema
console.log(safetyCultureApiJsonSchemas.audit);

// Outputs common definition schemas used in the audit public JSON schema
console.log(safetyCultureApiJsonSchemas.definitions);

// Outputs the geometry schema used to represent audit location properties
console.log(safetyCultureApiJsonSchemas.geometry);

Validation example

Add a file to your project that exposes a validator like this:

const Validator = require('jsonschema').Validator;
const publicApiJsonSchema = require('@safetyculture/api-json-schemas');

function createValidator() {
  const validator = new Validator();

  const SUB_SCHEMAS = [
    publicApiJsonSchema.definitions,
    publicApiJsonSchema.geometry
  ];

  SUB_SCHEMAS.forEach((subSchema) => {
    validator.addSchema(subSchema);
  });

  return validator;
}

/**
 * Checks if 'doc' is in a valid API audit JSON format.
 *
 * @param doc {Object} to check
 * @param options {Object} see json-schema.org for configuration options
 * @return {ValidatorResult} the result of validation against the schema (see the jsonschema NPM module for details)
 */
module.exports = function validate(doc, options) {
  if (!doc || typeof doc !== 'object') {
    throw new Error('Input to API schema validation function is not an object');
  }

  const validator = createValidator();
  return validator.validate(doc, publicApiJsonSchema.audit, options);
};

About

JSON Schemas and OpenAPI specs for the SafetyCulture Public API

Resources

License

Security policy

Stars

Watchers

Forks

Packages