Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add style validator #44

Open
aexol opened this issue Mar 24, 2020 · 1 comment
Open

Add style validator #44

aexol opened this issue Mar 24, 2020 · 1 comment
Assignees
Labels

Comments

@aexol
Copy link
Collaborator

aexol commented Mar 24, 2020

Add style validator so you cannot pass incorrect values to React PDF Editor

@aexol
Copy link
Collaborator Author

aexol commented Mar 24, 2020

To start validator for number values

import ReactPDF from "@react-pdf/renderer";

/**
 * If style does not validate replace incorrect values with undefined to keep the up running and throw console.error only
 */
export const ReactPDFStyleValidator = (
  style: ReactPDF.Style
): ReactPDF.Style => {
  const copyStyle: ReactPDF.Style = {};
  const validateNumberValue = (v: string) => {
    const validatedNumber = parseFloat(v);
    if (!isNaN(validatedNumber)) {
      return v;
    }
    return undefined;
  };
  const numberKeys: Array<keyof ReactPDF.Style> = [
    "borderBottomLeftRadius",
    "borderBottomRightRadius",
    "borderBottomWidth",
    "borderRadius",
    "borderRightWidth",
    "borderTopLeftRadius",
    "borderTopRightRadius",
    "borderTopWidth",
    "borderWidth",
    "bottom",
    "fontSize",
    "height",
    "left",
    "letterSpacing",
    "lineHeight",
    "margin",
    "marginBottom",
    "marginHorizontal",
    "marginLeft",
    "marginRight",
    "marginTop",
    "marginVertical",
    "maxHeight",
    "minHeight",
    "minWidth",
    "maxWidth",
    "opacity",
    "order",
    "padding",
    "paddingBottom",
    "paddingHorizontal",
    "paddingLeft",
    "paddingRight",
    "paddingTop",
    "paddingVertical",
    "right",
    "width"
  ];
  numberKeys.forEach(k => {
    if (style[k]) {
      const v = validateNumberValue(style[k]);
      if (v) {
        copyStyle[k] = v;
      } else {
        console.error(`Key "${k}" has the incorrect value`);
      }
    }
  });
  return copyStyle;
};

@aexol aexol added the stage-1 label Mar 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants