@cypress/schema-tools
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -24,2 +24,4 @@ "use strict"; | ||
var utils = __importStar(require("./utils")); | ||
var formats_1 = require("./formats"); | ||
var sanitize_1 = require("./sanitize"); | ||
var debug_1 = __importDefault(require("debug")); | ||
@@ -165,1 +167,28 @@ var is_my_json_valid_1 = __importDefault(require("@bahmutov/is-my-json-valid")); | ||
}; }; | ||
var mergeSchemas = function (schemas) { | ||
return ramda_1.mergeAll(schemas); | ||
}; | ||
var mergeFormats = function (formats) { | ||
return ramda_1.mergeAll(formats); | ||
}; | ||
var exists = function (x) { return Boolean(x); }; | ||
exports.bind = function () { | ||
var options = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
options[_i] = arguments[_i]; | ||
} | ||
var allSchemas = ramda_1.map(ramda_1.prop('schemas'), options); | ||
var schemas = mergeSchemas(allSchemas); | ||
var allFormats = ramda_1.filter(exists, ramda_1.map(ramda_1.prop('formats'), options)); | ||
var formats = mergeFormats(allFormats); | ||
var formatDetectors = formats_1.detectors(formats); | ||
var defaults = formats_1.getDefaults(formats); | ||
var api = { | ||
assertSchema: exports.assertSchema(schemas, formatDetectors), | ||
schemaNames: exports.schemaNames(schemas), | ||
getExample: exports.getExample(schemas), | ||
sanitize: sanitize_1.sanitize(schemas, defaults), | ||
validate: exports.validate(schemas), | ||
}; | ||
return api; | ||
}; |
{ | ||
"name": "@cypress/schema-tools", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Validate, sanitize and document JSON schemas", | ||
@@ -5,0 +5,0 @@ "main": "dist", |
@@ -115,9 +115,9 @@ # @cypress/schema-tools [](https://circleci.com/gh/cypress-io/schema-tools) [![renovate-app badge][renovate-badge]][renovate-app] | ||
type: 'string', | ||
format: 'uuid' | ||
} | ||
} | ||
format: 'uuid', | ||
}, | ||
}, | ||
}, | ||
example: { | ||
id: 'a368dbfd-08e4-4698-b9a3-b2b660a11835' | ||
} | ||
id: 'a368dbfd-08e4-4698-b9a3-b2b660a11835', | ||
}, | ||
} | ||
@@ -211,2 +211,16 @@ // person100 goes into "schemas", then | ||
### bind | ||
There are multiple methods to validate, assert or sanitize an object against a schema. All take schemas and (optional) formats. But a project using schema tools probably has a single collection of schemas that it wants to use again and again. The `bind` method makes it easy to bind the first argument in each function to a schema collection and just call methods with an object later. | ||
```js | ||
import { schemas } from './my-schemas' | ||
import { formats } from './my-formats' | ||
import { bind } from '@cypress/schema-tools' | ||
const api = bind({ schemas, formats }) | ||
api.assertSchema('name', '1.0.0')(someObject) | ||
``` | ||
See [test/bind-test.ts](test/bind-test.ts) for examples | ||
### SchemaError | ||
@@ -213,0 +227,0 @@ |
@@ -10,3 +10,10 @@ import { | ||
import * as utils from './utils' | ||
import { JsonSchemaFormats } from './formats' | ||
import { | ||
JsonSchemaFormats, | ||
CustomFormats, | ||
detectors, | ||
getDefaults, | ||
FormatDefaults, | ||
} from './formats' | ||
import { sanitize } from './sanitize' | ||
import debugApi from 'debug' | ||
@@ -17,3 +24,14 @@ import validator from '@bahmutov/is-my-json-valid' | ||
import get from 'lodash.get' | ||
import { uniq, find, whereEq, difference, keys, uniqBy } from 'ramda' | ||
import { | ||
uniq, | ||
find, | ||
whereEq, | ||
difference, | ||
keys, | ||
uniqBy, | ||
mergeAll, | ||
prop, | ||
map, | ||
filter, | ||
} from 'ramda' | ||
import stringify from 'json-stable-stringify' | ||
@@ -345,1 +363,42 @@ | ||
} | ||
type BindOptions = { | ||
schemas: SchemaCollection | ||
formats?: CustomFormats | ||
} | ||
const mergeSchemas = (schemas: SchemaCollection[]): SchemaCollection => | ||
mergeAll(schemas) | ||
const mergeFormats = (formats: CustomFormats[]): CustomFormats => | ||
mergeAll(formats) | ||
const exists = x => Boolean(x) | ||
/** | ||
* Given schemas and formats creates "mini" API bound to the these schemas. | ||
* Can take multiple schemas and merged them all, and merges formats. | ||
*/ | ||
export const bind = (...options: BindOptions[]) => { | ||
const allSchemas: SchemaCollection[] = map(prop('schemas'), options) | ||
const schemas = mergeSchemas(allSchemas) | ||
const allFormats: CustomFormats[] = filter( | ||
exists, | ||
map(prop('formats'), options), | ||
) | ||
const formats = mergeFormats(allFormats) | ||
const formatDetectors = detectors(formats) | ||
const defaults: FormatDefaults = getDefaults(formats) | ||
const api = { | ||
assertSchema: assertSchema(schemas, formatDetectors), | ||
schemaNames: schemaNames(schemas), | ||
getExample: getExample(schemas), | ||
sanitize: sanitize(schemas, defaults), | ||
validate: validate(schemas), | ||
} | ||
return api | ||
} |
64241
1604
257