{"size": 1492, "ext": "js", "max_stars_repo_path": "test/src/Rule/rule.spec.js", "max_stars_repo_name": "DanielchenN/shineout", "max_stars_repo_stars_event_min_datetime": "2018-09-02T10:52:52.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-29T07:30:22.000Z", "max_issues_repo_path": "test/src/Rule/rule.spec.js", "max_issues_repo_name": "PatricioPeng/shineout", "max_issues_repo_issues_event_min_datetime": "2018-09-14T09:38:23.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T06:03:50.000Z", "max_forks_repo_path": "test/src/Rule/rule.spec.js", "max_forks_repo_name": "PatricioPeng/shineout", "max_forks_repo_forks_event_min_datetime": "2018-09-10T14:32:07.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T07:43:55.000Z"}
{"max_stars_count": 780, "max_issues_count": 128, "max_forks_count": 145, "avg_line_length": 31.0833333333, "max_line_length": 95, "alphanum_fraction": 0.5764075067}
import { Rule } from 'shineout'

const innerType = ['email', 'integer', 'number', 'url', 'json', 'hex', 'rgb', 'ipv4']
describe('Rule', () => {
  test('should have inner validate', () => {
    const rule = Rule()
    const types = [...innerType, 'required', 'max', 'min', 'regExp', 'type', 'length', 'range']
    types.forEach(type => {
      expect(rule[type] instanceof Function).toBeTruthy()
    })
  })
  test('should combine same keys', () => {
    const funcs = {
      customRequired: {
        func: (value, formData, callback, props) => {
          callback(value ? true : new Error(`${props.title} is required.`))
        },
      },
    }
    const messages = {
      customRequired: {
        message: 'The field is required.',
      },
    }
    const rule = Rule(funcs, messages)
    const required = rule.customRequired()
    expect(required.message).toBe('The field is required.')
    expect(required.func instanceof Function).toBeTruthy()
  })
  test('should support custom validate', () => {
    const validateFn = (value, formData, callback, props) => {
      if (formData.list.includes(value)) {
        callback(new Error(props.message.replace('{title}', props.title)))
      } else {
        callback(true)
      }
    }
    const rule = Rule({
      isExisted: {
        func: validateFn,
        message: '{title} is existed.',
      },
    })
    expect(rule.isExisted().func).toBe(validateFn)
    expect(rule.isExisted().message).toBe('{title} is existed.')
  })
})
