Skip to content

Commit 5129ec3

Browse files
Copilotstreamich
andcommitted
Implement string format validation with ASCII and UTF-8 support
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent 3eb913a commit 5129ec3

File tree

7 files changed

+471
-133
lines changed

7 files changed

+471
-133
lines changed

src/json-schema/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface JsonSchemaString extends JsonSchemaGenericKeywords {
1616
type: 'string';
1717
const?: string;
1818
format?: string;
19+
pattern?: string;
1920
minLength?: number;
2021
maxLength?: number;
2122
}

src/schema/schema.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,21 @@ export interface NumberSchema extends TType<number>, WithValidator {
116116
export interface StringSchema extends TType<string>, WithValidator {
117117
kind: 'str';
118118

119+
/**
120+
* String format specification. When set, the string value will be validated
121+
* according to the specified format for maximum performance.
122+
*
123+
* - "ascii" - Only ASCII characters (0-127) are allowed
124+
* - "utf8" - Valid UTF-8 encoded strings are allowed
125+
*/
126+
format?: 'ascii' | 'utf8';
127+
119128
/**
120129
* When set to true, means that the string can contain only ASCII characters.
121130
* This enables a range of optimizations, such as using a faster JSON
122131
* serialization, faster binary serialization.
132+
*
133+
* @deprecated Use `format: 'ascii'` instead.
123134
*/
124135
ascii?: boolean;
125136

src/type/__tests__/getJsonSchema.spec.ts

Lines changed: 130 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -41,103 +41,86 @@ test('can print a type', () => {
4141
.options({unknownFields: true});
4242
// console.log(JSON.stringify(type.toJsonSchema(), null, 2));
4343
expect(type.toJsonSchema()).toMatchInlineSnapshot(`
44-
{
45-
"properties": {
46-
"arrayProperty": {
47-
"items": {
48-
"type": [
49-
"string",
50-
"number",
51-
"boolean",
52-
"null",
53-
"array",
54-
"object",
55-
],
56-
},
57-
"type": "array",
58-
},
59-
"binaryOperation": {
60-
"type": "binary",
44+
{
45+
"properties": {
46+
"arrayProperty": {
47+
"items": {
48+
"type": [
49+
"string",
50+
"number",
51+
"boolean",
52+
"null",
53+
"array",
54+
"object",
55+
],
56+
},
57+
"type": "array",
58+
},
59+
"binaryOperation": {
60+
"type": "binary",
61+
},
62+
"binaryProperty": {
63+
"type": "binary",
64+
},
65+
"booleanProperty": {
66+
"type": "boolean",
67+
},
68+
"enumAsConst": {
69+
"anyOf": [
70+
{
71+
"const": "a",
72+
"type": "string",
6173
},
62-
"binaryProperty": {
63-
"type": "binary",
74+
{
75+
"const": "b",
76+
"type": "string",
6477
},
65-
"booleanProperty": {
66-
"type": "boolean",
78+
{
79+
"const": "c",
80+
"type": "string",
6781
},
68-
"enumAsConst": {
69-
"anyOf": [
70-
{
71-
"const": "a",
72-
"type": "string",
73-
},
74-
{
75-
"const": "b",
76-
"type": "string",
77-
},
78-
{
79-
"const": "c",
80-
"type": "string",
81-
},
82-
],
82+
],
83+
},
84+
"id": {
85+
"type": "string",
86+
},
87+
"map": {
88+
"patternProperties": {
89+
".*": {
90+
"type": "string",
8391
},
92+
},
93+
"type": "object",
94+
},
95+
"numberProperty": {
96+
"exclusiveMinimum": 3.14,
97+
"type": "number",
98+
},
99+
"objectProperty": {
100+
"properties": {
84101
"id": {
102+
"maxLength": 128,
103+
"minLength": 3,
104+
"pattern": "^[\\x00-\\x7F]*$",
85105
"type": "string",
86106
},
87-
"map": {
88-
"patternProperties": {
89-
".*": {
90-
"type": "string",
91-
},
92-
},
93-
"type": "object",
94-
},
95-
"numberProperty": {
96-
"exclusiveMinimum": 3.14,
97-
"type": "number",
98-
},
99-
"objectProperty": {
100-
"properties": {
101-
"id": {
102-
"maxLength": 128,
103-
"minLength": 3,
104-
"type": "string",
105-
},
106-
},
107-
"required": [
108-
"id",
109-
],
110-
"type": "object",
107+
},
108+
"required": [
109+
"id",
110+
],
111+
"type": "object",
112+
},
113+
"operation": {
114+
"properties": {
115+
"path": {
116+
"type": "string",
111117
},
112-
"operation": {
113-
"properties": {
114-
"path": {
115-
"type": "string",
116-
},
117-
"type": {
118-
"const": "replace",
119-
"title": "Always use replace",
120-
"type": "string",
121-
},
122-
"value": {
123-
"type": [
124-
"string",
125-
"number",
126-
"boolean",
127-
"null",
128-
"array",
129-
"object",
130-
],
131-
},
132-
},
133-
"required": [
134-
"type",
135-
"path",
136-
"value",
137-
],
138-
"type": "object",
118+
"type": {
119+
"const": "replace",
120+
"title": "Always use replace",
121+
"type": "string",
139122
},
140-
"optional": {
123+
"value": {
141124
"type": [
142125
"string",
143126
"number",
@@ -147,51 +130,69 @@ test('can print a type', () => {
147130
"object",
148131
],
149132
},
150-
"refField": {
151-
"$ref": "#/$defs/refId",
152-
},
153-
"tags": {
154-
"items": {
155-
"type": "string",
156-
},
157-
"title": "Tags",
158-
"type": "array",
159-
},
160-
"und": {
161-
"const": undefined,
162-
"type": "undefined",
163-
},
164-
"unionProperty": {
165-
"anyOf": [
166-
{
167-
"type": "string",
168-
},
169-
{
170-
"type": "number",
171-
},
172-
{
173-
"const": null,
174-
"type": "object",
175-
},
176-
],
177-
},
178133
},
179134
"required": [
180-
"id",
181-
"tags",
182-
"booleanProperty",
183-
"numberProperty",
184-
"binaryProperty",
185-
"arrayProperty",
186-
"objectProperty",
187-
"unionProperty",
188-
"operation",
189-
"binaryOperation",
190-
"map",
135+
"type",
136+
"path",
137+
"value",
191138
],
192139
"type": "object",
193-
}
194-
`);
140+
},
141+
"optional": {
142+
"type": [
143+
"string",
144+
"number",
145+
"boolean",
146+
"null",
147+
"array",
148+
"object",
149+
],
150+
},
151+
"refField": {
152+
"$ref": "#/$defs/refId",
153+
},
154+
"tags": {
155+
"items": {
156+
"type": "string",
157+
},
158+
"title": "Tags",
159+
"type": "array",
160+
},
161+
"und": {
162+
"const": undefined,
163+
"type": "undefined",
164+
},
165+
"unionProperty": {
166+
"anyOf": [
167+
{
168+
"type": "string",
169+
},
170+
{
171+
"type": "number",
172+
},
173+
{
174+
"const": null,
175+
"type": "object",
176+
},
177+
],
178+
},
179+
},
180+
"required": [
181+
"id",
182+
"tags",
183+
"booleanProperty",
184+
"numberProperty",
185+
"binaryProperty",
186+
"arrayProperty",
187+
"objectProperty",
188+
"unionProperty",
189+
"operation",
190+
"binaryOperation",
191+
"map",
192+
],
193+
"type": "object",
194+
}
195+
`);
195196
});
196197

197198
test('exports "ref" type to JSON Schema "$defs"', () => {

0 commit comments

Comments
 (0)