How to use the ajv.ValidationError function in ajv

To help you get started, we’ve selected a few ajv 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 ditojs / dito / packages / server / src / schema / keywords / _validate.js View on Github external
async validate(func, ...args) {
    // The validator's `ctx` as passed to Ajv with passContext as `this`:
    const params = getParams(this, ...args)
    let result
    try {
      result = (await func(params)) ?? true
    } catch (error) {
      // Async validate methods need to throw their errors.
      throw new Ajv.ValidationError(getErrors(error, params))
    }
    return result
  }
}
github Open-EO / openeo-earthengine-driver / src / processgraph / jsonschema.js View on Github external
_validateGeoJson(data) {
		if (typeof data.type !== 'string') { // ToDo: Fully check against GeoJSON schema
			throw new ajv.ValidationError([{
				message: "Invalid GeoJSON specified (no type property)."
			}]);
		}
		return true;
	}
github Zooz / express-ajv-swagger-validation / src / customKeywords / files.js View on Github external
validate: function filesValidation(schema, data) {
        filesValidation.errors = [];
        const dataFileName = data.map((element) => { return element[fileNameField] });
        let missingFiles = missingElements(dataFileName, schema.required);
        if (missingFiles.length > 0) {
            filesValidation.errors.push(new Ajv.ValidationError({
                keyword: 'files',
                message: `Missing required files: ${missingFiles.toString()}`,
                params: { requiredFiles: schema.required, missingFiles: missingFiles }
            }));
            return false;
        }

        // Check that only the optional files exists
        let allFiles = schema.required.concat(schema.optional);
        let extraFiles = missingElements(allFiles, dataFileName);
        if (extraFiles.length > 0) {
            filesValidation.errors.push(new Ajv.ValidationError({
                keyword: 'files',
                message: `Extra files are not allowed. Not allowed files: ${extraFiles}`,
                params: { allowedFiles: allFiles, extraFiles: extraFiles }
            }));
github ditojs / dito / packages / server / src / app / Validator.js View on Github external
: function(data) {
          // Emulate `options.throw == true` behavior for sync validation:
          // Return `true` if successful, throw `Ajv.ValidationError` otherwise.
          // Use `call()` to pass `this` as context to Ajv, see passContext:
          const result = validator.call(this, data)
          if (!result) {
            throw new Ajv.ValidationError(validator.errors)
          }
          return result
        }
  }
github nick121212 / fx-schema-form / packages / fx-schema-form-react / libs / libs / dec.js View on Github external
timeId = setTimeout(() => {
                                updateItemMeta({
                                    parentKeys: options.parentKeys,
                                    keys: [],
                                    meta: root.value
                                });
                            }, 200);
                            if (Map.isMap(dataRaw) || List.isList(dataRaw)) {
                                dataRaw = dataRaw.toJS();
                            }
                            curAjv.errors = undefined;
                            if (!(yield validate(dataRaw))) {
                                if (!validate.errors) {
                                    validate.errors = [];
                                }
                                throw new ValidationError(validate.errors.concat(curAjv.errors || []));
                            }
                            root.value = root.value.merge({
                                isValid: true
                            });
                            updateItemMeta({
                                parentKeys: options.parentKeys,
                                keys: [],
                                meta: root.value
                            });
                        }
                        catch (e) {
                            if (!(e instanceof ValidationError)) {
                                return {
                                    isValid: false,
                                    errMsg: e.message
                                };
github Open-EO / openeo-earthengine-driver / src / processgraph / jsonschema.js View on Github external
_validateCollectionId(data) {
		if (this.context.getCollection(data) === null) {
			throw new ajv.ValidationError([{
				message: "Collection doesn't exist."
			}]);
		}
		return true;
	}
github Open-EO / openeo-earthengine-driver / src / processgraph / jsonschema.js View on Github external
_validateOutputFormat(data) {
		if (!this.context.server().isValidOutputFormat(data)) {
			throw new ajv.ValidationError([{
				message: "Output format not supported."
			}]);
		}
		return true;
	}
github Open-EO / openeo-earthengine-driver / src / processgraph / jsonschema.js View on Github external
async _validateProcessGraphId() {
		var pg = await this.context.getStoredProcessGraph(data);
		if (pg === null) {
			throw new ajv.ValidationError([{
				message: "Stored process graph doesn't exist."
			}]);
		}
		return true;
	}
github nick121212 / fx-schema-form / packages / fx-schema-form-extension / libs / demo / ajv / id.js View on Github external
setTimeout(() => {
            if (data === "nick" || data === "nora") {
                return resolve(true);
            }
            reject(new ajv.ValidationError([{ message: "idExists不是nick" }]));
        }, 2000);
    });
github Open-EO / openeo-earthengine-driver / src / processgraph / jsonschema.js View on Github external
async _validateJobId() {
		var job = await this.context.getJob(data);
		if (job === null) {
			throw new ajv.ValidationError([{
				message: "Job doesn't exist."
			}]);
		}
		return true;
	}