import isEmpty from 'lodash/isEmpty' ; import isArray from 'lodash/isArray' ; import includes from 'lodash/includes' ; import find from 'lodash/find' ; import { JsonSchema , Scopable , UISchemaElement } from '..' ; import { resolveData , resolveSchema } from './resolvers' ; import { composePaths , toDataPathSegments } from './path' ; import { isEnabled , isVisible } from './runtime' ; import Ajv from 'ajv' ; param { string } s returns { string } export const convertToValidClassName = ( s ) : string => s . replace ( '#' , 'root' ) . replace ( new RegExp ( '/' , 'g' ) , '_' ) ; export const formatErrorMessage = ( errors ) => { if ( errors === undefined || errors === null ) { return '' ; } return errors . join ( '\n' ) ; } ; export const hasType = ( jsonSchema , expected ) : boolean => { return includes ( deriveTypes ( jsonSchema ) , expected ) ; } ; /**
* Derives the type of the jsonSchema element
*/ export const deriveTypes = ( jsonSchema ) : string [ ] => { if ( isEmpty ( jsonSchema ) ) { return [ ] ; } if ( ! isEmpty ( jsonSchema . type ) && typeof jsonSchema . type === 'string' ) { return [ jsonSchema . type ] ; } if ( isArray ( jsonSchema . type ) ) { return jsonSchema . type ; } if ( ! isEmpty ( jsonSchema . properties ) || ! isEmpty ( jsonSchema . additionalProperties ) ) { return [ 'object' ] ; } if ( ! isEmpty ( jsonSchema . items ) ) { return [ 'array' ] ; } if ( ! isEmpty ( jsonSchema . allOf ) ) { const allOfType = find ( jsonSchema . allOf , ( schema ) => deriveTypes ( schema ) . length !== 0 ) ; if ( allOfType ) { return deriveTypes ( allOfType ) ; } } return [ ] ; } ; /**
* Convenience wrapper around resolveData and resolveSchema.
*/ export const Resolve = { schema : resolveSchema , data : resolveData } ; const fromScopable = ( scopable ) => toDataPathSegments ( scopable . scope ) . join ( '.' ) ; export const Paths = { compose : composePaths , fromScopable } ; export const Runtime = { isEnabled ( uischema , data , ajv ) { return isEnabled ( uischema , data , undefined , ajv ) ; } , isVisible ( uischema , data , ajv ) { return isVisible ( uischema , data , undefined , ajv ) ; } } ;