Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Struct with predicate #51

Open
jojolapin972 opened this issue Nov 2, 2016 · 0 comments
Open

Struct with predicate #51

jojolapin972 opened this issue Nov 2, 2016 · 0 comments

Comments

@jojolapin972
Copy link

I am using version 3.2.2 and I am trying to build a library of reusable validators around tcomb-validator.

So I first defined my custom field validators which are predicates and then wanted to compose an object validator using the structure like follow:

       ` var Title = function (s) {
              return s.length > 2 && s.length <= 255;
        }`

        `Title.getValidationErrorMessage = function (value) {
            if (!value) {
                return 'document.validation.error.missing';
            }

            if (value.length <= 2) {
                return 'document.validation.error.tooShort';
            }

            if (payload.title.length > 255) {
                return 'document.validation.error.tooLong';
            }
        };`

And then used like this:

        `var BaseDocument = t.struct({
            title: Title,
            body: OtherCustomType,
        });
        var payload = {
            title: "abcd",
            body: "some other valid value",
        };
        var validationResult = validate(payload, BaseDocument);`

But in this case, even if the payload is valid and the validationResult.isValid() returns true

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant