json: add date format + fix uuid

This commit is contained in:
ochafik 2024-03-11 02:58:14 +00:00
parent d736e928d2
commit 9a61802a28
3 changed files with 714 additions and 675 deletions

View file

@ -258,6 +258,8 @@ class SchemaConverter:
lit += f'\\{pattern[i]}'
i += 1
else:
if pattern[i] == '"':
lit += '\\'
lit += pattern[i]
i += 1
if lit:
@ -375,7 +377,11 @@ class SchemaConverter:
# TODO: support string formats "uri", "date", "date-time"
elif schema_type in (None, 'string') and re.match(r'^uuid[1-5]?$', schema.get('format', '')):
return self._visit_pattern('^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$', 'uuid')
return self._visit_pattern('^"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"$', 'uuid')
elif schema_type in (None, 'string') and schema.get('format') == 'date':
# Adapted from full-date at https://www.rfc-editor.org/rfc/rfc3339.txt
return self._visit_pattern('^"[0-9]{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])"$', 'date')
elif schema_type == 'object' and len(schema) == 1 or schema_type is None and len(schema) == 0:
# This depends on all primitive types

File diff suppressed because it is too large Load diff

View file

@ -277,6 +277,9 @@ export class SchemaConverter {
}
i += 1;
} else {
if (pattern[i] === '"') {
lit += '\\';
}
lit += pattern[i];
i += 1;
}
@ -397,7 +400,10 @@ export class SchemaConverter {
} else if ((schemaType === undefined || schemaType === 'string') && 'pattern' in schema) {
return this._visitPattern(schema.pattern, ruleName);
} else if ((schemaType === undefined || schemaType === 'string') && /^uuid[1-5]?$/.test(schema.format || '')) {
return this._visitPattern('^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$', 'uuid');
return this._visitPattern('^"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"$', 'uuid');
} else if ((schemaType === undefined || schemaType === 'string') && schema.format === 'date') {
// Adapted from full-date at https://www.rfc-editor.org/rfc/rfc3339.txt
return this._visitPattern('^"[0-9]{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])"$', 'date')
} else if (schemaType === 'object' && Object.keys(schema).length === 1 || schemaType === undefined && Object.keys(schema).length === 0) {
// This depends on all primitive types
for (const [t, r] of Object.entries(PRIMITIVE_RULES)) {