@datadayrepos/json-schema-common
TypeScript icon, indicating that this package has built-in type declarations

0.0.1-beta.11 • Public • Published

Json-Schema-Common

This holds a set of utility functions used across both render and editor tooling for json-schemas.

This is the access point for three main functionalities that are used frequently:

  • getDefaultFormState
  • retrieveSchema
  • validator And a number of form-utility functions.

No dependency on vue or uix frameworks.

🛠 Install

pnpm add @datadayrepos/json-schema-common

💻 Code

RetrieveSchema

Retrieves schema to resolve and normalize a JSON schema. Inspired by react json form, but quite simpler logic, optimized. Also, we dont go recursivelythrough the schema, so it is mostly used for sending in subschemas and resolve their dependencies.

/*
Retrieves schema to resolve and normalize a JSON schema. This means taking a given schema (and possibly a root or parent schema) and processing it to a standard, usable format. This could involve resolving $ref references, handling schema dependencies, or flattening nested schemas.
*/
import { retrieveSchema } from '@datadayrepos/json-schema-common'

GetDefaultFormState

/*
getDefaultFormState is a utility function that calculates default values for a form, based on its JSON Schema. It returns a new object that contains default values for all fields in the form. The resulting object can be used as the formData prop for a JSON Schema form.
*/
import { getDefaultFormState } from '@datadayrepos/json-schema-common'

Validator

This initiates, and imports the validator. It returns a very large object, and by initializing it only once, we save some processing time.

This is really a proxy for importing the full @datadayrepos/json-schema-validator - but deigned to persist the main imported class as a constant.

There are many methods and utilities exposed, but at the outset we really only need these two:

/*
useful when dealing with conditional schemas
like 'oneOf' or 'anyOf'. It identifies which schema from a list of options best matches the current state of formData.
*/
import { validator } from '@datadayrepos/json-schema-common'

// two main methods are exposed here

// simple boolean return
const val = validator.isValidFormData(data, schema, rootschema)

// returns validation errors
const val = validator.validateFormData(data, schema, rootschema)

This validates to schema version 7. Other drafts and utilities are available. See the docs for '@datadayrepos/json-schema-validator'

Utilities

Various form processng utilities used throughout the application.

// form utils exports
import {
  allowAdditionalItems,
  fallbackLabel,
  getSchemaType,
  getUiField,
  getUiOptions,
  getUserErrOptions,
  getWidgetConfig,
  isFixedItems,
  isMultiSelect,
  isSelect,
  optionsList,
  orderProperties,
  replaceArrayIndex,
  shouldShowItem,
} from '@datadayrepos/json-schema-common'

Constants

Various form constants used throughout the application.

// todo? move to application level?
import {
  ADDITIONAL_PROPERTIES_KEY,
  ADDITIONAL_PROPERTY_FLAG,
  ALL_OF_KEY,
  ANY_OF_KEY,
  CONST_KEY,
  DEFAULT_KEY,
  DEFINITIONS_KEY,
  DEPENDENCIES_KEY,
  ENUM_KEY,
  ERRORS_KEY,
  ID_KEY,
  ITEMS_KEY,
  NAME_KEY,
  ONE_OF_KEY,
  PROPERTIES_KEY,
  REF_KEY,
  REQUIRED_KEY,
  SUBMIT_BTN_OPTIONS_KEY,
  UI_FIELD_KEY,
  UI_OPTIONS_KEY,
  UI_WIDGET_KEY,
} from '@datadayrepos/json-schema-common'

📄 License

Prop License © 2023 Ivar Strand

Package Sidebar

Install

npm i @datadayrepos/json-schema-common

Weekly Downloads

2

Version

0.0.1-beta.11

License

Proprietary

Unpacked Size

54.2 kB

Total Files

21

Last publish

Collaborators

  • ivstrand