Skip to content

Instantly share code, notes, and snippets.

@making
Created September 1, 2023 03:36
Show Gist options
  • Save making/4708e293f75a6f4d0d9dfaa14d8b6580 to your computer and use it in GitHub Desktop.
Save making/4708e293f75a6f4d0d9dfaa14d8b6580 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "eb69353f-ed52-4ac5-9658-0ffdcbc86a7e",
"metadata": {},
"outputs": [],
"source": [
"record Car(String manufacturer, String licencePlate, int seatCount) {}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9a332f2e-3e8e-4b49-aea0-0ef59f77e021",
"metadata": {},
"outputs": [],
"source": [
"var car = new Car(\"Morris\", \"DD-AB-123\", 2);"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "64076fbb-27d7-4405-9507-82f20da14d07",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Car[manufacturer=Morris, licencePlate=DD-AB-123, seatCount=2]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"car"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ed9c6976-48d2-470b-ae2c-7c9fe76fda87",
"metadata": {},
"outputs": [],
"source": [
"%maven am.ik.yavi:yavi:0.13.1"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "93a3185d-a884-47cc-91c1-3c001c75f5e9",
"metadata": {},
"outputs": [],
"source": [
"import am.ik.yavi.builder.ValidatorBuilder;\n",
"import am.ik.yavi.core.Validator;"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4b501825-9c3e-46b0-9987-a8118c1519f5",
"metadata": {},
"outputs": [],
"source": [
"var validator = ValidatorBuilder.<Car>of()\n",
" .constraint(Car::manufacturer, \"manufacturer\", c -> c.notNull())\n",
" .constraint(Car::licencePlate, \"licensePlate\", c -> c.notNull().greaterThanOrEqual(2).lessThanOrEqual(14))\n",
" .constraint(Car::seatCount, \"seatCount\", c -> c.greaterThanOrEqual(2))\n",
" .build();"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3fad7da7-1220-4f15-89b4-904311c3f22d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"validator.validate(car);"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "e5adb638-81f0-4613-b9ae-acb1f2dd2ee2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[ConstraintViolation{name='licensePlate', messageKey='container.greaterThanOrEqual', defaultMessageFormat='The size of \"{0}\" must be greater than or equal to {1}. The given size is {2}', args=[licensePlate, 2, 1]}]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"validator.validate(new Car(\"Morris\", \"D\", 4));"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d5d6456-caad-4ade-accf-d46000aa453c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Java",
"language": "java",
"name": "java"
},
"language_info": {
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "17.0.7+8-LTS-jvmci-23.0-b12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment