How to change Pydantic default __root__ example - python-3.x

So I have this class:
class Table(BaseModel):
__root__: Dict[int, Test]
and I'm using the __root__ since it is a dynamic value but when I go to /redoc (I'm using FastAPI) the example values it defaults to is property1, property2 as in the image below but I wanted the default example to be id_1, id_2 for example. How can I achieve this?
I tried changing the alias and title using the Field method of the library but didn't seem to work like I expected.

Have you tried doing it with the schema customization ?
class QcastTable(BaseModel):
__root__: Dict[int, Test]
class Config:
schema_extra = {
"examples": [
{
"id_1": {
"title": "yourTitle",
"description": "yourDescription",
},
"id_2": {
"title": "yourTitle2",
"description": "yourDescription2",
}
}
]
}

Related

How to create schema model depend on have data element on mongoDB in NodeJs?

How to create dynamically schema element depend on data have in Nodejs for MongoDB ? Which syntax can use for create, add , update data object element like as the follow json format.
WorkTasks: [
{
PerformedBy: "Joe",
StartDate: 2021-07-19T17:43:06.693+00:00,
EndDate: 2021-07-19T17:43:06.693+00:00,
Remarks: "Pad error",
Failure: "DDPP 20 - 090"
},
{
PerformedBy: "Karen",
StartDate: 2021-07-19T17:43:06.693+00:00
}
]
From Node.js Best Practices:
Though validation can be coded or relied upon classes and types
(TypeScript, ES6 classes) the community seems to increasingly like
JSON-based schemas as these allow declaring complex rules without
coding and share the expectations with the frontend. JSON-schema is an
emerging standard that is supported by many npm libraries and tools
(e.g. jsonschema, Postman), joi is also highly popular with sweet
syntax. Typically JSON syntax can't cover all validation scenario and
custom code or pre-baked validation frameworks like validator.js come
in handy.
Example - JSON-Schema validation rules
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"name": {
"description": "Name of the product",
"type": "string"
},
"price": {
"type": "number",
"exclusiveMinimum": 0
}
},
"required": ["id", "name", "price"]
}
Example - Validating an entity using JSON-Schema
const JSONValidator = require('jsonschema').Validator;
class Product {
validate() {
const v = new JSONValidator();
return v.validate(this, schema);
}
static get schema() {
//define JSON-Schema, see example above
}
}

How to use Pydantic base schema to n number of child schema

