投稿時間:2022-06-06 11:14:19 RSSフィード2022-06-06 11:00 分まとめ(14件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
IT 気になる、記になる… Appleの整備済み商品情報 2022/6/6 − M1チップ搭載モデル多数追加 https://taisy0.com/2022/06/06/157705.html apple 2022-06-06 01:27:09
IT ITmedia 総合記事一覧 [ITmedia ビジネスオンライン] トヨタ、住宅用蓄電池システム販売 停電時の電力共有も可能に https://www.itmedia.co.jp/business/articles/2206/06/news064.html itmedia 2022-06-06 10:35:00
TECH Techable(テッカブル) 「中小だから、地方だから」は通用しない。面接DXで立ち向かう、採用力格差がなくなる時代 https://techable.jp/archives/179920 中小企業 2022-06-06 01:00:37
海外TECH DEV Community Response validation with Yup https://dev.to/hurricaneinteractive/response-validation-with-yup-2018 Response validation with YupProblem Many times in our Frontend we just accept that an API response is what it should be In Typescript we hide behind generics to type cast but what if our API is a success with a data structure that we didn t expect This happened a few times in a recent project The backend logic for the API hit about different services that we had no control over each of these are points of failure Sometimes one would silently fail causing the API to be a with invalid data I had a great time Here is what I am talking about async function getMe try const response await fetch const json Profile await response json Surely json will be the shape me need nothing can go wrong renderMe json catch error Nothing will ever go wrong console error error Now of the time this is fine and of the time I do this as well Probably shouldn t but here we are We kinda assume that if something goes wrong with the response then the catch will catch it Otherwise we re all good This doesn t just happen with custom fetch calls In React if you use a fetch hook many times it will allow you to pass in generics useFetch lt Profile gt to say what the shape of the data will be Again this works I do it but there isn t a lot of safety from incorrect data Idea I have been thinking about is using a validation library in this case yup to add a extra layer of protection this idea will work with any validation library Usually if we re working with forms we already have a validation library installed so we aren t really introducing extra dependencies into our project Additionally if you re a Typescript user these libraries can make type definitions a lot easier as well Looking at our example above we need to introduce extra things One is our schema and the other is validating our json SchemaContinuing with the get profile idea we ll create a profile schema Depending on how you like to structure your projects This could be in a profile schema ts or profile model ts file Allowing you to separate things a little easier import object string date from yup export const profile object email string email required name string required birthday date required For Typescript users you can import InferType from yup and export the Profile type export type Profile InferType lt typeof profile gt Validate the dataNow that we have our profile definition we can validate our json and handle any ValidationError that yup might throw import ValidationError from yup async function getMe try const response await fetch const json await response json const data await profile validate json stripUnknown true renderMe data catch error if error instanceof ValidationError alert The response data is invalid return alert Uncaught error occured You will notice a few things are different here We have removed our generics If the validate call is successful then we can be confident that data is in our Profile shape In the catch block we can now test for this ValidationError and provide the user some extra details about the issue instead of a generic Something went wrong message Optional I also passed in stripUnknown true to the validate options As the name suggests it will remove any data that isn t in our profile schema This makes the data more consistent but also forces someone to update the schema if additional data is added Using a hook libraryIn the case that you are using a fetch hook of some description Some of them may have a validation option where you can do the same thing Alternatively I ve seen that many allow for a transform step Giving you a chance to change the data before returning it to the user const data loading error useFetch transform async json gt const data await profile validate json return data That s all folksAaaand that is it Nothing else to really add If you take anything away from this it would be don t fully trust that your data is as expected Adding additional checks in your components or logic won t hurt anyone Validation libraries are usually very performant and already installed in many projects utilising them to standardise schema definitions type definitions and API data may provide some additional benefits to your projects It could also help with mocking data I am sure there are libraries out there that can take one of these schemas and output some JSON that matches the structure Below is a Codesandbox hopefully it shows up with this idea implemented feel free to play around a bit I did set the console to be open but it sometimes vanishes so it might be best to open it in a different tab Play around with the me function and return some weird data to see if the validation works Peace ️Bit of a disclaimer the ideas in this article are just ideas I haven t been able to test them out fully yet Complex data structures or conditional responses might require a more complex schema I have noticed with complex schemas that the InferType becomes a everything is optional type which isn t always ideal 2022-06-06 01:06:11
金融 ニュース - 保険市場TIMES SOMPOひまわり生命、「マイリンククロス」の登録者数が50万人に到達 https://www.hokende.com/news/blog/entry/2022/06/06/110000 SOMPOひまわり生命、「マイリンククロス」の登録者数が万人に到達健康を応援SOMPOひまわり生命保険株式会社は月日、同社が提供する登録制ウェブサービス「マイリンククロス」への登録者数が万人に到達したと発表した。 2022-06-06 11:00:00
海外ニュース Japan Times latest articles Russia strikes Kyiv for first time in weeks as battle rages in east https://www.japantimes.co.jp/news/2022/06/06/world/russia-strike-kyiv/ Russia strikes Kyiv for first time in weeks as battle rages in eastUkraine said the strike hit a rail car repair works while Moscow said it had destroyed tanks sent by Eastern European countries to Ukraine 2022-06-06 10:22:04
海外ニュース Japan Times latest articles U.K.’s Boris Johnson braced for Tory rebels to force confidence vote in days https://www.japantimes.co.jp/news/2022/06/06/world/politics-diplomacy-world/boris-johnson-no-confidence-vote/ U K s Boris Johnson braced for Tory rebels to force confidence vote in daysUncertainty over Johnson s future comes as Britons grapple with soaring energy bills and the highest inflation in four decades 2022-06-06 10:21:30
海外ニュース Japan Times latest articles New Australian PM reaches out to China over fighter jet encounter https://www.japantimes.co.jp/news/2022/06/06/asia-pacific/politics-diplomacy-asia-pacific/australia-anthony-albanese-china/ New Australian PM reaches out to China over fighter jet encounterLeader Anthony Albanese said the incident was a safety threat to the Australian aircraft and its crew and that he had reached out to Beijing 2022-06-06 10:16:24
GCP Google Cloud Platform Japan 公式ブログ ハマヤ:Google Workspace を導入し、年間 5,760 時間の削減に成功 https://cloud.google.com/blog/ja/topics/customers/hamaya-saves-time-with-google-workspace/ それをスプレッドシートに記入してもらって自動集計をするシステムなのですが、もともとは全部署を回覧していたものですから、全従業員が効果を実感できます。 2022-06-06 03:00:00
北海道 北海道新聞 インディカー第7戦、佐藤13位 ウィル・パワーが今季初勝利 https://www.hokkaido-np.co.jp/article/689957/ 自動車 2022-06-06 10:14:00
北海道 北海道新聞 バレーボール、日本が開幕4連勝 ネーションズリーグ女子 https://www.hokkaido-np.co.jp/article/689955/ 開幕 2022-06-06 10:12:00
北海道 北海道新聞 カツカレー、北見の新ご当地グルメに 北見工大・升井教授、食べ歩いてデータ分析 https://www.hokkaido-np.co.jp/article/689910/ 教授 2022-06-06 10:02:01
北海道 北海道新聞 八雲の熊石アワビ、フェス中止で割安販売 町外からの予約も受け付け https://www.hokkaido-np.co.jp/article/689918/ 販売 2022-06-06 10:05:09
GCP Cloud Blog JA ハマヤ:Google Workspace を導入し、年間 5,760 時間の削減に成功 https://cloud.google.com/blog/ja/topics/customers/hamaya-saves-time-with-google-workspace/ それをスプレッドシートに記入してもらって自動集計をするシステムなのですが、もともとは全部署を回覧していたものですから、全従業員が効果を実感できます。 2022-06-06 03:00:00

コメント

このブログの人気の投稿

投稿時間:2021-06-17 22:08:45 RSSフィード2021-06-17 22:00 分まとめ(2089件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)