@cypress/schema-tools
Advanced tools
Comparing version 1.7.2 to 1.8.0
@@ -77,29 +77,35 @@ "use strict"; | ||
}; | ||
exports.validateBySchema = function (schema, formats) { return function (object) { | ||
var validate = is_my_json_valid_1.default(schema, { formats: formats }); | ||
if (validate(object)) { | ||
return true; | ||
} | ||
var uniqueErrors = ramda_1.uniqBy(errorToString, validate.errors); | ||
if (includesDataHasAdditionalPropertiesError(uniqueErrors) && | ||
ramda_1.keys(schema.properties).length) { | ||
var hasData = findDataHasAdditionalProperties(uniqueErrors); | ||
var additionalProperties = ramda_1.difference(ramda_1.keys(object), ramda_1.keys(schema.properties)); | ||
hasData.message += ': ' + additionalProperties.join(', '); | ||
} | ||
var errors = ramda_1.uniq(errorsToStrings(uniqueErrors)); | ||
return errors; | ||
}; }; | ||
exports.validate = function (schemas, formats) { return function (schemaName, version) { return function (object) { | ||
schemaName = utils.normalizeName(schemaName); | ||
var namedSchemas = schemas[schemaName]; | ||
if (!namedSchemas) { | ||
return ["Missing schema " + schemaName]; | ||
} | ||
var aSchema = namedSchemas[version]; | ||
if (!aSchema) { | ||
return ["Missing schema " + schemaName + "@" + version]; | ||
} | ||
return exports.validateBySchema(aSchema.schema, formats)(object); | ||
}; }; }; | ||
exports.validateBySchema = function (schema, formats, greedy) { | ||
if (greedy === void 0) { greedy = true; } | ||
return function (object) { | ||
var validate = is_my_json_valid_1.default(schema, { formats: formats, greedy: greedy }); | ||
if (validate(object)) { | ||
return true; | ||
} | ||
var uniqueErrors = ramda_1.uniqBy(errorToString, validate.errors); | ||
if (includesDataHasAdditionalPropertiesError(uniqueErrors) && | ||
ramda_1.keys(schema.properties).length) { | ||
var hasData = findDataHasAdditionalProperties(uniqueErrors); | ||
var additionalProperties = ramda_1.difference(ramda_1.keys(object), ramda_1.keys(schema.properties)); | ||
hasData.message += ': ' + additionalProperties.join(', '); | ||
} | ||
var errors = ramda_1.uniq(errorsToStrings(uniqueErrors)); | ||
return errors; | ||
}; | ||
}; | ||
exports.validate = function (schemas, formats, greedy) { | ||
if (greedy === void 0) { greedy = true; } | ||
return function (schemaName, version) { return function (object) { | ||
schemaName = utils.normalizeName(schemaName); | ||
var namedSchemas = schemas[schemaName]; | ||
if (!namedSchemas) { | ||
return ["Missing schema " + schemaName]; | ||
} | ||
var aSchema = namedSchemas[version]; | ||
if (!aSchema) { | ||
return ["Missing schema " + schemaName + "@" + version]; | ||
} | ||
return exports.validateBySchema(aSchema.schema, formats, greedy)(object); | ||
}; }; | ||
}; | ||
var SchemaError = (function (_super) { | ||
@@ -124,2 +130,3 @@ __extends(SchemaError, _super); | ||
var AssertBySchemaDefaults = { | ||
greedy: true, | ||
substitutions: [], | ||
@@ -145,3 +152,3 @@ omit: { | ||
var replaced = allOptions.substitutions.length ? replace() : object; | ||
var result = exports.validateBySchema(schema, formats)(replaced); | ||
var result = exports.validateBySchema(schema, formats, allOptions.greedy)(replaced); | ||
if (result === true) { | ||
@@ -148,0 +155,0 @@ return object; |
{ | ||
"name": "@cypress/schema-tools", | ||
"version": "1.7.2", | ||
"version": "1.8.0", | ||
"description": "Validate, sanitize and document JSON schemas", | ||
@@ -5,0 +5,0 @@ "main": "dist", |
@@ -197,2 +197,8 @@ # @cypress/schema-tools [](https://circleci.com/gh/cypress-io/schema-tools) [![renovate-app badge][renovate-badge]][renovate-app] | ||
To stop after finding initial set of errors, pass `greedy = false` flag | ||
```js | ||
const validatePerson100 = validate(schemas, formats, false)('person', '1.0.0') | ||
``` | ||
### assertSchema | ||
@@ -241,2 +247,8 @@ | ||
By default the json schema check is [greedy](https://github.com/mafintosh/is-my-json-valid#greedy-mode-tries-to-validate-as-much-as-possible) but you can limit it via an option | ||
```js | ||
assertSchema(schemas, formats)('Person', '1.0.0', { greedy: false }) | ||
``` | ||
### bind | ||
@@ -243,0 +255,0 @@ |
@@ -145,6 +145,7 @@ import { | ||
formats?: JsonSchemaFormats, | ||
greedy: boolean = true, | ||
) => (object: object): true | string[] => { | ||
// TODO this could be cached, or even be part of the loaded module | ||
// when validating use our additional formats, like "uuid" | ||
const validate = validator(schema, { formats }) | ||
const validate = validator(schema, { formats, greedy }) | ||
if (validate(object)) { | ||
@@ -187,2 +188,3 @@ return true | ||
formats?: JsonSchemaFormats, | ||
greedy: boolean = true, | ||
) => (schemaName: string, version: string) => ( | ||
@@ -205,3 +207,3 @@ object: object, | ||
// when validating use our additional formats, like "uuid" | ||
return validateBySchema(aSchema.schema, formats)(object) | ||
return validateBySchema(aSchema.schema, formats, greedy)(object) | ||
} | ||
@@ -284,2 +286,3 @@ | ||
type AssertBySchemaOptions = { | ||
greedy: boolean | ||
substitutions: string[] | ||
@@ -290,2 +293,3 @@ omit: Partial<ErrorMessageWhiteList> | ||
const AssertBySchemaDefaults: AssertBySchemaOptions = { | ||
greedy: true, | ||
substitutions: [], | ||
@@ -322,3 +326,3 @@ omit: { | ||
const replaced = allOptions.substitutions.length ? replace() : object | ||
const result = validateBySchema(schema, formats)(replaced) | ||
const result = validateBySchema(schema, formats, allOptions.greedy)(replaced) | ||
if (result === true) { | ||
@@ -325,0 +329,0 @@ return object |
67259
1660
299