UNPKG

@thangk/easythemer

Version:

Easily generate shades from a colour palette for use in your app

87 lines (70 loc) 2.48 kB
import type { ShadeFactorsSet, ShadeOption } from "../types"; import { ValidateOptionName, ValidateHex } from "../validators"; import { TYPE } from "../constants"; import ValidateChannelParams from "../validators/ValidateChannelParams"; interface OutputType { result: ShadeOption[] | null; errors: null | {}; } export default function useValidate(inputToValidate: any) { const output: OutputType = { result: null, errors: null, }; let { result, errors } = output; const workingInputCopy = [...inputToValidate].map((shadeOption: ShadeOption) => { const { type, optionName, hex, channelParams, generateShades } = shadeOption; if (!type) { errors = { ...errors, type: "Type is undefined. Input data type is not a type made by this dev. Therefore no validation will be performed.", }; return; } if (type === TYPE.SHADE_OPTION) { /** * fields to validate: * - optionName * - hex * - generateShades * - channelParams */ const validatedOptionName = new ValidateOptionName(optionName); const validatedHex = new ValidateHex(hex); const validatedChannelParams = channelParams ? new ValidateChannelParams(channelParams!) : null; if (validatedOptionName.hasErrors) { errors = { ...errors, colourName: validatedOptionName.getErrorsList }; } if (validatedHex.hasErrors) { errors = { ...errors, colourHex: validatedHex.getErrorsList }; } if (validatedChannelParams && validatedChannelParams.hasErrors) { errors = { ...errors, channelParams: validatedChannelParams.getErrorsList }; } if (generateShades !== true && generateShades !== false) { errors = { ...errors, generateShades: "generateShades must be true or false" }; } return { ...shadeOption, optionName: validatedOptionName.getResult, hex: validatedHex.getResult, }; } if (type === TYPE.SHADE_FACTORS_SET) { /** * TODO: validate shades */ } }); console.log("errors in useValidate", errors); // if no errors, return validated object if (!errors) { console.log("no errors in useValidate"); return { ...output, result: workingInputCopy, // validated object }; } console.log("errors in useValidate"); return output; }