Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add README #30

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions es/packages/protobuf/README.md
Original file line number Diff line number Diff line change
@@ -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
```
81 changes: 81 additions & 0 deletions es/packages/protoc-gen-es-cybozu-validate/README.md
Original file line number Diff line number Diff line change
@@ -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