PhilanthropyDataCommons / service

A project for collecting and serving public information associated with grant applications
GNU Affero General Public License v3.0
8 stars 2 forks source link

Validate Primitives using Ajv #925

Closed hminsky2002 closed 4 months ago

hminsky2002 commented 5 months ago

This PR rids us of the last of the hard-coded regex's used in type validation of proposal field values. It utilizes the primitive type validation in ajv to validate numbers and booleans. It follows the pattern in PR #914 of refactoring the fieldValueIsValid function to use a series of validation functions, instead of performing the validation itself.

Closes #899

hminsky2002 commented 5 months ago

@slifty pushed some changes here, but wanted to wait for your feedback on the boolean validation:

function isValidBoolean(bool: string): boolean {
// Converting to lower case to abide by ajv's coercion rules
    const formattedBoolean = bool.toLowerCase();

    const booleanSchema = {
        type: 'boolean',
    };

    return ajv.validate(booleanSchema, formattedBoolean);
}
slifty commented 5 months ago

Let's not make any changes to the values before (or while) validating -- the goal here isn't to ensure maximial passage, it's to define a reasonable specification and test against it. I think that using ajv's specification is fine (and probably is driven by a standard somewhere), and if something doesn't pass it then it simply doesn't pass.

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 88.21%. Comparing base (f43cd9d) to head (3f222b4).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #925 +/- ## ========================================== - Coverage 88.23% 88.21% -0.03% ========================================== Files 123 123 Lines 1709 1706 -3 Branches 211 211 ========================================== - Hits 1508 1505 -3 Misses 201 201 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.