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

Format function doesn't works for numbers #159

Open
avkos opened this issue Apr 25, 2023 · 3 comments
Open

Format function doesn't works for numbers #159

avkos opened this issue Apr 25, 2023 · 3 comments
Assignees

Comments

@avkos
Copy link

avkos commented Apr 25, 2023

I want to use the custom format function, but it doesn't call if the data has a number type.

example:


var validate = validator({
    type: 'array',
    items: [{ $id:'/0/0', format: 'uint' }] ,
    maxItems: 1,
    minItems: 1
}, {
    includeErrors: true,
    formats: {
        'uint': (d) => {
            console.log('validator works')
            return d>0
        }
    },
});

console.log(validate([-2])); // true -  formator uint doesn't call
console.log(validate(['-2'])); // false - formator uint calls

Expect behavior:
Formats works for numbers and any other types

@ChALkeR
Copy link
Contributor

ChALkeR commented Jul 27, 2023

Hm. While the specs say that built-in formats apply only to strings and ignore/always pass on non-string values, that doesn't necessary mean that custom formats should be only applicable to strings.

That means that changing the impl in a way that will attempt to run custom format validators on everything including non-strings is acceptable per spec.

That might cause backwards incompatibility though (and might be unexpected in some cases), so likely shouldn't be the default behavior, but rather an option.

Will work on this.

@ChALkeR
Copy link
Contributor

ChALkeR commented Jul 27, 2023

That said, the schema you provided looks problematic as it lacks type checks.
I'll think if there could be a way to reduce the probability of mistakes here (though strong mode should help).

@ChALkeR
Copy link
Contributor

ChALkeR commented Aug 30, 2023

Perhaps adding a separate option for generic formats that would enable this behavior explicitly would be a non-dangerous path.

@ChALkeR ChALkeR self-assigned this Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants