FormulaProperty: NotionRubyMapping 解説 (37)

はじめに

NotionRubyMapping 解説の第37回目です。今日は Formula プロパティの追加です。データベースは共通なものを使い回します。

Database properties (32-37)

irb(main):002> db = Database.find "https://www.notion.so/hkob/cad035c49e5346e78331cb5d05cbc754?v=a03e96ed79ad4d35808e7a08583396a0&pvs=4"
=> NotionRubyMapping::Database-cad035c49e5346e78331cb5d05cbc754

FormulaProperty

まず、FormulaProperty を追加し、fp に代入しておきます。

irb(main):003> fp = db.add_property FormulaProperty, "Formula"
=>
#<NotionRubyMapping::FormulaProperty:0x000000011e3bff00

FormulaProperty は数式を formula_expression = で変更できます。ここでは、 now() でを設定してみました。ところが

irb(main):005> fp.formula_expression = "now()"
=> "now()"
irb(main):006> db.save
/Users/hkob/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/notion_ruby_mapping-0.8.3/lib/notion_ruby_mapping/blocks/database.rb:162:in `rescue in update': {"object"=>"error", "status"=>400, "code"=>"validation_error", "message"=>"body failed validation: body.properties.Formula.formula.expression should be a string or `undefined`, instead was `null`.", "request_id"=>"d1c50e36-0772-48fc-8b9d-b26fe873e591"} by {"properties"=>{"Formula"=>{"formula"=>{"expression"=>nil}}}} (StandardError)

実際に Notion API に渡される payload を確認してみます。expression が null になってしまっています。

irb(main):015> print db.save dry_run: true
#!/bin/sh
curl -X PATCH 'https://api.notion.com/v1/databases/cad035c49e5346e78331cb5d05cbc754' \
  -H 'Notion-Version: 2022-06-28' \
  -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
  -H 'Content-Type: application/json' \
  --data '{"properties":{"Formula":{"formula":{"expression":null}}}}'=> nil

ただ、FormulaProperty の formula_expression も設定されています。また、property_schema_json を確認してみると、正しく設定されています。

irb(main):007> fp.formula_expression
=> "now()"
irb(main):008> fp.property_schema_json
=> {"Formula"=>{"formula"=>{"expression"=>"now()"}}}

おわりに

今回は、うまく FormulaProperty が作成できませんでした。Database 作成時には作成できた覚えがあるので、プロパティの追加の場合にうまく expression が渡されない事象があるバグが残っていそうです。あとでデバッグしてみます。

FormulaProperty のマニュアルはこちらです。

FormulaProperty

NotionRubyMapping解説