I'm new to pydantic, I want to define pydantic schema and fields for the below python dictionary which in the form of JSONAPI standard
{
"data": {
"type": "string",
"attributes":{
"title": "string",
"name": "string"
}
}
I managed to achieve this by defining multiple schemas like below,
class Child2(BaseModel):
title: str
name: str
class Child1(BaseModel):
type: str
attributes: Child2
class BaseParent(BaseModel):
data: Child1
But, I will be having multiple json request with the same json API structure as below,
example 1 {
"data": {
"type": "string",
"attributes":{
"source": "001",
"status": "New"
}
}
example 2 {
"data": {
"type": "string",
"attributes":{
"id": "001"
}
}
If you look into the above python dictionary, Values only under the attributes object are different. So, is there any way that I can define a parent marshmallow scheme for { "data": { "type": "string", "attributes":{ } } } and use that parent schema for all child schema's.
I found an answer finally, Hope this will help someone.
Pydantic 'create_model' concept will help to resolve this kind of requirement by passing child schema as one of the field values to create_model.
class Child(BaseModel):
title: str
name: str
class BaseParent(BaseModel):
data: create_model('BaseParent', type=(str, ...), attributes=(Child, ...))
And this will frame a BaseParent schema structure as below,
data=BaseParent(type='id', attributes=Child2(title='test002', name='Test'))

How to colorize specific React.Js syntax on vscode?

I'm trying to figure out how to get a specific color syntax with vscode in a declaration of a class in React.
These are the colors I want to get:
As you see, the name of the class StorePicker is a purple, and the React method .Component (including the dot) is a pale grey.
As far as I know, to play with color syntax on vscode, it needs to be with the TM Scope. So to make this, on my code file where I have the class declaration, I press Ctrl+Shift+p and search for Inspect TM Scopes, and click on the specific elements to get their respective scopes.
In my specific case, I got these scopes for the elements I need to colorize:
The class Name StorePicker (entity.name.class.js, source.js)
The React keyword (entity.name.class.js, source.js)
The . (keyword.operator.accessor.js, source.js)
The method Component (entity.name.class.js, source.js)
As you can see, the StorePicker (class name), the React keyword and the method Component share the same Scope: entity.name.class.js.
So let's say I want to colorize only the .Component. So I put this on my theme's config:
{
"name": "[JAVASCRIPT] - Operator Accesor + Method",
"scope": ["keyword.operator.accessor.js", "entity.name.class.js", "source.js"],
"settings": {
"foreground": "#c2cacf"
}
}
but StorePicker and React.Component are also colorized with the same color:
And I also want to colorize only the StorePicker (class name):
{
"name": "[JAVASCRIPT] - Only Class Name",
"scope": ["entity.name.class.js", "source.js"],
"settings": {
"foreground": "#d393e9"
}
}
Again not only is StorePicker colorized, but React and Component are colorized with the same color too:
My question:
How I can get them colorized separately like in the first image (taken from a Screencast), if they share the same Scope?
I have no idea how you got those scopes...
{
"scope": "entity.name.type.module.js",
"settings": { "foreground": "#e2b419" }
},
{
"scope": "entity.other.inherited-class.js",
"settings": { "foreground": "#c2cacf" }
},
{
"scope": "entity.name.type.class.js",
"settings": { "foreground": "#d393e9" }
},

Loopback referencesMany nested foreign key

I want to reference a different model(as discribed here: https://loopback.io/doc/en/lb2/Embedded-models-and-relations.html) but the by a nested id:
{
"name" : "person",
...
"relations": {
"cars": {
"type": "referencesMany",
"model": "car",
"foreignKey": "cars.id"
}
}
Person json will actually be something like:
{
...
cars: [{"id": 1, "name": "car1"}, ...]
}
And car model will be the full car details
Do I have to write my own remote method to do this?
Yosh DaafVader,
I've came accross this issue also and took time to find a solution ^^ but actually you just have to play with the parameter options inside your target relation property. The documentation states how the relation should be defined (sure the loopback cli does not include in version 3.x yet the way to use embeds nor references).
In your person model you have to change the foreignKey and to add the following options to be able to only use id to reference cars.
{
"name" : "person",
...
"relations": {
"cars": {
"type": "referencesMany",
"model": "car",
"foreignKey": "",
"options": {
"validate": true,
"forceId": true
}
}
}
Now you will be able to see in the explorer the new routes to add, remove and see the cars that belongs to the target person.
[Edit]
the foreignKey shall be blank, in order to be able to add items properly in the list of cars, or you can test and give some feedbacks about it
The validate option ensures the id exists in your database
forceId option will ensure it accepts only ids as a parameter
[/Edit]
Hope it will help :)
Cheers

Angular formly: calculate value of one field based on other fields input

json configuration:
{
"moduleconfigs": {
"create": [
{
"key": "Committed",
"type": "horizontalInput",
"templateOptions": {
"label": "Committed"
}
},
{
"key": "Uncommitted",
"type": "horizontalInput",
"templateOptions": {
"label": "Uncommitted"
}
},
{
"key": "Line",
"type": "horizontalInput",
"templateOptions": {
"label": "Line"
}
},{
"key": "Total",
"type": "horizontalInput",
"templateOptions": {
"label": "Total"
},
"expressionProperties":{
"value": function($viewValue, $modelValue, scope){
return scope.model.lineFill+scope.model.uncommitedBPD+scope.model.commitedBPD;
}
}
}
]
}
}
html:
<form>
<formly-for model="vm.myModel" fields="vm.myFields"></formly-form> </form>
I am new to angular formly. I am creating form using angular formly json. Total field should display sum of values provided in Committed+Uncommitted+Line fields. i am using expressionProperties but is not working.
I'm guessing you've moved on from this issue... however...
You are doing two things wrong.
First (1): They key value in the formly field configuration object is setting the value by that name on the model.
So your first field configuration object is:
{
"key": "Committed",
"type": "horizontalInput",
"templateOptions": {
"label": "Committed"
}
},
Then later you try to access that value using the key commitedBPDso you'll always get undefined.
Basically formly is setting the value input in that field on the model object with the key of Committed you need to change the key to match.
Second (2): I could be wrong but I don't think you can use an expression property to set the value like that. Formly will automatically respect value changes on the model so you're better off putting on onChange on the other formly field configuration objects that does the parsing and addition something like this:
{
"key": "Committed",
"type": "horizontalInput",
"templateOptions": {
"label": "Committed"
"onChange": addTotal
}
}...
function addTotal() {
//You have access to the model here because it's in your controller
// NOTE: the parseInput function you'll have to write yourself
vm.model.Total = parseInput(vm.model.Committed) + ...
}
All in all your biggest problem is trying to access the values from the model object with the wrong key
Yes, updating the model does not change form.input.value
The only way I've found is like this:
item.fieldGroup['Import'].formControl.setValue(333.33)

Resources