import { isValidISODateString } from 'iso-datestring-validator'; import { array, InferType, mixed, number, object, string } from 'yup'; import { SERIES_SLUG_REGEXP, ETH_ADDRESS_REGEXP, SERIES_ID_REGEXP, } from '~/regexp.constants'; const VALID_RIGHTS_VALUES = [ 'Editorial', 'Extended Editorial', 'Limited Commercial', 'Unlimited Commercial', 'Exclusive', ]; export const SERIES_ENTRY_SCHEMA = object() .shape({ editions: number().integer().min(1), name: string().required(), description: string(), image: string().required(), animation: string(), external_url: string().url(), attributes: array() .of( object().shape({ value: mixed() .required() .when('trait_type', { is: 'Rights', then: () => string().oneOf(VALID_RIGHTS_VALUES).required(), }), trait_type: string().required(), }), ) .test('supports-rights', "${path} doesn't have a Rights trait", (value) => typeof value !== 'undefined' ? value.some(({ trait_type }) => trait_type === 'Rights') : false, ) .test( 'no-editions-trait', "${path} trait type is 'Editions', which is prohibited (did you mean 'Edition'?)", (value) => typeof value !== 'undefined' ? value.every(({ trait_type }) => trait_type !== 'Editions') : true, ), }) .noUnknown(); export type SeriesEntry = InferType; export const SERIES_SCHEMA = object() .shape({ id: string().required().matches(SERIES_ID_REGEXP), slug: string().required().matches(SERIES_SLUG_REGEXP), name: string().required(), symbol: string().required(), contract_metadata: object() .shape({ name: string().required(), symbol: string().required(), description: string(), image: string(), banner_image: string(), seller_fee_basis_points: number().integer().min(0).lessThan(10_000), fee_recipient: string().matches(ETH_ADDRESS_REGEXP), drop_date: string().test( 'is-iso-datetime', '${path} is not a valid datetime string', (value) => typeof value !== 'undefined' ? isValidISODateString(value) : true /* NB: returns true to allow skipping the field */, ), external_link: string().url(), }) .noUnknown(), entries: array().of(SERIES_ENTRY_SCHEMA).required(), extras: array().of(string().required()), }) .noUnknown(); export type Series = InferType;