import { string , object , array } from 'yup' ; import { ValidationResultsWrapper } from '../domain/Referee' ; const tocSchema = object ( ) . shape ( { home : string ( ) . trim ( ) . required ( ) , table_of_contents : array ( ) . of ( object ( ) . shape ( { directory : string ( ) . trim ( ) . required ( ) , pages : array ( ) . of ( object ( ) . shape ( { filename : string ( ) . trim ( ) . required ( ) , display_name : string ( ) . trim ( ) } ) ) } ) ) . required ( ) , faq : string ( ) . trim ( ) } ) ; export const validateToc = ( toc ) : ValidationResultsWrapper => { let error ; const errors = { } ; try { tocSchema . validateSync ( toc , { abortEarly : false , strict : true } ) ; } catch ( e ) { error = e ; } if ( error ) { if ( error . name !== 'ValidationError' ) { throw error ; } const inner = error . inner ; if ( inner ) { inner . forEach ( validationError => { errors [ validationError . path ] = validationError . errors . join ( ', ' ) ; } ) ; } } return { errors : errors , isValid : Object . keys ( errors ) . length === 0 } ; } ;