How to validate a json field using Yup? - yup

I have this:
const jsonField = { [key]: value };
How can I validate that with Yup?
Keeping in mind that the shape of the object changes a lot.

Found it!
It appears that Yup has a mixed validator to use in this case.
Yup.mixed()

Related

yup - is there any way to set the default value for a string field to be something without defining it for each one

I want that every time I use yup.string(), it will add a specific default value for it
for example:
const schema = yup.object({
text: yup.string()// I want it to also do .default('some string') in the background,
});
or - another option - is there any way to set the default value after creating the scheme? something like setDefault('text', 'some string')
The closest solution I came across to solve your issue is extending your string with a custom method that implements your needs. To do that you need to use addMethod from yup:
import { addMethod, string } from 'yup';
addMethod(string, 'append', function append(appendStr) {
return this.transform((value) => `${value}${appendStr}`);
});
Now, you can use your custom method (append) and apply it to any string you want:
string().append('~~~~').cast('hi'); // 'hi~~~~'
If you want to add the custom method to all your schema types like date, number, etc..., you need to extend the abstract base class Schema:
import { addMethod, Schema } from 'yup';
addMethod(Schema, 'myCustomMethod', ...)
Extra
For Typescript
In your type definition file, you need to declare module yup with your custom method's arguments and return types:
// globals.d.ts
import { StringSchema } from "yup";
declare module 'yup' {
interface StringSchema<TType, TContext, TDefault, TFlags> {
append(appendStr: string): this;
}
}
Unknow behavior for transform method
While I was trying to extend the functionality of the date schema with a custom method that transform the date that user enters from DD-MM-YYY to YYYY-MM-DD, the custom method broke after I used it with other methods like min, max for example.
// `dayMonthYear` should transform "31-12-2022"
// to "2022-12-31" but for some reason it kept
// ignoring the `cast` date and tried to transform
// `1900` instead!
Yup.date().dayMonthYear().min(1900).max(2100).required().cast("31-12-2022") // error
To work around this issue, I appended my custom method at the end of my schema chain:
Yup.date().min(1900).max(2100).required().cast("31-12-2022").dayMonthYear() // works as expected
This issue is mentioned in this GH ticket which I recommend going through it as it's going more in-depth on how to add custom methods with Typescript.
References
addMethod
Extending built-in schema with new methods
Example of addMethod in Typescript (GH ticket)

Symfony Form - Value Object, DataMappers and EventSubscriber

I am having trouble to dynamically modify field in my form using DataMapper and EventSubscriber.
Here is my form:
A select field
A field which will be modified by the select field above.
I am using an EventSubscriber to dynamically modify my form using AJAX.
And a DataMapper to map my Value Object to the form and vice-versa.
So when i do that:
$moneyForm = $this->createForm(MoneyType::class);
Everything is working. But when i pass my Value Object as data class:
$money = new Money(199, 'USD');
$moneyForm = $this->createForm(MoneyType::class, $money);
I got an error here:
public function mapDataToForms($viewData, $forms)
{
$forms = iterator_to_array($forms);
$forms['money']->setData($viewData ? $viewData->money() : 0);
$forms['currency']->setData($viewData ? $viewData->currency() : 'USD');
}
This error says that: Notice: Undefined index:.
It seems like the form has been replaced by a new one and i don't understand it.
I don't know why when i use a data mapper alone, or event subscriber alone everything is working.
But when i try to mix, both of them i got this error.
Does anyone have a clue of what's going on here?
Thank you

How to PUT (save) object to REST endpoint using Restangular

