Skip to content

Instantly share code, notes, and snippets.

@s1ider
Created June 16, 2016 14:00
Show Gist options
  • Save s1ider/8c238413f0693f7c94d99141016dd536 to your computer and use it in GitHub Desktop.
Save s1ider/8c238413f0693f7c94d99141016dd536 to your computer and use it in GitHub Desktop.
JSON schema validation with Trafaret
# -*- coding: utf-8 -*-
import trafaret as t
class SchemaValidationError(t.DataError):
pass
# /hotels/destination_lookup
Entity = t.Dict({
t.Key('code'): t.String,
t.Key('type'): t.String,
t.Key('label'): t.String,
})
DestinationLookupSchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key('user_agent'): t.String,
t.Key('session_id'): t.String,
t.Key('locale'): t.String,
t.Key('customer_ip'): t.String | t.Null,
t.Key('order'): t.List(t.String),
t.Key('match_data'): t.Dict({
t.Key('landmarks', optional=True): t.List(Entity),
t.Key('airports_and_stations', optional=True): t.List(Entity),
t.Key('company_locations', optional=True): t.List(Entity),
t.Key('cities', optional=True): t.List(Entity),
t.Key('hotels', optional=True): t.List(Entity),
}),
})
# /hotels/get_list
HotelData = t.Dict({
t.Key('id'): t.Int,
t.Key('name'): t.String,
t.Key('address'): t.String,
t.Key('amenities'): t.Dict({
t.Key('property_amenities'): t.List(t.String),
t.Key('room_amenities'): t.List(t.String),
}),
t.Key('city', optional=True): t.String,
t.Key('state_province_code', optional=True): t.String(allow_blank=True) | t.Null,
t.Key('postal_code', optional=True): t.String(allow_blank=True) | t.Null,
t.Key('country_code'): t.String,
t.Key('latitude', optional=True): t.Float | t.Null,
t.Key('longitude', optional=True): t.Float | t.Null,
t.Key('distance', optional=True): t.Float | t.Null,
t.Key('direction', optional=True): t.String(allow_blank=True),
t.Key('hotel_in_destination'): t.Bool,
t.Key('trip_advisor_rating', optional=True): t.Float | t.Null,
t.Key('hotel_rating'): t.Float | t.Null,
t.Key('tmc_preferred'): t.Bool,
t.Key('tmc_preference_tier', optional=True): t.Int | t.Null,
t.Key('client_preferred'): t.Bool,
t.Key('client_preference_tier', optional=True): t.Int | t.Null,
t.Key('thumbnails'): t.List(t.URL),
t.Key('chain_info'): t.List(t.Dict({
t.Key('chain_code'): t.String,
t.Key('chain_name'): t.String,
})),
})
GetListSchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key('user_agent'): t.String,
t.Key('session_id'): t.String,
t.Key('locale'): t.String,
t.Key('customer_ip'): t.String | t.Null,
t.Key('more_results_available'): t.Bool,
t.Key('hotels_returned'): t.Int,
t.Key('hotels_found_total'): t.Int,
t.Key('current_page'): t.Int,
t.Key('page_results'): t.Int,
t.Key('hotels_data'): t.List(HotelData),
})
# /hotels/get_availability
HotelData = t.Dict({
t.Key('id'): t.Int,
t.Key('currency'): t.String | t.Null,
t.Key('is_available'): t.Bool,
t.Key('min_avg_rate'): t.Float | t.Null,
t.Key('min_total'): t.Float | t.Null,
t.Key('out_of_policy'): t.Bool | t.Null,
t.Key('out_of_policy_reason'): t.List(t.String) | t.Null,
})
GetAvailabilitySchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key('user_agent'): t.String,
t.Key('session_id'): t.String,
t.Key('locale'): t.String,
t.Key('customer_ip'): t.String | t.Null,
t.Key('currency'): t.String | t.Null,
t.Key('data_is_complete'): t.Bool,
t.Key('chunks_requested'): t.Int,
t.Key('chunks_received'): t.Int,
t.Key('chunks_with_errors'): t.Int,
t.Key('search_id'): t.String,
t.Key('warnings'): t.List(t.String, min_length=0),
t.Key('hotels_data'): t.List(HotelData, min_length=1),
})
# /hotels/get_detailed_info
HotelDetails = t.Dict({
t.Key('id'): t.Int,
t.Key('name'): t.String,
t.Key('property_category'): t.String | t.Null,
t.Key('address'): t.String,
t.Key('city'): t.String | t.Null,
t.Key('postal_code'): t.String(allow_blank=True) | t.Null,
t.Key('state_province_code'): t.String | t.Null,
t.Key('country_code'): t.String,
t.Key('latitude'): t.Float,
t.Key('longitude'): t.Float,
t.Key('airport_code'): t.String,
t.Key('trip_advisor_rating',
optional=True): t.Float | t.Null,
t.Key('trip_advisor_location_id',
optional=True): t.Int | t.Null,
t.Key('trip_advisor_review_count',
optional=True): t.Int | t.Null,
t.Key('hotel_rating'): t.Float | t.Null,
t.Key('rating_extra'): t.String(allow_blank=True),
t.Key('tmc_preferred'): t.Bool,
t.Key('tmc_preference_tier',
optional=True): t.Int | t.Null,
t.Key('client_preferred'): t.Bool,
t.Key('client_preference_tier',
optional=True): t.Int | t.Null,
t.Key('accepted_cards'): t.List(t.String),
t.Key('checkin_time'): t.String | t.Null,
t.Key('checkout_time'): t.String | t.Null,
t.Key('hotel_currency'): t.String,
t.Key('thumbnails'): t.List(t.URL),
t.Key('amenities'): t.Dict({
t.Key('property_amenities'): t.List(t.String),
t.Key('room_amenities'): t.List(t.String),
}),
t.Key('regions'): t.Dict({
t.Key('neighborhood'): t.List(t.String),
}),
t.Key('description'): t.String(allow_blank=True) | t.Null,
t.Key('short_description'): t.String(allow_blank=True) | t.Null,
t.Key('dining'): t.String(allow_blank=True) | t.Null,
t.Key('area_attraction'): t.String(allow_blank=True) | t.Null,
t.Key('policy'): t.String(allow_blank=True) | t.Null,
t.Key('recreation'): t.String(allow_blank=True) | t.Null,
t.Key('what_to_expect'): t.String(allow_blank=True) | t.Null,
t.Key('check_in_instructions'): t.String(allow_blank=True) | t.Null,
t.Key('hotel_policy'): t.String(allow_blank=True) | t.Null,
t.Key('mandatory_fees_description'): t.String(allow_blank=True) | t.Null,
t.Key('other_fees_description'): t.String(allow_blank=True) | t.Null,
t.Key('chain_info'): t.List(t.Dict({
t.Key('chain_code'): t.String,
t.Key('chain_name'): t.String,
})),
})
GetDetailedInfoSchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key('user_agent'): t.String,
t.Key('session_id'): t.String,
t.Key('locale'): t.String,
t.Key('customer_ip', optional=True): t.String | t.Null,
t.Key('hotels_data'): HotelDetails,
})
# /hotels/get_profile_info
TravelerProfile = t.Dict({
# t.Key('user_id'): t.Int,
t.Key('salutation'): t.String,
t.Key('first_name'): t.String,
t.Key('last_name'): t.String,
t.Key('email'): t.String,
t.Key('loyalty_programs'): t.List(t.Dict({
t.Key('loyalty_card_chain_code'): t.String,
t.Key('loyalty_card_chain_name'): t.String,
t.Key('loyalty_card_number'): t.String,
})),
t.Key('special_requests',
optional=True): t.String,
t.Key('room_smoking_preference',
optional=True): t.String | t.Null,
t.Key('payment_information'): t.Dict({
t.Key('credit_cards',
optional=True): t.List(t.Dict({
t.Key('card_index'): t.Int,
t.Key('last_four_digits'): t.String,
t.Key('card_type'): t.String,
t.Key('card_month'): t.String | t.Null,
t.Key('card_year'): t.String | t.Null,
t.Key('name_on_card'): t.String | t.Null,
}))
})
})
GetProfileInfoSchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key('user_agent'): t.String,
t.Key('session_id'): t.String,
t.Key('locale'): t.String,
t.Key('customer_ip'): t.String | t.Null,
t.Key('traveler_profile'): TravelerProfile
})
# /hotels/get_reservations_list
BookingData = t.Dict({
t.Key("booking_uid"): t.String,
t.Key("reservation_status"): t.String | t.Null,
t.Key("traveler_first_name"): t.String,
t.Key("traveler_last_name"): t.String,
t.Key("traveler_email"): t.String,
t.Key("booker_first_name"): t.String(allow_blank=True),
t.Key("booker_last_name"): t.String(allow_blank=True),
t.Key("booker_email"): t.String,
t.Key("checkin"): t.String,
t.Key("checkout"): t.String,
t.Key("hotel_id"): t.Int,
t.Key("hotel_name"): t.String,
t.Key("hotel_address"): t.String,
t.Key("hotel_city"): t.String,
t.Key("hotel_state_province_code"): t.String | t.Null,
t.Key("hotel_postal_code"): t.String | t.Null,
t.Key("hotel_country_code"): t.String,
t.Key("hotel_thumbnail"): t.String | t.Null,
t.Key("hotel_latitude"): t.Float | t.Null,
t.Key("hotel_longitude"): t.Float | t.Null,
t.Key("hotel_rating"): t.Float | t.Null,
})
GetReservationsListSchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key('session_id'): t.String,
t.Key('user_agent'): t.String,
t.Key('locale'): t.String,
t.Key('customer_ip'): t.String | t.Null,
t.Key('booking_data'): t.List(BookingData)
})
# /hotels/get_rates/
RatesData = t.Dict({
t.Key("provider"): t.String,
t.Key("rate_key"): t.String(min_length=64),
t.Key("rate_description"): t.String,
t.Key("rate_promoline"): t.String | t.Null,
t.Key("rate_value_adds"): t.List(t.String),
t.Key("commission"): t.Dict({
t.Key("commissionable"): t.Bool
}),
t.Key("out_of_policy"): t.Bool,
t.Key("out_of_policy_reason"): t.List(t.String) | t.Null,
t.Key("client_preferred"): t.Bool,
t.Key("tmc_preferred"): t.Bool,
t.Key("non_refundable"): t.Bool,
t.Key("cancellation_policy_known"): t.Bool,
t.Key("cancel_by"): t.String | t.Null,
t.Key("cancellation_policy"): t.String(allow_blank=True) | t.List(t.String(allow_blank=True)) | t.Null,
t.Key("paid_policy"): t.String,
t.Key("currency"): t.String,
t.Key("total"): t.Float,
t.Key("average"): t.Float,
t.Key("original_currency"): t.String,
t.Key("original_total"): t.Float,
t.Key("original_average"): t.Float,
t.Key("tax_mark"): t.String,
t.Key("taxes"): t.Float | t.Null,
t.Key("additional_rate_information",
optional=True): t.String | t.Null,
t.Key("original_taxes"): t.Float | t.Null,
t.Key("change_during_stay"): t.Bool,
})
GetRatesSchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key('user_agent', optional=True): t.String,
t.Key('session_id', optional=True): t.String,
t.Key('locale', optional=True): t.String,
t.Key('customer_ip', optional=True): t.String | t.Null,
t.Key('search_id'): t.String,
t.Key('chunks_requested'): t.Int,
t.Key('chunks_received'): t.Int,
t.Key('chunks_with_errors'): t.Int,
t.Key('hotel_id'): t.Int,
t.Key('hotel_name'): t.String,
t.Key('warnings'): t.List(t.String, min_length=0),
t.Key('rates'): t.List(RatesData),
})
# /hotels/get_rate_details/
RateDetails = t.Dict({
t.Key("rate_key"): t.String(min_length=64),
t.Key("out_of_policy"): t.Bool,
t.Key("cancellation_policy"): t.String(allow_blank=True) | t.List(t.String(allow_blank=True)),
t.Key("paid_policy"): t.String,
t.Key("cvv_required"): t.Bool,
t.Key('currency'): t.String,
t.Key("change_during_stay"): t.Bool,
t.Key("total"): t.Float,
t.Key("original_taxes"): t.Float,
t.Key('rate_description'): t.String,
t.Key("additional_rate_information",
optional=True): t.String | t.Null,
t.Key("commission"): t.Dict({
# t.Key("included"): t.Bool,
t.Key("commissionable"): t.Bool,
}),
t.Key("cancel_by"): t.String | t.Null,
t.Key("non_refundable"): t.Bool,
t.Key("original_average"): t.Float,
t.Key("original_currency"): t.String,
t.Key("provider"): t.String,
t.Key("rate_promoline"): t.String,
t.Key("original_total"): t.Float,
t.Key("out_of_policy_reason"): t.List(t.String) | t.Null,
t.Key("cancellation_policy_known"): t.Bool,
t.Key("payment_cards_accepted"): t.String(allow_blank=True),
t.Key("rate_value_adds"): t.List(t.String(allow_blank=True)),
t.Key("per_night"): t.List(t.Dict({
t.Key("date"): t.String,
t.Key("rate"): t.Float,
t.Key("original_rate"): t.Float,})),
t.Key("tax_mark"): t.String,
t.Key('special_requests'): t.Dict({
t.Key("sr_allowed"): t.Bool,
t.Key("sr_length"): t.Int,
}),
t.Key("average"): t.Float,
t.Key("client_preferred"): t.Bool,
t.Key("tmc_preferred"): t.Bool,
t.Key("taxes"): t.Float | t.Null,
t.Key('loyalty_programs'): t.List(t.Dict({
t.Key('loyalty_card_chain_code'): t.String,
t.Key('loyalty_card_chain_name'): t.String,})),
t.Key("billing_address_required"): t.Bool,
})
GetRateDetailsSchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key('locale', optional=True): t.String,
t.Key('session_id', optional=True): t.String,
t.Key('customer_ip', optional=True): t.String | t.Null,
t.Key('user_agent', optional=True): t.String,
t.Key('search_id'): t.String,
t.Key('hotel_name'): t.String,
t.Key('rates'): t.List(RateDetails)
})
# /hotels/get_reservation_details/
GetReservationDetailsSchema = t.Dict({
t.Key('errors'): t.List(t.String),
t.Key("hotel_state_province_code"): t.String | t.Null,
t.Key("traveler_first_name"): t.String,
t.Key("locale"): t.String,
t.Key("hotel_longitude"): t.Float | t.Null,
t.Key("paid_policy"): t.String,
t.Key("booker_first_name"): t.String,
t.Key("currency"): t.String,
t.Key("taxes"): t.Float,
t.Key("hotel_thumbnail"): t.String | t.Null,
t.Key("hotel_postal_code"): t.String | t.Null,
t.Key("booker_email"): t.String,
t.Key("booking_status"): t.String,
t.Key("total"): t.Float,
t.Key("original_taxes"): t.Float,
t.Key("rate_description"): t.String,
t.Key("traveler_last_name"): t.String,
t.Key("customer_ip"): t.String | t.Null,
t.Key("supplier_confirmation_number"): t.String | t.Null,
t.Key("cancellation_error"): t.Dict,
t.Key("hotel_address"): t.String,
t.Key("hotel_country_code"): t.String,
t.Key("hotel_rating"): t.Float | t.Null,
t.Key("traveler_email"): t.String,
t.Key("checkout"): t.String,
t.Key("original_average"): t.Float,
t.Key("cancellation_status"): t.String | t.Null,
t.Key("cancellation_confirmation_number"): t.String | t.Null,
t.Key("original_currency"): t.String,
t.Key("checkin"): t.String,
t.Key("hotel_id"): t.Int,
t.Key("guest_count"): t.Int,
t.Key("cancellation_policy"): t.String,
t.Key("pnr_id"): t.String,
t.Key("supplier_name"): t.String,
t.Key("booking_uid"): t.String,
t.Key("hotel_city"): t.String,
t.Key("hotel_latitude"): t.Float | t.Null,
t.Key("booker_last_name"): t.String,
t.Key("hotel_name"): t.String,
t.Key("res_payment_description"): t.String,
t.Key("gds_name"): t.String,
t.Key("original_total"): t.Float,
t.Key("average"): t.Float,
t.Key("session_id", optional=True): t.String,
t.Key("booking_error"): t.Dict({
t.Key("is_recoverable",
optional=True): t.Bool,
t.Key("internal_error_text",
optional=True): t.String,
t.Key("external_error_text",
optional=True): t.String
}),
t.Key("user_agent", optional=True): t.String,
t.Key("hotel_confirmation_number"): t.String | t.Null
})
# hotels/book/
BookSchema = t.Dict({
t.Key('user_agent'): t.String,
t.Key('session_id'): t.String,
t.Key('locale'): t.String,
t.Key('customer_ip'): t.String | t.Null,
t.Key('booking_uid'): t.String,
t.Key('errors'): t.List(t.String),
})
# hotels/cancel_reservation
CancelReservationSchema = t.Dict({
t.Key('user_agent'): t.String,
t.Key('session_id'): t.String,
t.Key('locale'): t.String,
t.Key('customer_ip'): t.String | t.Null,
t.Key('cancellation_status'): t.String,
t.Key('booking_uid'): t.String,
t.Key('errors'): t.List(t.String),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment