How to get failed validation rule? - knockout-validation

I'm using knockout validation plugin. I have a few validation rules for my observable.
Is it possible to define which rule failed during the validation?
I tried to use method rules(), but it just returns list of the attached rules, but not a state of the some rule (failed or not):
http://i.imgur.com/5YvHPiw.png?1?8272
I've extended observable in this way:
self.SomeField = ko.observable().extend({
required: {
message: "The field is required.",
params: true
},
maxLength: {
message: "Please enter no more than 300 characters.",
params: 300
}
});
My not validated observable looks like:
http://i.imgur.com/7ybfLfZ.png
Thank you.

Related

How to return 400 response using the same format that Asp.Net Core validation uses?

I use model validation in Asp.Net Core and it returns 400 errors like that:
{
"errors": {
"MyProperty": [
"Error 1",
"Error 2"
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "0HLRTF1TPCO60:00000002"
}
I do have my custom validation that does not use Asp.Net core built-in mechanisms, but I'd like to generate the error in exact same format so that it would be easier for users to consume. I can of course just generate JSON myself, but I think there is some built-in API to generate such an error from model, though I cannot manage to find what should I call.
P.S. I want to do this in middleware
I have figured out myself how this can be achieved:
var modelState = new ModelStateDictionary();
modelState.AddModelError("FieldName", "ErrorMessage");
var details = new ValidationProblemDetails(modelState);
details.Status = (int?) HttpStatusCode.BadRequest;
details.Extensions["traceId"] = context.TraceIdentifier;
The details can later be serialized to JSON.

Swagger: How to get formatted example json

I'm really struggling to understand the format of the examples section of a response. I have the following response defined for a 500 Internal Sever error.
500InternalServerError:
description: The server encountered an unexpected condition which prevented it from fulfilling the request
schema:
allOf:
- $ref: '#/definitions/Failure'
headers:
X-Rate-Limit-Limit:
description: The number of allowed requests in the current period
type: integer
X-Rate-Limit-Remaining:
description: The number of remaining requests in the current period
type: integer
X-Rate-Limit-Reset:
description: The number of seconds left in the current period
type: integer
examples:
application/json:
code: -1
message: The server encountered an unexpected condition which prevented it from fulfilling the request
When I load it up in swagger-ui, it looks like this:
How do I get the response to be formatted over multiple lines and look like this?:
{
"code": "-1",
"message": "The server encountered an unexpected condition which prevented it from fulfilling the request"
}
The lack of pretty print in the response-level examples seems to be a bug or missing functionality in Swagger UI 3.0.x. Feel free to submit an issue on GitHub.
The workaround is to use a schema-level example instead:
definitions:
Failure:
type: object
...
example:
code: "-1" # Quotes force the number to be treated as a string
message: The server encountered an unexpected condition which prevented it from fulfilling the request
By the way, you do not need allOf when using $ref alone (without combining it with other items):
schema:
$ref: '#/definitions/Failure'

Show all JIRA issues closed after its Internal Deadine date?

We have a custom field called Internal Deadline in our JIRA but how do I find all issues that has already been Closed but has surpassed its deadline?
{
id: "customfield_6524",
name: "Internal Deadline",
custom: true,
orderable: true,
navigable: true,
searchable: true,
clauseNames: [
"cf[6524]",
"Internal Deadline"
],
schema: {
type: "date",
custom: "com.atlassian.jira.plugin.system.customfieldtypes:datepicker",
customId: 6524
}
}
I have no problem in checking where we have missed the deadline now() but I want the historical data too but there is no possibility to chose my Closed status in the JQL:
Trying to put the Resolved date results in this error:
Date value 'Resolved' for field 'Internal Deadline' is invalid.
Valid formats include: 'YYYY/MM/DD', 'YYYY-MM-DD', or a period
format e.g. '-5d', '4w 2d'.
Maybe JIRA has only been designed with the here-and-now date in scope? I had hoped I could monitor the "delivery track record".
You should download Script runner plugin (free), that provides some already implemented functions for JQL querying. Once you have it installed, you just can use this query:
issueFunction in dateCompare("","Internal Deadline < Resolved")
For further reference see the docs for dateCompare here, https://scriptrunner.adaptavist.com/latest/jira/jql-functions.html#_datecompare

Syntax for submitting a mutation to a graphql-relay mutationWithClientMutationId

I defined a GraphQL Mutation using graphql-relay but am having issues figuring out how to submit a mutation to it.
Here is the relevant schema:
const userType = new GraphQLObjectType({
name: 'User',
description: 'user',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLString),
description: 'The UUID for the user.',
resolve(user) {
return user.uuid;
},
},
})
});
const registerUser = mutationWithClientMutationId({
name: 'registerUser',
inputFields: {
},
outputFields: {
user: {
type: userType,
resolve: (payload) => {
models.user.findById(payload.userId);
}
},
},
mutateAndGetPayload: (args) => {
var newUser = models.user.build().save();
return {
userId: newUser.id,
};
}
});
const rootMutation = new GraphQLObjectType({
name: 'RootMutationType',
fields: {
registerUser: registerUser,
},
});
const schema = new GraphQLSchema({
query: rootQuery,
mutation: rootMutation,
});
What should an HTTP call look like to register a new user and get back the userId?
Thanks!
I want to point out that I see that you're saying that your mutation requires no parameters - how does it know what the new user's details are? You'll probably need some parameters on that mutation, eventually. They would be available to your mutateAndGetPayload on that first function parameter. (I'm not saying every mutation needs parameters, but this one probably does)
If you're using Relay, there is some pretty good information on the official document as to how to use your mutations from Relay. Particularly down at the bottom where it shows the various mutator configs. If you're using connections, you may want to use RANGE_ADD to add this new account to the Relay store manually, otherwise if you'd like to perform a more broad refetch you can use FIELDS_CHANGE. You said you need the new user id after the mutation finishes. If you're using Relay, you may need to look into REQUIRED_CHILDREN to specify that regardless of the computed query that Relay will build, you always want that id to be queried.
The output of your mutation is a userType, so you'd be able to access it with a fragment on the payload type, which would probably be RegisterUserPayload, that might look something like ...
fragment on RegisterUserPayload {
user {
id
}
}
Now, that's assuming you're using Relay. If you'd like to try this out manually via GraphiQL, then you can use the examples of how to do mutations through there on the GraphQL Mutation docs. There's a direct example of how you'd query your mutation.
Last, since you asked how to do this at a low level of issuing the HTTP request yourself, for that you can look at express-graphql documentation, which explains how to query it.
I figured out a mutation format that worked:
mutation RootMutationType {
registerUser(input:{clientMutationId:"123"}){
clientMutationId, user { id }
}
}

