I need to add a password check pattern to the json schema that Golang handles.
"User" : {
"type": "object",
"required": ["password"],
"properties": {
},
"password": {
"type": "string",
"pattern":"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[#%$!*+=_#-])[A-Za-z0-9#%$!*+=_-]{8,}$"
}
}
At least one digit At least one lowercase character
At least one uppercase character
At least one special character
At least 8 characters in length, but no more than 32.
The pattern doesn't work because Golang doesn't support positive lookahead
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[#%$!*+=_#-])[A-Za-z0-9#%$!*+=_-]{8,32}$
How to change the pattern to keep the password verification conditions and not use positive lookahead?
Related
Can anyone advise how to code up a JSON Schema document to describe a string that can be one of three possible sequences? Say a string "fruit" can be only the following: "apple", "bananna" or "coconut".
I was thinking it might be possible to use regex but not sure how to indicate the regex constraint in JSON Schema.
https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.6.1
Here is what I have so far:
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "TestSession",
"type": "object",
"properties": {
"fruit": {
"type": "string",
"description": "only three legal possibilities: apple, banana, or coconut"
}
}
You need to use the enum keyword for this.
The value of this keyword MUST be an array. This array SHOULD have
at least one element. Elements in the array SHOULD be unique.
An instance validates successfully against this keyword if its
value is equal to one of the elements in this keyword's array
value.
Elements in the array might be of any type, including null.
https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-6.1.2
For example "enum": [ "apple", "bananna", "coconut" ].
For JSON schemas, can you have the keyword "pattern" set but not have the keyword "type" set to anything? Concern is whether I need to correct existing schemas that do this like the following:
"ExampleType": {
"description": "blah blah",
"type": "array",
"items": {
"pattern": "sometext/ExampleType:[^:/]+:[0-9]*$"
}
}
I checked this here JSON validator and it can work however this would allow any non-string value so it will not care if a non-string value matches the pattern or not. For instance, you could have
{
"ExampleType" : ["sometext/ExampleType:try:1", "sometext/ExampleType:try:2", 5]
}
and it would pass. If the number 5 is put in quotes, then it would fail. So bottom line is that you should always specify the type and it will default to any type if not specified.
I'm a non-developer and new to JSON and have created a JSON schema to validate JSON code when troubleshooting customer issues to insure syntactics against our defined data elements.
I noticed that I can only verify min/maxLength if the field type is an integer. If the type=string, min/maxLength does not work.
JSON Schema code snippet:
"LastName": {
"type" : "string",
"optional": false,
"minLength": 1,
"maxLength": 254,
"description": "Last Name of Insured"
},
In JSON code, if "LastName": "" it still validates, but our WS/JSON call will fail because this field is required and requires data.
Thanks in advance!
It should work per the spec. Reference and quote below. Please elaborate "If the type=string, min/maxLength does not work." - what unexpected exacting behavior do you get?
https://json-schema.org/understanding-json-schema/reference/string.html
Length
The length of a string can be constrained using the minLength
and maxLength keywords. For both keywords, the value must be a
non-negative number.
{
"type": "string",
"minLength": 2,
"maxLength": 3
}
I would like to validate a human input in json schema with pattern, but I cannot figure out the regex for it.
Valid formats are:
"Joe":"day1","Mitch":"day2"
or
"Joe":"day1"
So any number of iterations of "somebody":"someday" separated with , (comma).
Invalid formats are:
"Joe":"day1","Mitch":"day2",
Or
"Joe":"day1";"Mitch":"day2"
Example Json schema (pattern is not working here):
{
"title": "Test",
"type": "object",
"properties": {
"meeting_date": {
"type": "string",
"description": "Give me a name and a day with the following format: \"Joe\":\"day1\" ",
"default": "",
"pattern": "(\"\\w*\")(?::)(\"\\w*\")"
}
}
}
try this solution https://regex101.com/r/vW8m6K/2/
^("\w+":"\w+",)*"\w+":"\w+"$
But it fails on extra spaces, for it test:
^("\w+"\s*:\s*"\w+"\s*(?:,\s*|$))+$
Your pattern acutally almost works. You just have to remove the backslashes in front of your quotation marks.
("\w*")(?::)("\w*")
You can test your Regex on https://regex101.com/ (or some simular website).
The Schema should allow only the following constellation: {"status":"nok"}.
The Key must always be "status" and the value should allow "ok","nok","inProgress"
No differen or additional objects,... should be allowed
I have tried this:
{
"description": "blabla",
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": [
"ok",
"inProgress",
"nok"
],
"required": true,
"additionalItems": false
}
},
"required": true,
"additionalProperties": false
}
This works, but this scheme allows that i can send the same key/value pair twice like {"status":"nok","status":"nok"}
I would be also happy, if it would work without this "object"-container that i'm using, because to reduce overhead.
Maybe someone knows a solution, thanks
There is a more fundamental issue with that input:
{"status":"nok","status":"nok"}
mainly: that input is not valid JSON. RFC 4627, section 2.2, explicitly states that "The names within an object SHOULD be unique". And in your case, they are not.
Which means the JSON parser you use can do whatever it wants to with such an input. Some JSON APIs will grab whatever value they come upon first, other parsers will grab the last value they read, others will even coalesce values -- none of this is illegal as per the RFC.
In essence: given such input, you cannot guarantee what the output of the JSON parser will be; and as such, you cannot guarantee JSON Schema validation of such an input either.