import Field from '../Field' ; export default class Text extends Field { rules = [ ] ; constructor ( min = null , max = null , defaultVal = undefined , flags = null ) { super ( defaultVal , flags ) ; if ( min ) { this . assert ( `.{${ min }}` , true , `must be at least ${ min } characters` ) ; } if ( max ) { this . assert ( `.{${ max + 1 }}` , false , `must be at most ${ max } characters` ) ; } } assert ( rule , expect = true , message = '' ) { const regex = rule instanceof RegExp ? rule : new RegExp ( rule ) ; this . rules . push ( { regex , expect , message } ) ; } async parse ( value ) { if ( value && typeof value === 'object' && value . toString ) { value = value . toString ( ) ; } if ( typeof value === 'string' ) { for ( let i = 0 ; i < this . rules . length ; ++ i ) { const { regex , expect , message } = this . rules [ i ] ; if ( ! ! value . match ( regex ) !== expect ) { this . lastError = message ; return undefined ; } } return value ; } if ( ! value ) { return super . parse ( value ) ; } return undefined ; } }