refactor: support string literals without quotes
This commit is contained in:
parent
51a11a26bf
commit
97c8b1966c
|
|
@ -26,6 +26,15 @@ describe('Type generation for string literals and unions', () => {
|
||||||
expect(schema.schema.type).toBe('stringLiteral');
|
expect(schema.schema.type).toBe('stringLiteral');
|
||||||
expect(schema.parse('"hello-world"')).toBe('hello-world');
|
expect(schema.parse('"hello-world"')).toBe('hello-world');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should parse string literal without quotes', () => {
|
||||||
|
const schema = defineSchema('"on"');
|
||||||
|
expect(schema.schema.type).toBe('stringLiteral');
|
||||||
|
// 不带引号的值应该像普通字符串一样解析
|
||||||
|
expect(schema.parse('on')).toBe('on');
|
||||||
|
expect(schema.validator('on')).toBe(true);
|
||||||
|
expect(schema.validator('off')).toBe(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Union schema', () => {
|
describe('Union schema', () => {
|
||||||
|
|
@ -43,9 +52,11 @@ describe('Type generation for string literals and unions', () => {
|
||||||
const schema = defineSchema('"on" | "off"');
|
const schema = defineSchema('"on" | "off"');
|
||||||
expect(schema.schema.type).toBe('union');
|
expect(schema.schema.type).toBe('union');
|
||||||
|
|
||||||
// Both values should be valid
|
// 带引号和不带引号的值都应该有效
|
||||||
expect(schema.parse('"on"')).toBe('on');
|
expect(schema.parse('"on"')).toBe('on');
|
||||||
expect(schema.parse('"off"')).toBe('off');
|
expect(schema.parse('"off"')).toBe('off');
|
||||||
|
expect(schema.parse('on')).toBe('on');
|
||||||
|
expect(schema.parse('off')).toBe('off');
|
||||||
expect(schema.validator('on')).toBe(true);
|
expect(schema.validator('on')).toBe(true);
|
||||||
expect(schema.validator('off')).toBe(true);
|
expect(schema.validator('off')).toBe(true);
|
||||||
expect(schema.validator('maybe')).toBe(false);
|
expect(schema.validator('maybe')).toBe(false);
|
||||||
|
|
@ -71,9 +82,14 @@ describe('Type generation for string literals and unions', () => {
|
||||||
const schema = defineSchema('[name: string; status: "active" | "inactive"]');
|
const schema = defineSchema('[name: string; status: "active" | "inactive"]');
|
||||||
expect(schema.schema.type).toBe('tuple');
|
expect(schema.schema.type).toBe('tuple');
|
||||||
|
|
||||||
const value = schema.parse('[john; "active"]');
|
// 带引号和不带引号的值都应该有效
|
||||||
expect(value).toEqual(['john', 'active']);
|
const value1 = schema.parse('[john; "active"]');
|
||||||
|
expect(value1).toEqual(['john', 'active']);
|
||||||
expect(schema.validator(['john', 'active'])).toBe(true);
|
expect(schema.validator(['john', 'active'])).toBe(true);
|
||||||
|
|
||||||
|
const value2 = schema.parse('[john; active]');
|
||||||
|
expect(value2).toEqual(['john', 'active']);
|
||||||
|
|
||||||
expect(schema.validator(['john', 'unknown'])).toBe(false);
|
expect(schema.validator(['john', 'unknown'])).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -81,8 +97,13 @@ describe('Type generation for string literals and unions', () => {
|
||||||
const schema = defineSchema('("pending" | "approved" | "rejected")[]');
|
const schema = defineSchema('("pending" | "approved" | "rejected")[]');
|
||||||
expect(schema.schema.type).toBe('array');
|
expect(schema.schema.type).toBe('array');
|
||||||
|
|
||||||
const value = schema.parse('["pending"; "approved"]');
|
// 带引号和不带引号的值都应该有效
|
||||||
expect(value).toEqual(['pending', 'approved']);
|
const value1 = schema.parse('["pending"; "approved"]');
|
||||||
|
expect(value1).toEqual(['pending', 'approved']);
|
||||||
|
|
||||||
|
const value2 = schema.parse('[pending; approved]');
|
||||||
|
expect(value2).toEqual(['pending', 'approved']);
|
||||||
|
|
||||||
expect(schema.validator(['pending', 'approved'])).toBe(true);
|
expect(schema.validator(['pending', 'approved'])).toBe(true);
|
||||||
expect(schema.validator(['pending', 'unknown'])).toBe(false);
|
expect(schema.validator(['pending', 'unknown'])).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
@ -91,8 +112,13 @@ describe('Type generation for string literals and unions', () => {
|
||||||
const schema = defineSchema('"item"[]');
|
const schema = defineSchema('"item"[]');
|
||||||
expect(schema.schema.type).toBe('array');
|
expect(schema.schema.type).toBe('array');
|
||||||
|
|
||||||
const value = schema.parse('["item"; "item"; "item"]');
|
// 带引号和不带引号的值都应该有效
|
||||||
expect(value).toEqual(['item', 'item', 'item']);
|
const value1 = schema.parse('["item"; "item"; "item"]');
|
||||||
|
expect(value1).toEqual(['item', 'item', 'item']);
|
||||||
|
|
||||||
|
const value2 = schema.parse('[item; item; item]');
|
||||||
|
expect(value2).toEqual(['item', 'item', 'item']);
|
||||||
|
|
||||||
expect(schema.validator(['item', 'item'])).toBe(true);
|
expect(schema.validator(['item', 'item'])).toBe(true);
|
||||||
expect(schema.validator(['item', 'other'])).toBe(false);
|
expect(schema.validator(['item', 'other'])).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -126,9 +126,9 @@ class ValueParser {
|
||||||
|
|
||||||
private parseStringLiteralValue(schema: StringLiteralSchema): string {
|
private parseStringLiteralValue(schema: StringLiteralSchema): string {
|
||||||
const quote = this.peek();
|
const quote = this.peek();
|
||||||
if (quote !== '"' && quote !== "'") {
|
|
||||||
throw new ParseError(`Expected string literal with quotes for value "${schema.value}"`, this.pos);
|
// 支持带引号或不带引号的字符串值
|
||||||
}
|
if (quote === '"' || quote === "'") {
|
||||||
this.consume(); // Consume opening quote
|
this.consume(); // Consume opening quote
|
||||||
|
|
||||||
let value = '';
|
let value = '';
|
||||||
|
|
@ -160,6 +160,28 @@ class ValueParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new ParseError('Unterminated string literal', this.pos);
|
throw new ParseError('Unterminated string literal', this.pos);
|
||||||
|
} else {
|
||||||
|
// 不带引号的字符串,像普通字符串一样解析
|
||||||
|
let value = '';
|
||||||
|
while (this.pos < this.input.length) {
|
||||||
|
const char = this.peek();
|
||||||
|
if (char === ';' || char === ']' || char === ')') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
value += this.consume();
|
||||||
|
}
|
||||||
|
|
||||||
|
value = value.trim();
|
||||||
|
|
||||||
|
if (value !== schema.value) {
|
||||||
|
throw new ParseError(
|
||||||
|
`Invalid value '${value}'. Expected '${schema.value}'`,
|
||||||
|
this.pos - value.length
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private parseUnionValue(schema: UnionSchema): unknown {
|
private parseUnionValue(schema: UnionSchema): unknown {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue