- Notifications
You must be signed in to change notification settings - Fork0
Narazaka/activehashie-validation.js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Rails等のマスターデータをJavaScript/TypeScriptでチェック可能にするツールです。
validateModel(Item,({validates, validate})=>{validates("name","presence",true);validates("name","uniqueness",true);validates("type","presence",true);validate((errors,items)=>{constids=items.where().not({type:["a","b"]}).pluck("id");if(ids.length){errors.push({column:"type", ids,message:"typeは'a', 'b'のいずれかであるべきです"});}});validates("itemGroup","presenceBelongsTo",true);// 参照先の存在をチェック});
rails_app/db/seeds.rbにてデータをテーブルごとにymlファイルから読み込む仕組みになっているとします。
下記のようにchecker/にチェック用の内容を書いて、checker/フォルダでahevコマンドを実行すると、エラー内容が記載されたreport.htmlが生成されます。
なおこの内容はexamples/の下にあります。
rails_app/db/seeds/items.yml:
data1:id:1name:item1type:aitem_group_id:1data2:id:2name:item2type:aitem_group_id:1data3:id:3name:item3type:bitem_group_id:2data4:id:4name:item3type:citem_group_id:2
rails_app/db/seeds/item_groups.yml:
data1:id:1name:group1data2:id:2name:group2data3:id:3name:group2
checker/models/application_record.ts:
import{ActiveHashRecord,ActiveHashRecordBase,ActiveYaml,}from"activehashie";constrootPath="../rails_app/db/seeds";exportclassApplicationRecord<RecordextendsActiveHashRecord>extendsActiveYaml<Record>{constructor(name:string,recordClass:new(source:ActiveHashRecordBase)=>Record,indexColumns?:Array<keyof(Record)>,){super(name,recordClass,{rootPath, indexColumns});}}// displayNameメソッドを定義しておくと、エラー表示の時にそのレコードを表すものとして使われます。(<any>ActiveHashRecord.prototype).displayName=function(){returnthis.name;};
checker/models/item.ts:
import{ActiveHashRecord}from"activehashie";import{validate}from"activehashie-validation";import{ApplicationRecord}from"./application_record";import{ItemGroup,ItemGroupRecord}from"./item_group";exportclassItemRecordextendsActiveHashRecord{name:string;type:"a"|"b";item_group_id:number;getitemGroup():ItemGroupRecord|undefined{returnthis.belongsTo(ItemGroup);}displayName(){constitemGroup=this.itemGroup;return`${itemGroup ?itemGroup.name :""} -${this.name} [${this.type}]`;}}exportconstItem=newApplicationRecord("Item",ItemRecord);validate(Item,({validates, validate})=>{validates("name","presence",true);validates("name","uniqueness",true);validates("type","presence",true);validate((errors,model)=>{constids=model.where().not({type:["a","b"]}).pluck("id");if(ids.length){errors.push({column:"type", ids,message:"typeは'a', 'b'のいずれかであるべきです"});}});validates("itemGroup","presenceBelongsTo",true);// 参照先の存在をチェックします});
checker/models/item_group.ts:
import{ActiveHashRecord}from"activehashie";import{validate}from"activehashie-validation";import{ApplicationRecord}from"./application_record";import{Item}from"./item";exportclassItemGroupRecordextendsActiveHashRecord{name:string;getitems(){returnthis.hasMany(Item);}}exportconstItemGroup=newApplicationRecord("ItemGroup",ItemGroupRecord);validate(ItemGroup,({validates})=>{validates("name","presence",true);validates("name","uniqueness",true);});
checker/spec/length.ts:
import{validate}from"activehashie-validation";import"../models/item";import{ItemGroup}from"../models/item_group";validate(ItemGroup,({validate})=>{validate((errors,model)=>{if(model.length<10){errors.push({message:"ItemGroupは10個以上必要です"});}});});
db/schema.rbからTypeScriptコードを自動生成できます。
npx generate-model-ar-schema ./db/schema.rb ../checker
以下のファイルが生成されます
- ApplicationTable.ts (同名ファイルがなければ生成 テーブルのベースクラスファイル)
- Models.ts (テーブルの定義ファイル)
- Extensions.ts (同名ファイルがなければ生成 テーブルの拡張メソッドを定義するためのファイル)
- ModelAndExtensions.ts (テーブル拡張メソッドを便利に使うための定義ファイル)
生成されるExtensions.tsにテーブル名TableExt(ActiveRecordのscope等)、テーブル名RecordExt(ActiveRecordのインスタンスメソッド)を定義していくことができます。
This is released underMIT License.
About
activehashie Validation
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.