ASP.NET MVC with jQuery Validation - messages customization and localization

I have ASP.NET MVC (4) project which localization is supported by the framework. Should I change browser settings to another language, framework automatically picks up the right resource file.
However, because I am using knockoutjs I fall back to jQuery validation at those views. Unfortunately there's no automatic support for localization there.
My question is - what are the best practices and ways to customize and localize jQuery validation messages so they will be picked automatically together with all MVC resources?
Something that jQuery validation messages will behave in a similar manner to Data Annotations messages given resources and message Ids.
In particular -
How to I make jQuery pick up the message I want from the resources instead of its default "This field is required", so it will print something like "Please enter email" and
How can I make jQuery print the same customized message in another language automatically should I change browser language ?
Thank you in advance.
1) How to I make jQuery pick up the message I want from the resources instead of its default "This field is required", so it will print something like "Please enter email".
The following is called to over-ride messages at any time. The strings below can be replaced with variables.
jQuery.extend(jQuery.validator.messages, {
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits.",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
minlength: jQuery.validator.format("Please enter at least {0} characters."),
rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
range: jQuery.validator.format("Please enter a value between {0} and {1}."),
max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});
Otherwise, you can get more specific when declaring your rules within .validate().
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
rules: {
field1: {
required: true,
minlength: 5
}
},
messages: {
field1: {
required: "custom message for field 1 required",
minlength: "custom message: {0} chars required"
}
}
});
});
DEMO: http://jsfiddle.net/XV3ZR/
To dynamically change any messages after the plugin is first initialized, requires an over-ride by using the rules('add') method.
$('#field1').rules('add', {
messages: {
required: "field 1 required",
minlength: "{0} chars required"
}
});
DEMO 2: http://jsfiddle.net/PJGgE/1/
2) How can I make jQuery print the same customized message in another language automatically should I change browser language ?
I'm not sure what you mean by "change browser language", but again, the methods in #1 above are the only ways, AFAIK. These are just strings and you'll have to translate them manually or via outside methods.
I think that you can give on the server to the client script file with localization depending client's browser settings (something like i18n)

Resources