Given I have resource id and data as plain JSON, how do I save that JSON to /cars/123/ ???
There seem to be no clear explanation. I don't have restangular element.
restangularizeElement might be the option, but it lacks any meaningful documentation.
All I know is that I want to save {make: 'honda', color: 'now blue, was red'} for car_id == 123.
Thanks.
If you have plain object you cannot use Restangular save function to update (it will make put request as object has id) object.
Easiest way to achieve it construct your end point url then call put function...
Restangular.one('cars', 123).customPUT(carObject).then(function(response){
TO-DO
})
If you want to restangularized your plain object you can use this one...
Restangular.restangularizeElement(null,carObject, 'cars').put().then(function (response) {
TO-DO
})
The comman way should be like this. Whenever you get object from backend with get or getList your object will be Restangularized unless you do not choose to turn them plain object by calling .plain() method of Restangular response. Then you can safely call .save() and it will automatically will be put or post accordingly...
Here is what worked for me. Thanks to #wickY26:
updateGroup: function (group, id_optional) {
if (!group.hasOwnProperty('restangularCollection')) {
// safe to assume it is NOT restangular element; sadly instanceof won't work here
// since there is no element class
// need to restangularize
group = Restangular.restangularizeElement(null, group, ENDPOINT);
group.id = id_optional;
}
return group.put();
},

unity,how to use generic by using js?

When I write like this
List.<Dictionary.<String, System.Object>>,
the ide tells me
Assets/Scripts/yhj/Model/PrintItem.js(23,71): BCE0044: expecting >, found '>>.
How can I resolve it?
Why would you make a list of dictionaries? Can't you just use the dictionary as the list with the key, value input instead of this? If you wanted to use it like this I would either define an object to be the <String, System.Object> and inserting that as the value and just leave the key as the number.
Or making a list of objects where the object is the <String, System.Object>
#ILiveForVR makes me think about this.thanks.
I solved it, but I'm not sure if the solution is best.
I do it like this:
var list = List.<System.Object>;
for(var i=0;i<list.Count;i++){
var data = list[i] as Dictionary<String, System.Object>;
}

CodeIgniter: URIs and Forms

I'm implementing a search box using CodeIgniter, but I'm not sure about how I should pass the search parameters through. I have three parameters: the search string; product category; and the sort order. They're all optional. Currently, I'm sending the parameters through $_POST to a temporary method, which forwards the parameters to the regular URI form. This works fine. I'm using a weird URI format though:
http://site.com/products/search=computer,sort=price,cat=laptop
Does anyone have a better/cleaner format of passing stuff through?
I was thinking of passing it into the products method as arguments, but since the parameters are optional things would get messy. Should I suck it up, and just turn $_GET methods on? Thanks in advance!
Query Strings
You can enable query strings in CodeIgniter to allow a more standard search function.
Config.php
$config['enable_query_strings'] = FALSE;
Once enabled, you can accept the following in your app:
http://site.com/products/search?term=computer&sort=price&cat=laptop
The benefit here is that the user will find it easy to edit the URL to make a quick change to their search, and your search uses common search functionality.
The down side of this approach is that you are going against one of the design decisions of the CodeIgniter development team. However, my personal opinion is that this is OK provided that query strings are not used for the bulk of your content, only for special cases such as search queries.
A much better approach, and the method the CI developers intended, is to add all your search parameters to the URI instead of a query string like so:
http://site.com/products/search/term/computer/sort/price/cat/laptop
You would then parse all the URI segments from the 3rd segment ("term") forward into an array of key => value pairs with the uri_to_assoc($segment) function from the URI Class.
Class Products extends Controller {
...
// From your code I assume you are calling a search method.
function search()
{
// Get search parameters from URI.
// URI Class is initialized by the system automatically.
$data->search_params = $this->uri->uri_to_assoc(3);
...
}
...
}
This would give you easy access to all the search parameters and they could be in any order in the URI, just like a traditional query string.
$data->search_params would now contain an array of your URI segments:
Array
(
[term] => computer
[sort] => price
[cat] => laptop
)
Read more about the URI Class here: http://codeigniter.com/user_guide/libraries/uri.html
If you're using a fixed number of parameters, you can assign a default value to them and send it instead of not sending the parameter at all. For instance
http://site.com/products/search/all/somevalue/all
Next, in the controller you can ignore the parameter if (parameter == 'all'.)
Class Products extends Controller {
...
// From your code I assume that this your structure.
function index ($search = 'all', $sort = 'price', $cat = 'all')
{
if ('all' == $search)
{
// don't use this parameter
}
// or
if ('all' != $cat)
{
// use this parameter
}
...
}
...
}