Skip to main content
Calculator Library - FlatSchema
Praise Magidi avatar
Written by Praise Magidi
Updated over 8 months ago

The flatSchemaUtils provides a set of functions for working with flattened schemas generated from objects. These functions allow you to generate, merge, validate, compare, and sort schemas.

This article provides information on the following:

generate(obj, options)

Generates a flattened schema from the provided object.

Parameters

  • obj (*): The object to generate a flattened schema from.

  • options (optional) (Object): The generation options.

    • ignoreCase (optional) (Boolean): If set to true, field name case will be ignored.

Returns

  • object[]: The flattened schema.

Example

let obj = {
name: "Peter Parker",
alias: "Spiderman",
city: "New York City",
age: 18
}
const flattenedSchema = flatSchemaUtils.generate(obj);
// flattenedSchema = [
// {
// "type": "string",
// "fieldName": "name",
// "path": "",
// "fullPath": "name",
// "hasNull": false,
// "instances": 1,
// "format": null,
// "stringLength": 12
// },
// {
// "type": "string",
// "fieldName": "alias",
// "path": "",
// "fullPath": "alias",
// "hasNull": false,
// "instances": 1,
// "format": null,
// "stringLength": 9
// },
// {
// "type": "string",
// "fieldName": "city",
// "path": "",
// "fullPath": "city",
// "hasNull": false,
// "instances": 1,
// "format": null,
// "stringLength": 13
// },
// {
// "type": "integer",
// "fieldName": "age",
// "path": "",
// "fullPath": "age",
// "hasNull": false,
// "instances": 1,
// "format": null,
// "stringLength": null
// }
// ]

mergeSchemas(...schemas)

Merges multiple schemas together.

Parameters

  • schemas (Object[]): An array of schemas to merge.

Returns

  • object[]: The merged schemas.

validate(data, schema, options)

Validates an object against a schema.

Parameters

  • data (*): The data to validate.

  • schema (Object[]): The flat schema to validate against.

  • options (optional) (Object): The validation options.

Returns

  • object[]|null: An array of validation results or null if the data is valid against the schema.

compareSchemas(schema1, schema2, options)

Compares two schemas for differences.

Parameters

  • schema1 (Object[]): The first schema to compare.

  • schema2 (Object[]): The second schema to compare.

  • options (optional) (Object): The comparison options.

Returns

  • object[]: An array of comparison results.

sortSchema(schema)

Sorts a schema by path.

Parameters

  • schema (Object[]): The schema to sort.

Returns

  • object[]: The sorted schema.

Did this answer your question?