import Validation, {ErrorMessages, Fields, Rules} from "./mod.ts";

const input = {
    name: 'Doğukan Akkaya',
    email: 'info@codethereal.com',
    password: '1234',
    password_confirmation: '1234',
    url: 'codethereal.com',
    address: [
        {
            name: 'Address line 1',
            location: 'Turkey, Istanbul'
        },
        {
            name: 'Address line 2',
            location: 'Turkey, Istanbul'
        }
    ],
    detail: {
        job: 'Software Developer',
        salary: 10.000,
        currency: 'USD'
    },
    status: 1,
    numbers: [1,2,3,4,5,6]
}

const rules: Rules = {
    name: 'string|required|max:25|min:5',
    email: 'string|required|email',
    password: 'string|required|min:8',
    url: 'string|required|url',
    //password_confirmation: 'match:password',
    address: {
        name: 'string|required',
        location: 'string|required'
    },
    detail: {
        job: 'string',
        salary: 'number',
        currency: 'string|in:USD,EUR,TRY'
    },
    status: 'number|required|in:0,1',
    numbers: 'required|array|max:5'
}

const fields: Fields = {
    name: 'Name',
    email: 'Email',
    password: 'Password',
    password_confirmation: 'Password Confirmation',
    address: {
        name: 'Address Name',
        location: 'Address Location'
    },
    detail: {
        job: 'Job',
        salary: 'Salary',
        currency: 'Currency'
    },
    status: 'Status'
}

const customErrorMessages: ErrorMessages = JSON.parse(Deno.readTextFileSync("./en.json"))
/*
{
  "required": "The {field} field is required.",
  "email": "The {field} field must contain a valid email address.",
  "url": "The {field} field must contain a valid URL.",
  "ip": "The {field} field must contain a valid IP.",
  "min": "The {field} field must be at least {param} in length.",
  "max": "The {field} field cannot exceed {param} in length.",
  "size": "The {field} field must be exactly {param} in length.",
  "in": "The {field} field must be one of: {param}.",
  "string": "The {field} must be a text.",
  "number": "The {field} field must be an number.",
  "array": "The {field} field must be an array."
}
*/
const validation = new Validation(customErrorMessages)

const errors = validation
    .validate(input, rules, fields)
    .single('abc', 'min:5', 'test')
    .getErrors()

if (errors.length > 0) {
    console.log(errors)
} else {
    console.log('There is no error')
}