import ErrorCodes from '../errors/io-error-codes' ; import MemberDef from './memberdef' ; import { ErrorArgs , InternetObjectError , InternetObjectValidationError } from '../errors/io-error' ; import { Node } from '../parser/index' ; import { isToken , isString } from '../utils/is' ; param memberDef param value param node internal export function doCommonTypeCheck ( memberDef , value ? , node ? ) { const isUndefined = value === undefined ; const isNull = isToken ( node ) ? node . value === null : value === null ; if ( isUndefined ) { if ( memberDef . default ) return _default ( memberDef . default ) ; if ( memberDef . optional ) return undefined ; throw new InternetObjectValidationError ( ... _valueRequired ( memberDef , node ) ) ; } if ( isNull ) { if ( memberDef . null ) return null ; throw new InternetObjectError ( ... _nullNotAllowed ( memberDef , node ) ) ; } if ( memberDef . choices !== undefined && memberDef . choices . indexOf ( value ) === - 1 ) { throw new InternetObjectValidationError ( ... _invlalidChoice ( memberDef , value , node ) ) ; } return value ; } function _default ( value ) { if ( isString ( value ) ) { if ( value === 'N' ) return null ; if ( value === 'T' || value === 'true' ) return true ; if ( value === 'F' || value === 'false' ) return false ; return value ; } return value ; } function _valueRequired ( memberDef , node ? ) { return [ ErrorCodes . valueRequired , `Value is missing for '${ memberDef . path }'.` , node ] ; } function _nullNotAllowed ( memberDef , node ? ) { return [ ErrorCodes . nullNotAllowed , `${ memberDef . path } does not support null.` ] ; } function _invlalidChoice ( memberDef , value , node ? ) { if ( ! memberDef . choices ) throw Error ( 'Choices not checked during NumberDef implementation.' ) ; return [ ErrorCodes . invalidChoice , `The value of "${ memberDef . path }" must be one of the [${ memberDef . choices . join ( ',' ) }]. Currently it is ${ value }.` , node ] ; }