Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a JSON String to validate which contains two separate objects. The string is "1A" but i want to validate it as individual objects for example: {"NumberInt":1,"LetterThing":"A"}.
In conclusion, my string is "1A" but I need to validate it as individual objects despite the fact it's in a string format.
Why do I want this? I have a minimum and maximum for the NumberInt integer value and I have a particular pattern for the LetterThing string value. For example: I do not want "5H" to validate.
If this is possible in a string format please let me know how.
Solved:
Was solved by using regex to validate on my JsonSchema i.e "pattern": "^[A-Ja-j1-4\\s]*$".
Thanks guys
You could use a regex to extract what you need from the JSON.
//obtains the number part, then you can perform operations on that number
var startingDigits = incomingString.replace( /^\D+/g, '');
You would need to PARSE the string in this case.
To parse a STRING you ITERATE over each CHARACTER in the string and then compose the needed parsed ELEMENTS.
For example in this CASE you might start looking for only DIGITS and putting them in another string. When you hit a LETTER you can CONVERT that string to an integer.
Then take the REMAINING as the 2nd part.
Finally do your VALIDATION.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Can someone please explain me why is highly recommended to use Django forms to get an html object instead of direct accessing from request.POST or request.GET?
In short: forms make retrieving and validating less error-prone, and often will perform a lot of logic that would otherwise require (a lot of) boilerplate code.
The items in request.POST (or request.GET) are send by the browser, so you can in no way guarantee that the items will (all) be present, and in the expected format.
Indeed, request.POST['somefield'] can raise a KeyError in case the POST request does not contain an key-value pair with somefield as key. Of course you can wrap this in a try … except …, but that easily will generate a large amount of code for a form with multiple items.
Even if the item is present in the request.POST, it is not per see in the expected format. For example if you use a number field (<input name="somefield" type="number">), you expect a number. But one can easily "forge" a HTTP request, where somefield contains 'blabla' or 'forty-two' instead. Django can validate the format of the fields. For an IntegerField [Django-doc] for example, it will require the value to be an optional minus sign followed by a sequence of digits.
A Django form will also "clean" the values. THis means that if you use an IntegerField, and later you access form.cleaned_data['somefield'], it will not have as type a string, but an int. You thus do not have to do the conversion yourself explicitly. This means that you can immediately add the values of two IntegerFields together for example, whereas if you do it yourself, these are first strings, and adding these together would concatenate the strings.
If cleaning or validating somehow fails, and a field contains an error, then a Form can explain what the error(s) are. You can access form.errors, which is a dictionary-like object that maps the names of the fields to a list of problems. It also makes it more convenient to list multiple errors. For example if a password should be at least 10 characters, and contain an uppercase, you can report both problems.
A ModelForm [Django-doc] also makes creating and updating an object more convenient. It has a .save() method for that. This thus will create a new record at the database in case the .instance wrapped in it was not yet saved, or update the record otherwise. It furthermore also will save ManyToManyFields (at least if you do not set commit=False). This is a tricky task since in order to save the objects in the many-to-many relations, you first need to create the object at the database side.
Finally you can also use a form to easily render it at the HTML side. There are Django apps like django-crispy-forms [GitHub] that can give the form another look. It thus makes it convenient to render the form, and easily change the appearance if you change your mind.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm facing problem when I run the app on my mobile and when I run the error with stack trace or with debug option there is no error so I don't know what to do
Here's the error message
Error:Execution failed for task
':app:transformClassesWithInstantRunForDebug'.
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
at line 1 column 1 path` $
This usually happens when your model is expecting a JSON array value but was surprised by a String value so it threw an exception. You can solve this in two different ways:
1- Edit your API response to return an empty JSON array or a JSON array with only one value in it. I mean always wrap your response value in a JSON array.
2- Edit your model to accept all data types, you can use Object as the data type for this value then cast it to an array or to a single value only.
Hope this helps, happy coding!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
In many languages multiline String literal syntax is
"""
Hello
World
"""
But why it require a new syntax, why just not to use " instead of """ ?
Like:
"Hello
World"
I think this is primarily to allow nested double quotes:
"""
Hello, "World"
"""
If " was used for multiline strings, then you had to escape nested quotes which is a bit inconvenient.
You can simply include formatted code like this:
val text = """
for (c in "foo")
print(c)
"""
or use special chars like " without the need to escape. It's very neat when it comes to JSON for example.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have response from server in this format :
{
"id":"552cd25444ae47fe5f3a41b2",
"eventType":"REST_EVENT",
"userSid":"REST_USER",
"content":{
"key":"value",
"key1":"value1",
"key2":"value2"
}
}
How I can map content field to Ember Data Model if this field has dynamic count of values and without knowing key names ?
Its really simple. Use this in your model
content : DS.attr()
Ember will automatically pick up if you pass array, string or number. In your case array.
You can even iterate over it. Watch it using Observers like any other array or model attribute in Ember.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
N-Triples is a line based serialization format for an RDF graph. Each line represents the subject, predicate and object of an RDF Triple separated by whitespace and ended with a dot like:
<http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> .
More details can be found here: http://www.w3.org/TR/n-triples/
But why is it necessary to define such a format, if one could serialize RDF Triples simply using CSV like
http://one.example/subject1, http://one.example/predicate1, http://one.example/object1
I could even easily extend to support N-Quads, N-Quints, ... using CSV. What are the advantages of N-Triples over CSV for serializing RDF triples?
Disclaimer: I'm the original editor of N-Triples and implemented it in Raptor http://librdf.org/raptor/ both the N-Triples original and the 2013 version.
There are multiple answers to this but it's basically ambiguity. CSV can't distinguish between a URI that looks like http://foo.com/ and a string http://foo.com/
In CSV
http://foo.com/,http://foo.com/,http://foo.com/
this could be a triple
(URI http://foo.com/, URI http://foo.com/, URI http://foo.com/)
or
(URI http://foo.com/, URI http://foo.com/, Literal http://foo.com/)
N-Triples adds <> and "" for distinguishing these cases