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 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 +``` diff --git a/es/packages/protoc-gen-es-cybozu-validate/README.md b/es/packages/protoc-gen-es-cybozu-validate/README.md index a6f1cf9..9d8f2a7 100644 --- a/es/packages/protoc-gen-es-cybozu-validate/README.md +++ b/es/packages/protoc-gen-es-cybozu-validate/README.md @@ -1 +1,82 @@ # @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: . +``` + +## Examples + +`protoc-gen-es-cybozu-validate` 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. + +## Limitation + +`protoc-gen-es-cybozu-validate` does not support following features: + +- RegExp validation +- UNICODE and PRECIS normalization