import Ajv from 'ajv' ; import addFormats from 'ajv-formats' ; import _ from 'lodash' ; import { InvalidFieldError , FieldTypeError } from '../../errors/general' ; import { allAttributesConfig } from '../../config/dataFields' ; import { BuildingAttributes , BuildingUserAttributes } from '../../models/building' ; import { fieldSchemaConfig } from '../../config/fieldSchemaConfig' ; const ajv = new Ajv ( ) ; addFormats ( ajv ) ; const compiledSchemas = _ . mapValues ( fieldSchemaConfig , ( val ) => ajv . compile ( val ) ) ; function isDefined ( key ) { return allAttributesConfig [ key ] !== undefined ; } function canEdit ( key , allowDerived = false ) { const config = allAttributesConfig [ key ] ; return config . edit || ( allowDerived && config . derivedEdit ) ; } export function validateFieldChange ( field , value , isExternal = true ) { if ( ! isDefined ( field ) ) { throw new InvalidFieldError ( 'Field does not exist' , field ) ; } const allowDerived = ! isExternal ; if ( ! canEdit ( field , allowDerived ) ) { throw new InvalidFieldError ( 'Field is not editable' , field ) ; } if ( field in compiledSchemas ) { if ( ! compiledSchemas [ field ] ( value ) ) { throw new FieldTypeError ( 'Invalid format of data sent' , field ) ; } } } export function validateChangeSet ( attributes , isExternal = true ) { _ . forIn ( attributes , ( value , fieldKey ) => validateFieldChange ( fieldKey , value , isExternal ) ) ; }