
import {computed,observe} from 'mobx'
import Validator from '@/Common-Library/Validator'
import camelCase from './camelCase'


function defineComputedProperty(target, name, descriptor){
  Object.defineProperty(
    target,
    name,
    descriptor
  );
  computed(target, name, descriptor);
}
export const prefix = "validateError"


export default function validate(target, name, descriptor)
{
    const clazz = target.constructor;

    const prototype = clazz.prototype
    
    const validateName = camelCase('validateError', name);
    //增加
    if (target.hasOwnProperty('__validateFields')){
        target.__validateFields.push(name);
      } else {
        Object.defineProperty(target, '__validateFields', {
          configurable: true,
          enumerable: false,
          value: [name],
        })
    }
    //校验错误
    if (!target.hasOwnProperty('validateError')) {
        defineComputedProperty(target, 'validateError', {
          configurable: true,
          enumerable: false,
          get: getValidateError,
        })
      }
    //返回是否有错误
    if (!target.hasOwnProperty('isValid')) {
        defineComputedProperty(target, 'isValid', {
          configurable: true,
          enumerable: false,
          get: getIsValid,
        })
      }
    
}

function getValidateError() {
    return this.__validateFields.find(key =>this[camelCase(prefix,key)]);
}

function getIsValid() {
    return !this.validateError;
}