From 1cce26878bf03274d5e0ab6d469bfd6fa6ddefed Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 13 Aug 2023 17:08:16 +0900 Subject: [PATCH 1/5] Update README --- .../protoc-gen-es-cybozu-validate/README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/es/packages/protoc-gen-es-cybozu-validate/README.md b/es/packages/protoc-gen-es-cybozu-validate/README.md index a6f1cf9..d28652c 100644 --- a/es/packages/protoc-gen-es-cybozu-validate/README.md +++ b/es/packages/protoc-gen-es-cybozu-validate/README.md @@ -1 +1,24 @@ # @cybozu/protoc-gen-es-cybozu-validate + +A protoc plugin that generates validators for TypeScript from the custom fields defined in [`cybozu.validate`](../../../README.md#cybozuvalidate). + +## Inatall + +``` +npm i @cybozu/protoc-gen-es-cybozu-validate +``` + +## Usage + +The following is an example `buf.gen.yaml` to generate validation code: + +```yaml +version: v1 +plugins: + - plugin: es + out: . + opt: + - target=ts + - plugin: es-cybozu-validate + out: . +``` From bf73ff4087b340f0e7980256bebeebbb5e5cae65 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 13 Aug 2023 17:21:00 +0900 Subject: [PATCH 2/5] Add example section --- .../protoc-gen-es-cybozu-validate/README.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/es/packages/protoc-gen-es-cybozu-validate/README.md b/es/packages/protoc-gen-es-cybozu-validate/README.md index d28652c..4336a27 100644 --- a/es/packages/protoc-gen-es-cybozu-validate/README.md +++ b/es/packages/protoc-gen-es-cybozu-validate/README.md @@ -22,3 +22,54 @@ plugins: - plugin: es-cybozu-validate out: . ``` + +## Examples + +This protoc plugin takes the following message type as input: + +```proto +message Scalars { + float float = 1 [(cybozu.validate.rules).float = {lt: 3.2}]; + double double = 2 [(cybozu.validate.rules).double = {gt: 3.2}]; +} +``` + +And outputs the following TypeScript code: + +```ts +export const ScalarsValidators: { + /** + * @generated from field: float float = 1; + */ + validateFloat: (value: unknown) => asserts value is Scalars["float"]; + /** + * @generated from field: double double = 2; + */ + validateDouble: (value: unknown) => asserts value is Scalars["double"]; +} = { + /** + * @generated from field: float float = 1; + */ + validateFloat(value) { + if (typeof value !== "number") { + throw new CybozuValidateTypeError("number", typeof value); + } + if (value >= 3.200000047683716) { + throw new CybozuValidateNumberRuleError({ lt: 3.200000047683716 }, value); + } + }, + /** + * @generated from field: double double = 2; + */ + validateDouble(value) { + if (typeof value !== "number") { + throw new CybozuValidateTypeError("number", typeof value); + } + if (value <= 3.2) { + throw new CybozuValidateNumberRuleError({ gt: 3.2 }, value); + } + }, +}; +``` + +This is designed to provide validation functions for each field, not for each message type, as it is intended for frontend form validation. From ad6f87c385c6bfcd619f43e7a07ac60e2605d987 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 13 Aug 2023 17:23:08 +0900 Subject: [PATCH 3/5] Add Limitation section --- es/packages/protoc-gen-es-cybozu-validate/README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/es/packages/protoc-gen-es-cybozu-validate/README.md b/es/packages/protoc-gen-es-cybozu-validate/README.md index 4336a27..9d8f2a7 100644 --- a/es/packages/protoc-gen-es-cybozu-validate/README.md +++ b/es/packages/protoc-gen-es-cybozu-validate/README.md @@ -25,7 +25,7 @@ plugins: ## Examples -This protoc plugin takes the following message type as input: +`protoc-gen-es-cybozu-validate` takes the following message type as input: ```proto message Scalars { @@ -73,3 +73,10 @@ export const ScalarsValidators: { ``` This is designed to provide validation functions for each field, not for each message type, as it is intended for frontend form validation. + +## Limitation + +`protoc-gen-es-cybozu-validate` does not support following features: + +- RegExp validation +- UNICODE and PRECIS normalization From 0b7abb702f3807d3119078b84d9fc6c1e7e3e5ca Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 13 Aug 2023 17:30:13 +0900 Subject: [PATCH 4/5] Update docs --- es/packages/protobuf/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/es/packages/protobuf/README.md b/es/packages/protobuf/README.md index fc61ef5..82f8b2a 100644 --- a/es/packages/protobuf/README.md +++ b/es/packages/protobuf/README.md @@ -1 +1,11 @@ # @cybozu/protobuf + +A library that provides JavaScript code generated from the Protocol Buffers schema provided by this repository. + +**This package is not intended to be used by external applications as a library.** It is used as an internal library for creating protoc plugins such as [`protoc-gen-es-cybozu-validate`](../protoc-gen-es-cybozu-validate/). + +## Inatall + +``` +npm i @cybozu/protobuf +``` From b55c819edd607af4192fb369ac4433c9c79577d0 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 17 Sep 2023 22:39:48 +0900 Subject: [PATCH 5/5] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 74dc076..b726187 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ plugins: opt: paths=source_relative ``` +If you want to generate TypeScript validation code, please see [`@cybozu/protoc-gen-es-cybozu-validate`](es/packages/protoc-gen-es-cybozu-validate/README.md). + ## API documentation Visit https://buf.build/cybozu/protobuf