How to use ajv-keywords - 10 common examples

To help you get started, we’ve selected a few ajv-keywords examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github huridocs / uwazi / app / api / thesauris / validateThesauri.js View on Github external
import Ajv from 'ajv';
import ajvKeywords from 'ajv-keywords';
import model from './dictionariesModel';

const ajv = ajvKeywords(Ajv({ allErrors: true }), ['uniqueItemProperties']);

ajv.addKeyword('uniqueName', {
  async: true,
  // eslint-disable-next-line max-params
  validate: async (config, value, propertySchema, property, thesauri) => {
    const [duplicated] = await model.get({
      _id: { $ne: thesauri._id },
      name: new RegExp(`^${thesauri.name}$` || null, 'i')
    });

    if (duplicated) {
      return false;
    }
    return true;
  }
});
github balena-io-modules / rendition / src / components / Filters / SchemaSieve.ts View on Github external
import findIndex from 'lodash/findIndex';
import findKey from 'lodash/findKey';
import includes from 'lodash/includes';
import isArray from 'lodash/isArray';
import isBoolean from 'lodash/isBoolean';
import map from 'lodash/map';
import pickBy from 'lodash/pickBy';
import reduce from 'lodash/reduce';
import startsWith from 'lodash/startsWith';
import trimStart from 'lodash/trimStart';
import { FilterSignature } from '.';
import * as utils from '../../utils';
import { getDataModel } from '../DataTypes';

const ajv = new Ajv();
ajvKeywords(ajv, ['regexp', 'formatMaximum', 'formatMinimum']);
ajv.addMetaSchema(metaSchema6);

export const FULL_TEXT_SLUG = 'full_text_search';
const DEFAULT_DELIMITER = '___';

export const filter = (
	filters: JSONSchema6 | JSONSchema6[],
	collection: any[],
) => {
	// Remove all schemas that may have been compiled already
	ajv.removeSchema(/^.*$/);

	const validators = isArray(filters)
		? filters.map(s => ajv.compile(s))
		: [ajv.compile(filters)];
github gajus / babel-plugin-react-css-modules / src / index.js View on Github external
import optionsSchema from './schemas/optionsSchema.json';
import optionsDefaults from './schemas/optionsDefaults';
import createObjectExpression from './createObjectExpression';
import requireCssModule from './requireCssModule';
import resolveStringLiteral from './resolveStringLiteral';
import replaceJsxExpressionContainer from './replaceJsxExpressionContainer';
import attributeNameExists from './attributeNameExists';
import createSpreadMapper from './createSpreadMapper';
import handleSpreadClassName from './handleSpreadClassName';

const ajv = new Ajv({
  // eslint-disable-next-line id-match
  $data: true
});

ajvKeywords(ajv);

const validate = ajv.compile(optionsSchema);

export default ({
  types: t
}: {
  types: BabelTypes
}) => {
  const filenameMap = {};

  let skip = false;

  const setupFileForRuntimeResolution = (path, filename) => {
    const programPath = path.findParent((parentPath) => {
      return parentPath.isProgram();
    });
github ArkEcosystem / core / packages / crypto / src / validation / keywords.ts View on Github external
const bignumber = (ajv: Ajv) => {
    const instanceOf = ajvKeywords.get("instanceof").definition;
    instanceOf.CONSTRUCTORS.BigNumber = BigNumber;

    ajv.addKeyword("bignumber", {
        compile(schema) {
            return (data, dataPath, parentObject: any, property) => {
                const minimum = typeof schema.minimum !== "undefined" ? schema.minimum : 0;
                const maximum = typeof schema.maximum !== "undefined" ? schema.maximum : "9223372036854775807"; // 8 byte maximum

                const bignum = BigNumber.make(data);

                if (!bignum.isInteger()) {
                    return false;
                }

                let bypassGenesis: boolean = false;
                if (schema.bypassGenesis) {
github gajus / table / test / streamConfig.js View on Github external
before(() => {
    const ajv = new Ajv({
      allErrors: true
    });

    ajv.addMetaSchema(ajvSchemaDraft06);

    ajvKeywords(ajv, 'typeof');

    validate = ajv.compile(configSchema);
  });
github oors / oors / packages / oors-ajv / src / index.js View on Github external
initialize() {
    const ajv = new Ajv({
      allErrors: true,
      async: 'es7',
      coerceTypes: 'array',
      useDefaults: true,
    });

    ajvKeywords(ajv, 'instanceof');
    ajv.addKeyword('isObjectId', idValidator);
    this.ajv = ajv;
    this.manager.ajv = ajv;

    this.onModule('oors.mongodb', 'repository', ({ repository }) => {
      if (!repository.schema || !test(v.isJSONSchema())(repository.schema)) {
        return;
      }

      const isValid = this.ajv.compile(repository.schema);

      Object.assign(repository, {
        isValid,
        validate: (data = {}) => {
          if (!isValid(data)) {
            throw new ValidationError(isValid.errors);
github zerobias / telegram-mtproto / src / service / main / config-validation.js View on Github external
additionalProperties: false
}

const schema = {
  properties: {
    app,
    api,
    server,
    schema  : type.obj,
    mtSchema: type.obj
  },
  additionalProperties: false
}

const ajv = new Ajv()
AjvKeys(ajv)
const validate = ajv.compile(schema)

const configValidator = config => {
  const valid = validate(config)
  if (!valid) {
    console.log('config errors')
    validate.errors.map(printObj)
    throw new Error('wrong config fields')
  }
}

const canDir = propIs(Function, 'dir', console)
const printObj = canDir
  ? (arg) => console.dir( arg, { colors: true })
  : (arg) => console.log(arg)
github oors / oors / packages / oors-ajv / src / Module.js View on Github external
import Ajv from 'ajv';
import ajvKeywords from 'ajv-keywords';
import { Module } from 'oors';
import ValidationError from './ValidationError';

const ajv = new Ajv({
  allErrors: true,
  async: 'es7',
  coerceTypes: 'array',
  useDefaults: true,
});

ajvKeywords(ajv, 'instanceof');

export default class extends Module {
  static validateConfig(config, configSchema) {
    const schema = configSchema || this.schema;
    if (!schema) {
      return config;
    }

    const validate = ajv.compile({
      type: 'object',
      ...schema,
      properties: {
        enabled: {
          type: 'boolean',
          default: true,
        },
github ArkEcosystem / core / packages / crypto / src / validation / ajv-wrapper.ts View on Github external
constructor() {
        const ajv = new Ajv({ $data: true, schemas, removeAdditional: true, extendRefs: true });
        ajvKeywords(ajv);

        keywords.forEach(addKeyword => {
            addKeyword(ajv);
        });

        formats.forEach(addFormat => {
            addFormat(ajv);
        });

        this.ajv = ajv;
    }
github swagger-api / swagger-editor / src / plugins / json-schema-validator / validator / index.js View on Github external
constructor() {
    this.ajv = new Ajv({
      allErrors: true,
      jsonPointers: true,
    })

    AjvKeywords(this.ajv, "switch")
    AjvErrors(this.ajv)

    this.addSchema(jsonSchema)
  }

ajv-keywords

Additional JSON-Schema keywords for Ajv JSON validator

MIT
Latest version published 2 years ago

Package Health Score

70 / 100
Full package analysis