How can I validate number of digits in a number using JSON Schema (ajv)?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
In a two-factor authentication (2FA) the form ask a code formed by only 6 digits
for exampe: 064964
I use the famous Ajv JSON Schema Validator
I can not build the validation scheme for this code:
export const code = {
'type': 'object',
'properties': {
code: {
'type': ['number'],
'minimum': 6,
'minLength': 6
},
},
'required': ['code'],
};
Can you help me?
jsonschema ajv
add a comment |
In a two-factor authentication (2FA) the form ask a code formed by only 6 digits
for exampe: 064964
I use the famous Ajv JSON Schema Validator
I can not build the validation scheme for this code:
export const code = {
'type': 'object',
'properties': {
code: {
'type': ['number'],
'minimum': 6,
'minLength': 6
},
},
'required': ['code'],
};
Can you help me?
jsonschema ajv
add a comment |
In a two-factor authentication (2FA) the form ask a code formed by only 6 digits
for exampe: 064964
I use the famous Ajv JSON Schema Validator
I can not build the validation scheme for this code:
export const code = {
'type': 'object',
'properties': {
code: {
'type': ['number'],
'minimum': 6,
'minLength': 6
},
},
'required': ['code'],
};
Can you help me?
jsonschema ajv
In a two-factor authentication (2FA) the form ask a code formed by only 6 digits
for exampe: 064964
I use the famous Ajv JSON Schema Validator
I can not build the validation scheme for this code:
export const code = {
'type': 'object',
'properties': {
code: {
'type': ['number'],
'minimum': 6,
'minLength': 6
},
},
'required': ['code'],
};
Can you help me?
jsonschema ajv
jsonschema ajv
edited Nov 23 '18 at 16:18
Relequestual
3,54763467
3,54763467
asked Nov 23 '18 at 16:00
JankaJanka
5942524
5942524
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
minLength
only applies to strings, and is not applicable to numbers.
Given codes can start with 0, you can't do minimum: 100000
.
If you want to use pure JSON Schema to do this, you will need to express your code as a string and not a number.
JSON Schema has no validation key word for "number of digits in a number".
That being said, ajv does allow you to add your own keywords, and write the validation code for them, but that would mean your schemas can't be used by other people.
In fact, you may run into problems, if you're expecting to represent 012345. In fact, javascript may treat a number starting with 0 as octal: stackoverflow.com/questions/37003770/…
– Relequestual
Nov 23 '18 at 16:22
1
No need for a custom keyword.pattern
can be used to restrict all the characters in a string to digits. You can enforce the number of digits using the regex or useminLength
/maxLength
.
– Jason Desrosiers
Nov 23 '18 at 17:20
In a matter of fact, properly shaped regex in 'pattern' keyword would handle all: just digits, min 6 od them.
– PsychoFish
Nov 24 '18 at 14:46
Actually, no.pattern
is only applicable to strings, and is not applicable to other types. Some libraries may automagically cast to a string, but that's not the correct behaviour. There's a test for it, but it doesn't check all other types. Maybe it should. github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/…
– Relequestual
Nov 26 '18 at 9:09
@Relequestual - my comment followed assumption that the digits will be casted to string before validation using JSON schema will be executed. Sorry I didnt underlined it properly earlier. I agree, assumption that it will be converted automagically is careless. One needs to do it explicitly. Once it's done, usepattern
with fairly simple regex and that's it. Sometimes, depending on regex parser one uses, it'll come for free, but better be safe than sorry.
– PsychoFish
Nov 26 '18 at 13:52
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449740%2fhow-can-i-validate-number-of-digits-in-a-number-using-json-schema-ajv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
minLength
only applies to strings, and is not applicable to numbers.
Given codes can start with 0, you can't do minimum: 100000
.
If you want to use pure JSON Schema to do this, you will need to express your code as a string and not a number.
JSON Schema has no validation key word for "number of digits in a number".
That being said, ajv does allow you to add your own keywords, and write the validation code for them, but that would mean your schemas can't be used by other people.
In fact, you may run into problems, if you're expecting to represent 012345. In fact, javascript may treat a number starting with 0 as octal: stackoverflow.com/questions/37003770/…
– Relequestual
Nov 23 '18 at 16:22
1
No need for a custom keyword.pattern
can be used to restrict all the characters in a string to digits. You can enforce the number of digits using the regex or useminLength
/maxLength
.
– Jason Desrosiers
Nov 23 '18 at 17:20
In a matter of fact, properly shaped regex in 'pattern' keyword would handle all: just digits, min 6 od them.
– PsychoFish
Nov 24 '18 at 14:46
Actually, no.pattern
is only applicable to strings, and is not applicable to other types. Some libraries may automagically cast to a string, but that's not the correct behaviour. There's a test for it, but it doesn't check all other types. Maybe it should. github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/…
– Relequestual
Nov 26 '18 at 9:09
@Relequestual - my comment followed assumption that the digits will be casted to string before validation using JSON schema will be executed. Sorry I didnt underlined it properly earlier. I agree, assumption that it will be converted automagically is careless. One needs to do it explicitly. Once it's done, usepattern
with fairly simple regex and that's it. Sometimes, depending on regex parser one uses, it'll come for free, but better be safe than sorry.
– PsychoFish
Nov 26 '18 at 13:52
|
show 2 more comments
minLength
only applies to strings, and is not applicable to numbers.
Given codes can start with 0, you can't do minimum: 100000
.
If you want to use pure JSON Schema to do this, you will need to express your code as a string and not a number.
JSON Schema has no validation key word for "number of digits in a number".
That being said, ajv does allow you to add your own keywords, and write the validation code for them, but that would mean your schemas can't be used by other people.
In fact, you may run into problems, if you're expecting to represent 012345. In fact, javascript may treat a number starting with 0 as octal: stackoverflow.com/questions/37003770/…
– Relequestual
Nov 23 '18 at 16:22
1
No need for a custom keyword.pattern
can be used to restrict all the characters in a string to digits. You can enforce the number of digits using the regex or useminLength
/maxLength
.
– Jason Desrosiers
Nov 23 '18 at 17:20
In a matter of fact, properly shaped regex in 'pattern' keyword would handle all: just digits, min 6 od them.
– PsychoFish
Nov 24 '18 at 14:46
Actually, no.pattern
is only applicable to strings, and is not applicable to other types. Some libraries may automagically cast to a string, but that's not the correct behaviour. There's a test for it, but it doesn't check all other types. Maybe it should. github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/…
– Relequestual
Nov 26 '18 at 9:09
@Relequestual - my comment followed assumption that the digits will be casted to string before validation using JSON schema will be executed. Sorry I didnt underlined it properly earlier. I agree, assumption that it will be converted automagically is careless. One needs to do it explicitly. Once it's done, usepattern
with fairly simple regex and that's it. Sometimes, depending on regex parser one uses, it'll come for free, but better be safe than sorry.
– PsychoFish
Nov 26 '18 at 13:52
|
show 2 more comments
minLength
only applies to strings, and is not applicable to numbers.
Given codes can start with 0, you can't do minimum: 100000
.
If you want to use pure JSON Schema to do this, you will need to express your code as a string and not a number.
JSON Schema has no validation key word for "number of digits in a number".
That being said, ajv does allow you to add your own keywords, and write the validation code for them, but that would mean your schemas can't be used by other people.
minLength
only applies to strings, and is not applicable to numbers.
Given codes can start with 0, you can't do minimum: 100000
.
If you want to use pure JSON Schema to do this, you will need to express your code as a string and not a number.
JSON Schema has no validation key word for "number of digits in a number".
That being said, ajv does allow you to add your own keywords, and write the validation code for them, but that would mean your schemas can't be used by other people.
answered Nov 23 '18 at 16:17
RelequestualRelequestual
3,54763467
3,54763467
In fact, you may run into problems, if you're expecting to represent 012345. In fact, javascript may treat a number starting with 0 as octal: stackoverflow.com/questions/37003770/…
– Relequestual
Nov 23 '18 at 16:22
1
No need for a custom keyword.pattern
can be used to restrict all the characters in a string to digits. You can enforce the number of digits using the regex or useminLength
/maxLength
.
– Jason Desrosiers
Nov 23 '18 at 17:20
In a matter of fact, properly shaped regex in 'pattern' keyword would handle all: just digits, min 6 od them.
– PsychoFish
Nov 24 '18 at 14:46
Actually, no.pattern
is only applicable to strings, and is not applicable to other types. Some libraries may automagically cast to a string, but that's not the correct behaviour. There's a test for it, but it doesn't check all other types. Maybe it should. github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/…
– Relequestual
Nov 26 '18 at 9:09
@Relequestual - my comment followed assumption that the digits will be casted to string before validation using JSON schema will be executed. Sorry I didnt underlined it properly earlier. I agree, assumption that it will be converted automagically is careless. One needs to do it explicitly. Once it's done, usepattern
with fairly simple regex and that's it. Sometimes, depending on regex parser one uses, it'll come for free, but better be safe than sorry.
– PsychoFish
Nov 26 '18 at 13:52
|
show 2 more comments
In fact, you may run into problems, if you're expecting to represent 012345. In fact, javascript may treat a number starting with 0 as octal: stackoverflow.com/questions/37003770/…
– Relequestual
Nov 23 '18 at 16:22
1
No need for a custom keyword.pattern
can be used to restrict all the characters in a string to digits. You can enforce the number of digits using the regex or useminLength
/maxLength
.
– Jason Desrosiers
Nov 23 '18 at 17:20
In a matter of fact, properly shaped regex in 'pattern' keyword would handle all: just digits, min 6 od them.
– PsychoFish
Nov 24 '18 at 14:46
Actually, no.pattern
is only applicable to strings, and is not applicable to other types. Some libraries may automagically cast to a string, but that's not the correct behaviour. There's a test for it, but it doesn't check all other types. Maybe it should. github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/…
– Relequestual
Nov 26 '18 at 9:09
@Relequestual - my comment followed assumption that the digits will be casted to string before validation using JSON schema will be executed. Sorry I didnt underlined it properly earlier. I agree, assumption that it will be converted automagically is careless. One needs to do it explicitly. Once it's done, usepattern
with fairly simple regex and that's it. Sometimes, depending on regex parser one uses, it'll come for free, but better be safe than sorry.
– PsychoFish
Nov 26 '18 at 13:52
In fact, you may run into problems, if you're expecting to represent 012345. In fact, javascript may treat a number starting with 0 as octal: stackoverflow.com/questions/37003770/…
– Relequestual
Nov 23 '18 at 16:22
In fact, you may run into problems, if you're expecting to represent 012345. In fact, javascript may treat a number starting with 0 as octal: stackoverflow.com/questions/37003770/…
– Relequestual
Nov 23 '18 at 16:22
1
1
No need for a custom keyword.
pattern
can be used to restrict all the characters in a string to digits. You can enforce the number of digits using the regex or use minLength
/maxLength
.– Jason Desrosiers
Nov 23 '18 at 17:20
No need for a custom keyword.
pattern
can be used to restrict all the characters in a string to digits. You can enforce the number of digits using the regex or use minLength
/maxLength
.– Jason Desrosiers
Nov 23 '18 at 17:20
In a matter of fact, properly shaped regex in 'pattern' keyword would handle all: just digits, min 6 od them.
– PsychoFish
Nov 24 '18 at 14:46
In a matter of fact, properly shaped regex in 'pattern' keyword would handle all: just digits, min 6 od them.
– PsychoFish
Nov 24 '18 at 14:46
Actually, no.
pattern
is only applicable to strings, and is not applicable to other types. Some libraries may automagically cast to a string, but that's not the correct behaviour. There's a test for it, but it doesn't check all other types. Maybe it should. github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/…– Relequestual
Nov 26 '18 at 9:09
Actually, no.
pattern
is only applicable to strings, and is not applicable to other types. Some libraries may automagically cast to a string, but that's not the correct behaviour. There's a test for it, but it doesn't check all other types. Maybe it should. github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/…– Relequestual
Nov 26 '18 at 9:09
@Relequestual - my comment followed assumption that the digits will be casted to string before validation using JSON schema will be executed. Sorry I didnt underlined it properly earlier. I agree, assumption that it will be converted automagically is careless. One needs to do it explicitly. Once it's done, use
pattern
with fairly simple regex and that's it. Sometimes, depending on regex parser one uses, it'll come for free, but better be safe than sorry.– PsychoFish
Nov 26 '18 at 13:52
@Relequestual - my comment followed assumption that the digits will be casted to string before validation using JSON schema will be executed. Sorry I didnt underlined it properly earlier. I agree, assumption that it will be converted automagically is careless. One needs to do it explicitly. Once it's done, use
pattern
with fairly simple regex and that's it. Sometimes, depending on regex parser one uses, it'll come for free, but better be safe than sorry.– PsychoFish
Nov 26 '18 at 13:52
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449740%2fhow-can-i-validate-number-of-digits-in-a-number-using-json-schema-ajv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown