import { KeywordDefinition } from 'ajv' ; import { isObject } from '../utils' ; const DateRangeKeyword = { type : 'boolean' , compile ( schema ) { const items = schema . items ; const type = schema . type ; const validFun = ( ) => true ; if ( type !== 'array' || ! isObject ( items ) ) { return validFun ; } const itemType = items . type ; const itemFormat = items . format ; if ( ( itemType !== 'number' && itemType !== 'string' ) || ( itemFormat !== 'date' && itemFormat !== 'date-time' ) ) { return validFun ; } return ( data ) => { const [ before , after ] = data ; return Date . parse ( before ) <= Date . parse ( after ) ; } ; } , macro ( ) { return { minItems : 2 , maxItems : 2 , } ; } , } ; const DateTimeRangeKeyword = { type : 'boolean' , compile ( schema ) { const items = schema . items ; const type = schema . type ; const validFun = ( ) => true ; if ( type !== 'array' || ! isObject ( items ) ) { return validFun ; } const itemType = items . type ; const itemFormat = items . format ; if ( ( itemType !== 'number' && itemType !== 'string' ) || ( itemFormat !== 'date' && itemFormat !== 'date-time' && itemFormat !== 'time' ) ) { return validFun ; } return ( data ) => { const [ before , after ] = data ; return Date . parse ( before ) <= Date . parse ( after ) ; } ; } , macro ( ) { return { minItems : 2 , maxItems : 2 , } ; } , } ; export { DateTimeRangeKeyword , DateRangeKeyword } ;